请您帮我解决这个问题好吗?我不知道我做错了什么。我与数据库的连接工作正常,但无法登录。
login.php
<?php
session_start();
$username= $_POST["username"];
$password= $_POST["password"];
include("/inc/connect.inc.php");
if(!isset($conn)){
$conn = null;
header('Location: index.php');
}
else{
$query = $conn->prepare("SELECT * FROM `users` WHERE `username`='$username' AND `password`='$password'");
$query ->execute(array(':username' =>$username, ':password' =>$password)
);
if ( ($query->rowCount() == 0) && ( ($password == null) or ($username == null) ) ){
echo "<h3>Please enter your username and password</h3>";
$conn = null;
header("Refresh: 3;URL=index.php");
}
else if ($query->rowCount() == 1)
{
$_SESSION['user_logged'] = $_POST['username'];
unset($username);
unset($password);
echo "<h3>Your password is correct</h3>";
$conn = null;
header("Refresh: 3;URL=interface.php");
}
else {
echo "<h3>The username / password combination entered is incorrect!</h3>";
unset($username);
unset($password);
$conn = null;
header("Refresh: 3;URL=index.php");
}
}
?>
以前我不理解stackoverflow规则。我希望这次我的问题更准确。我已经做了很多工作才达到这一点,现在只有登录数据库的问题。我的$query = $conn->prepare
没有找到任何东西。它随时跳转到输入的用户名/密码组合不正确!。如果我将用户名或/和密码留空或输入正确的用户名和密码,结果总是相同的。
最佳答案
我发布此内容是因为它解决了OP代码不起作用的真正问题。
首先,bname
中存在拼写错误,在您的连接中应读取为 dbname
。
现在,您将 MySQL API 与 mysql_
函数和 PDO 混合在一起。
- 这些不同的 API 不会相互混合。
在评论中您说:
"You are welcome to convert my code to PDO. I can understand that this code is mixed up but have no idea how to fix it."
- 我不喜欢在这里表现得像“坏人”,或者听起来像“坏人”,但这不是我们的工作,将您的
mysql_
代码转换为 PDO,这是您的工作。我们不会在 Stack 上转换代码,我们会帮助解决有问题的代码。
有大量教程供您学习和使用。
以下只是其中的一些,您可以在 Stack/Google 上进一步研究:
- http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers
- http://code.tutsplus.com/tutorials/why-you-should-be-using-phps-pdo-for-database-access--net-12059
- http://www.phpro.org/tutorials/Introduction-to-PHP-PDO.html
PHP.net 上还有手册
堆栈上:
<小时/>关于密码 MD5:
$password = md5($password);
MD5 已过时,被认为已损坏,不再安全地用于密码存储。
我建议您使用CRYPT_BLOWFISH或 PHP 5.5 的 password_hash()
功能。对于 PHP < 5.5,请使用 password_hash() compatibility pack
。
"I'm getting blank screen. Any ideas?"
关于“空白屏幕”。
这意味着您有语法错误。
执行 error_reporting(0);
意味着“关闭所有错误报告”
根据 PHP.net 上的手册
您需要做的是打开错误报告,而不是关闭。
添加error reporting到文件顶部,这将有助于查找错误。
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// rest of your code
旁注:错误报告只能在暂存阶段完成,而不能在生产阶段完成。
关于php - PDO连接登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29717472/