求助 PHP 登录不能正常工作?
Posted
技术标签:
【中文标题】求助 PHP 登录不能正常工作?【英文标题】:Help PHP Login not working correctly? 【发布时间】:2011-02-07 10:13:33 【问题描述】:好的,所以我正在尝试创建一个登录脚本,这里我使用的是 php5 和 mysqli,我想问您是否可以让我知道为什么它一直只返回“错误:您的用户名和密码与我们的D b”?当我创建了一个帐户并且它显然是这样的?这可能是我错过的显而易见的事情。这是代码...
//检查表单是否已经提交
if (isset($_POST['login']))
//Check if username and password are empty
if ($_POST['username']!='' && $_POST['password']!='')
//Create query to check username and password to database
$validate_user = $mysqli->query('SELECT id, username, password, active FROM users WHERE ="'.$mysqli->real_escape_string(md5($_POST['username'])).' AND password = "'.$mysqli->real_escape_string(md5($_POST['password'])).'"');
//We check if the query returns true
if ($validate_user->num_rows == 1)
$row = $validate_user->fetch_assoc();
//Check if the user has activated there account
if ($row['activated'] == 1)
$_SESSION['id'] = $row['id'];
$_SESSION['logged_in'] = true;
Header('Location: ../main/index.php');
//Show this error if activation returns as 0
else
$error = '<p class="error">Please activate your account.</p>';
//Show this error if the details matched any in the db
else
$error = '<p class="error">Your username and password are not in our database!</p>';
//Show this error if the username and password field have not been entered
else
$error = '<p class="error">Please enter your username and password.</p>';
【问题讨论】:
您以什么形式将密码保存在数据库中?您是否同时保留 - 用户名和密码 md5 散列?因为这就是你的脚本所期望的。 不,我不知道。哈愚蠢,谢谢,我猜是因为我不在数据库中散列用户名,而是尝试在这里。 嗯,我似乎已经通过从用户名查询中删除“md5()”来纠正它......但仍然是同样的错误...... 【参考方案1】:查询在 WHERE 子句后不包含“用户名=”,所以应该是:
$mysqli->query('SELECT id, username, password, active FROM users WHERE username ="'.$mysqli->real_escape_string(md5($_POST['username'])).' AND password = "'.$mysqli->real_escape_string(md5($_POST['password'])).'"');
【讨论】:
【参考方案2】:行:
$validate_user = $mysqli->query('SELECT id, username, password, active FROM users WHERE ="'.$mysqli->real_escape_string(md5($_POST['username'])).' AND password = "'.$mysqli->real_escape_string(md5($_POST['password'])).'"');
应该是:
$validate_user = $mysqli->query('SELECT id, username, password, active FROM users WHERE username ="'.$mysqli->real_escape_string(md5($_POST['username'])).' AND password = "'.$mysqli->real_escape_string(md5($_POST['password'])).'"');
【讨论】:
【参考方案3】:您的查询经过简化,如下所示:
SELECT id, username, password, active FROM users WHERE ="'.md5(
我确定应该是:
SELECT id, username, password, active FROM users WHERE username="'.
【讨论】:
以上是关于求助 PHP 登录不能正常工作?的主要内容,如果未能解决你的问题,请参考以下文章