我在尝试在我的网站上创建登录系统时遇到了另一个问题。我有一个数据库和一个只有一个用户的“用户”表。目前,除非您已登录,否则索引页面会将您重定向到登录页面。我有一个用户名和密码字段,用于获取您输入的数据并使用用户数据库进行检查。但是,我无法连接到我的数据库,每当我尝试提交登录信息时,它会导致500内部服务器错误,说“website.com无法处理此请求”。这是我的login.php代码:
<?php
session_start();
if(isset($_POST['login'])) {
include_once("db.php");
$username = strip_tags($_POST['username']);
$password = strip_tags($_POST['password']);
$username = stripslashes($username);
$password = stripslashes($password);
$username = mysqli_real_escape_string($db, $username);
$password = mysqli_real_escape_string($db, $password);
$password = md5($password);
//echo "$username - $password";
$sql = "SELECT * FROM users WHERE username='$username' LIMIT 1";
$query = mysqli_query($db, $sql);
$row = mysqli_fetch_array($query);
$id = $row['id'];
$db_password = $row['password'];
if($password == $db_password) {
$_SESSION['username'] = $username;
$_SESSION['id'] = $id;
header("Location: index.php");
} else {
echo "You didn't enter the correct details!";
}
}
?>
<html>
<head>
<title>Login</title>
</head>
<body>
<h1 style="font-family: Tahoma;">Login</h1>
<form action="login.php" method="post" enctype="multipart/form-data">
<input placeholder="Username" name="username" type="text" autofocus>
<input placeholder="Password" name="password" type="password">
<input name="login" type="submit" value="Login">
</form>
</body>
</html>
如果我注释掉使用变量$db
的行并取消注释echo命令,则脚本可以正常工作。 php5-fpm日志文件没有给出任何错误消息:
[27-May-2016 10:21:01] NOTICE: Reloading in progress ...
[27-May-2016 10:21:01] NOTICE: reloading: execvp("/usr/sbin/php5-fpm", {"/usr/sbin/php5-fpm", "--fpm-config",$
[27-May-2016 10:21:01] NOTICE: using inherited socket fd=7, "/var/run/php5-fpm.sock"
[27-May-2016 10:21:01] NOTICE: using inherited socket fd=7, "/var/run/php5-fpm.sock"
[27-May-2016 10:21:01] NOTICE: fpm is running, pid 25147
[27-May-2016 10:21:01] NOTICE: ready to handle connections
所以我假设错误位于我的db.php文件中,我在那里进行连接。但是,我无法分辨导致连接失败的原因,并且不知道如何在失败时从die()调用中查看错误消息。这是我的db.php文件的内容:
<?php
$servername = "localhost";
$user = "root"; // also tried with a new user named "yohlo" - same problem
$pass = "password";
$database = "mydatabase";
$db = mysqli_connect($servername, $user, $pass, $database);
if(!$db) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected Successfully";
?>
我正在使用nginx,php5-fpm和mysql安装的raspberry pi2运行该站点。
非常感谢任何帮助。