php - 使用我的 PHP 登录页面验证 mySQL 数据库时遇到问题
问题描述
我正在尝试使用 PHP 为我的网站创建登录页面,但似乎无法让登录验证正常工作。我正在尝试验证数据库中存在但似乎无法正常工作的用户名和密码。有人看到我可能错过的任何东西吗?
<?php // login.php
// This page lets people log in to the site.
define('TITLE', 'Clermont County Disc Golf Club');
require('templates/header.html');
require_once('discgolf_fns.php');
// Print some introductory text:
print '<h2>Login Form</h2>
<p align=center>Login to view member only secret meetups!</p>';
?><!DOCTYPE HTML>
<html lang="en">
<div id="wrapper">
<?php
$conn = db_connect();
$username = $_POST['username'];
$passwd = $_POST['passwd'];
if($_SERVER['REQUEST_METHOD'] == 'POST') {
if(!empty($_POST['username']) && (!empty($_POST['passwd']))){
if (mysqli_connect_errno()) {
echo "Error: Could not connect to database. Please try again later.";
exit;
}
$conn = db_connect();
$query = "select * from user where username = '".$username."' and passwd = sha1('".$passwd."')";
$result = $conn->query($query);
if ($result->num_rows > 0)
{
session_start();
$_SESSION['username'] = $username;
$_SESSION['loggedin'] = time();
print "User found";
$conn->close();
ob_end_clean();
header('Location: welcome.php');
exit();
}else{
print '<p>The submitted username and password do not match. Go back and try again</p>';
}
}else{
print '<p>Please make sure you enter both email and password. Go back and try again</p>';
}
}else{
print '<form action="login.php" method="post" class="loginform">
<p align=center><label for="username"> User Name: </label><input type="text" name="username" size"20"></p>
<p align=center><label for="passwd"> Password: </label><input type="password" name="passwd" size"20"></p>
<p align=center><input type="submit" name="submit" value="Log In!" class ="button--pill"></p>
</form>';
}
?>
</div>
</html>
<?php include('templates/footer.html'); ?>
解决方案
推荐阅读
- python - 如何在套接字上制作 https 服务器
- python - 遇到不支持的操作数类型问题
- angular - AWS amplify TruncateBigNumber 无法转换为字符串
- amazon-web-services - 如何识别 ec2 实例是现货实例还是按需实例
- javascript - 在 SVG 中对齐饼图的 textPath
- android-studio - 用于创建新文件/类的 Android Studio 快捷方式
- javascript - 类型为 ( A | B ) 的 TS 函数,具有单一返回类型 B。如何在不出现类型错误的情况下完成此操作?
- google-apps-script - 如何使用 Apps 脚本自动将每行 Google 表格文档的图像和图表插入幻灯片模板(邮件合并)?
- jenkins - 让 jenkins 在不同节点池中的 gke 上生成代理
- angular - 基于角材料页面的动态文本