我一直在尝试使用PHP和MySQL设置登录页面,当我使用带有 - >的预准备语句时,我遇到了一个问题。运营商。我正确安装了php5和apache2,phpinfo();
运行没有问题,但是当我使用这段代码时:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Login</title>
</head>
<body>
<form action="" method="POST">
<label for="username">Username:</label>
<br />
<input type="text" name="username" id="username" />
<br />
<label for="password">Password:</label>
<br />
<input type="password" name="password" id="password" />
<br />
<input type="submit" value="Submit" />
</form>
<?php
error_reporting(-1);
ini_set('display_errors', 'On');
if (isset($_POST['username']) && isset($_POST['password'])) {
$logged_in = false;
$username = $_POST['username'];
$password = $_POST['password'];
$conn = new mysqli('localhost', 'root', 'password', 'db');
/* create a prepared statement */
if ($stmt = $conn->prepare("SELECT username, password, salt FROM `users` WHERE `username` = ?")) {
/* bind parameters for markers */
$stmt->bind_param("s", $username);
/* execute query */
$stmt->execute();
$stmt->bind_result($storedUser, $storedPassword, $storedSalt);
while ($stmt->fetch()) {
// $storedUser, $storedPassword, $storedSalt
if (crypt($password, $storedSalt) == $storedPassword) {
$logged_in = true;
}
}
/* close statement */
$stmt->close();
if ($logged_in === true) {
echo 'Successfully logged in as ' . $username;
}
}
}
?>
</body>
</html>
php打破&gt;其余部分显示在页面上。
prepare("SELECT username, password, salt FROM `users` WHERE `username` = ?")) { /* bind parameters for markers */ $stmt->bind_param("s", $username); /* execute query */ $stmt->execute(); $stmt->bind_result($storedUser, $storedPassword, $storedSalt); while ($stmt->fetch()) { // $storedUser, $storedPassword, $storedSalt if (crypt($password, $storedSalt) == $storedPassword) { $logged_in = true; } } /* close statement */ $stmt->close(); if ($logged_in === true) { echo 'Successfully logged in as ' . $username; } } } ?>
答案 0 :(得分:0)
虽然我仍然不确定导致此问题的原因,但我通过运行来修复它:
sudo /etc/init.d/apache2 stop
sudo apt-get remove --purge apache2 php5
sudo apt-get remove --purge libapache2-mod-php5
sudo apt-get install php5 apache2 libapache2-mod-php5
sudo /etc/init.d/apache2 start
sudo a2enmod php5
sudo /etc/init.d/apache2 force-reload
感谢大家的帮助。