Php mysql_fetch assoc拒绝返回密码行。需要帮助
用户名行显示得很完美,但密码行拒绝显示 我在这里不知所措 有人知道解决办法是什么吗 这是我的密码:Php mysql_fetch assoc拒绝返回密码行。需要帮助,php,Php,用户名行显示得很完美,但密码行拒绝显示 我在这里不知所措 有人知道解决办法是什么吗 这是我的密码: <?php //Mass include file include ("includes/mass.php"); //This is the login script //Grabbing the login values and storing them $username = $_POST['username']; $password = $_POST['password']; $
<?php
//Mass include file
include ("includes/mass.php");
//This is the login script
//Grabbing the login values and storing them
$username = $_POST['username'];
$password = $_POST['password'];
$submit = $_POST['submit'];
if (isset($submit))
{
if (strlen($username)<2) // put || ($username==(same as value on the database)
{
echo ("<br>You must enter a longer username</br>");
}
elseif (strlen($password)<=6)
{
echo ("<br>You must enter a longer password<br>");
}
else
{
$sql = "SELECT * FROM user WHERE username = '$username'";
$query = mysql_query($sql);
$numrows = mysql_num_rows($query);
if ($numrows != 0)
{
while ($row = mysql_fetch_assoc($query))
$dbusername = $row['username'];
$dbpassword = $row['password'];
if ($dbusername == $username && $dbpassword == $password)
{
echo "your in!";
}
else
{
echo "Wrong info";
}
}
else
{
die ("That username doesnt exist");
}
}
}
?>
while之后缺少一个括号
两者
应该在while循环中,但实际情况并非如此
您需要执行以下操作:
while ($row = mysql_fetch_assoc($query)) {
$dbusername = $row['username'];
$dbpassword = $row['password'];
if ($dbusername == $username && $dbpassword == $password) {
echo "your in!";
}
....
除了马修所说的之外:
您不应该向用户显示“用户名不存在”之类的消息。这为想要入侵的黑客提供了有价值的信息。如果用户提供了错误的用户名和/或密码,则应显示“无效用户名或密码”。while之后缺少括号
两者
应该在while循环中,但实际情况并非如此
您需要执行以下操作:
while ($row = mysql_fetch_assoc($query)) {
$dbusername = $row['username'];
$dbpassword = $row['password'];
if ($dbusername == $username && $dbpassword == $password) {
echo "your in!";
}
....
除了马修所说的之外:
您不应该向用户显示“用户名不存在”之类的消息。这为想要入侵的黑客提供了有价值的信息。如果用户提供了错误的用户名和/或密码,则应显示“无效用户名或密码”。如果用户名不唯一,脚本将失败。可能是这样吗
除此之外,将密码存储为纯文本而不清理用户输入:非常糟糕的想法
编辑:顺便问一下,为什么不在查询本身中检查有效的用户名/密码组合:
... WHERE username='" . mysql_real_escape_string($username) . "'
AND password='" . some_hashing_function($password) . "'";
另一个编辑:你必须仔细观察正在发生的事情;我的初始答案(非唯一用户id)可能是个问题,但@codaddict的答案可能已经解决了您的问题
基本上,如果没有大括号,你的陈述是这样的:
while ($row = mysql_fetch_assoc($query))
$dbusername = $row['username'];
$dbpassword = $row['password'];
....
与php相同,如下所示:
while ($row = mysql_fetch_assoc($query))
{
$dbusername = $row['username'];
}
$dbpassword = $row['password'];
...
因此,当您要填写$dbpassword时,$row已为空。如果用户名不唯一,脚本将失败。可能是这样吗
I like the suggestion about the {} that normally encompasses the code that occurs in a while loop. Id replace
while ($row = mysql_fetch_assoc($query))
$dbusername = $row['username'];
$dbpassword = $row['password'];
with
while ($row = mysql_fetch_assoc($query))
{
$dbusername = $row['username'];
$dbpassword = $row['password'];
}
// this lets the loop finish so $dbusername and $dbpassword are potentially set
// so you can contine with your if statements
Another thing to check is your html - check your password field looks something
like <input type="password" name="password" />
You could always try echo $password; immediately after $_POST['password'] to see
if it existed in the first place. Then echo any of your variables after they have
been set to see if they exist.
除此之外,将密码存储为纯文本而不清理用户输入:非常糟糕的想法
编辑:顺便问一下,为什么不在查询本身中检查有效的用户名/密码组合:
... WHERE username='" . mysql_real_escape_string($username) . "'
AND password='" . some_hashing_function($password) . "'";
另一个编辑:你必须仔细观察正在发生的事情;我的初始答案(非唯一用户id)可能是个问题,但@codaddict的答案可能已经解决了您的问题
基本上,如果没有大括号,你的陈述是这样的:
while ($row = mysql_fetch_assoc($query))
$dbusername = $row['username'];
$dbpassword = $row['password'];
....
与php相同,如下所示:
while ($row = mysql_fetch_assoc($query))
{
$dbusername = $row['username'];
}
$dbpassword = $row['password'];
...
因此,当您要填写$dbpassword时,$行已经为空
I like the suggestion about the {} that normally encompasses the code that occurs in a while loop. Id replace
while ($row = mysql_fetch_assoc($query))
$dbusername = $row['username'];
$dbpassword = $row['password'];
with
while ($row = mysql_fetch_assoc($query))
{
$dbusername = $row['username'];
$dbpassword = $row['password'];
}
// this lets the loop finish so $dbusername and $dbpassword are potentially set
// so you can contine with your if statements
Another thing to check is your html - check your password field looks something
like <input type="password" name="password" />
You could always try echo $password; immediately after $_POST['password'] to see
if it existed in the first place. Then echo any of your variables after they have
been set to see if they exist.
我喜欢关于{}的建议,它通常包含while循环中发生的代码。Id替换
while($row=mysql\u fetch\u assoc($query))
$dbusername=$row['username'];
$dbpassword=$row['password'];
具有
while($row=mysql\u fetch\u assoc($query))
{
$dbusername=$row['username'];
$dbpassword=$row['password'];
}
//这将使循环完成,因此可能会设置$dbusername和$dbpassword
//所以你可以继续你的if语句
另一件要检查的事情是你的html-检查你的密码字段
喜欢
您可以随时尝试echo$password;紧接着$\u POST['password']查看
如果它一开始就存在的话。然后在你的变量完成后回显它们
已设置为查看它们是否存在。
我喜欢关于{}的建议,它通常包含while循环中发生的代码。Id替换
while($row=mysql\u fetch\u assoc($query))
$dbusername=$row['username'];
$dbpassword=$row['password'];
具有
while($row=mysql\u fetch\u assoc($query))
{
$dbusername=$row['username'];
$dbpassword=$row['password'];
}
//这将使循环完成,因此可能会设置$dbusername和$dbpassword
//所以你可以继续你的if语句
另一件要检查的事情是你的html-检查你的密码字段
喜欢
您可以随时尝试echo$password;紧接着$\u POST['password']查看
如果它一开始就存在的话。然后在你的变量完成后回显它们
已设置为查看它们是否存在。
您永远不应该在数据库中保留未保存的密码,并且应该使用准备好的语句。上面的代码完全容易受到SQL注入攻击。是的,我知道。我这样做最初是为了测试。当我获得正确的功能时,我会关注安全性。你所说的准备好的陈述是什么意思?既然你是第一次这样做,我想告诉你,你可以通过点击答案旁边的“正确标记”来接受对你帮助最大的答案。谢谢你。我刚刚打了个勾,你不应该在数据库中保留未加密的密码,你应该使用事先准备好的语句。上面的代码完全容易受到SQL注入攻击。是的,我知道。我这样做最初是为了测试。当我获得正确的功能时,我会关注安全性。你所说的准备好的陈述是什么意思?既然你是第一次这样做,我想告诉你,你可以通过点击答案旁边的“正确标记”来接受对你帮助最大的答案。谢谢你。我刚刚打了个勾,谢谢你的意见。此脚本用于登录。没有注册。这就是为什么我没有包括哈希。Tapha,那没关系,当你在注册时存储密码时,你应该存储一个哈希,为了在登录时匹配,你还需要哈希来比较。如果你有一个满是纯文本密码的数据库,很容易一次散列所有密码;如有必要,请在新字段中输入密码,并在一切正常时删除纯文本密码。(拍脑袋)谢谢。我犯了这些错误我是个新手。嗨,杰伦,谢谢你的意见。此脚本用于登录。没有注册。这就是为什么我没有包括哈希。Tapha,这没关系,当你在注册时存储密码时,你应该存储一个哈希,并在注册时匹配