我通过遵循各种教程和在线阅读文章从头开始创建了一个登录/注册系统。该系统工作,但有一个我不明白的错误。当用户第一次尝试登录时,它返回一个找不到帐户的错误,但是如果用户再次尝试登录,那么它会继续登录。我测试了它是否存储了会话 cookie,结果是它没有(至少不是第一次)。下次用户尝试登录时,它会正确存储 cookie。
这是我登录脚本的第一部分,它检查输入的验证码是否正确,然后设置会话 cookie 并将用户重定向到检查用户是否存在的登录页面脚本。
<?php
session_start();
$mode = $_GET['mode'];
if($mode == 'login')
{
require_once('recaptchalib.php');
$privatekey = "---";
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
if (!$resp->is_valid) {
// What happens when the CAPTCHA was entered incorrectly
header('Location: http://cpalander.net/login.php?option=captcha');
} else {
$user = $_POST['username2'];
$pass = $_POST['password3'];
$_SESSION['pass3'] = $pass;
$_SESSION['user3'] = $user;
header('Location: http://cpalander.net/login.php?option=checkuser');
}
die();
}
else if($mode == 'sendticket')
{
require_once('recaptchalib.php');
$privatekey = "---";
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
if (!$resp->is_valid) {
// What happens when the CAPTCHA was entered incorrectly
header('Location: http://cpalander.net/dashboard.php?option=sendticket&error=captcha');
} else {
$subject = $_POST['subject'];
$message = $_POST['message'];
header('Location: http://cpalander.net/dashboard.php?option=sendticket&subject=' . urlencode($subject) . '&message=' . urlencode($message));
}
die();
}
else
{
require_once('recaptchalib.php');
$privatekey = "---";
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
if (!$resp->is_valid) {
// What happens when the CAPTCHA was entered incorrectly
header('Location: http://cpalander.net/register.php?option=captcha');
} else {
$_SESSION['user2'] = $_POST['username'];
$_SESSION['pass2'] = $_POST['password'];
$_SESSION['mail2'] = $_POST['email'];
header('Location: http://cpalander.net/makeacc.php');
}
die();
}
?>
这是检查用户是否存在并在出现错误时重定向用户的代码部分:
<?php session_start();
$data = $_GET["option"];
$user = $_SESSION['user3'];
$pass = $_SESSION['pass3'];
function generateRandomString($length = 10)
{
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, strlen($characters) - 1)];
}
return $randomString;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>....</head>...
<body>...
...<?php
if ($data == 'checkuser')
{
$user = $_SESSION['user3'];
$link = new mysqli('127.0.0.1', '*******', '*******', '*******');
if ($link->connect_errno) {
die('Failed to connect to MySQL: (' . $mysqli->connect_errno . ') ' . $mysqli->connect_error);
}
$result = $link->query("SELECT * FROM users WHERE username='$user' AND active=1 AND banned=0");
$numrows = $result->num_rows;
if($numrows == 0)
{
$link->close();
session_destroy();
echo '<META HTTP-EQUIV="Refresh" Content="0; URL=login.php?option=notfound&user=' . $user . '">'; // This is the part that I used to check if the $user variable is set
exit;
}
$row = $result->fetch_assoc();
$sid = $row['salt'];
$pass_h = hash('sha256', $sid . $pass);
$result = $link->query("SELECT * FROM users WHERE username='$user' AND password='$pass_h' AND active=1 AND banned=0");
$numrows = $result->num_rows;
if($numrows == 0)
{
$link->close();
session_destroy();
echo '<META HTTP-EQUIV="Refresh" Content="0; URL=login.php?option=notfound">';
exit;
}
else
{
$link->close();
$_SESSION['user'] = $user;
echo '<META HTTP-EQUIV="Refresh" Content="0; URL=dashboard.php?option=home&user=' . $user . '">';
exit;
}
$link->close();
}...</body>
谁能帮我解决这个问题?另外, session_start(); 位于登录页面代码的顶部。