我在突然停止工作的用户登录时遇到了一些麻烦。我有一个session_start();在每个需要它的页面的顶部,但我声明的会话变量似乎刚刚停止转移到登录页面。
我还可以在登录页面上禁用重定向和print_r($ _ SESSION),这将打印相应的变量。
以下是登录页面的代码:
<? session_start(); ?>
<?php include($_SERVER["DOCUMENT_ROOT"]."/_/includes/head.php"); ?>
<?php
if (isset($_POST['submitted'])){
$username = $_POST['username'];
$password = $_POST['password'];
function enc($string){
$salt = "2@xp";
$hash = sha1(md5($salt.$string)).md5($string).sha1(md5(md5($string)));
return $hash;
}
$pass = enc($password);
if($username&&$password){
$query=mysql_query("SELECT * FROM `users` WHERE `username`='$username'");
$numrows = mysql_num_rows($query);
if ($numrows!=0){
while ($row = mysql_fetch_assoc($query)){
$dbusername = $row['username'];
$dbpassword = $row['password'];
//check to see if they match
if ($username==$dbusername&&$pass==$dbpassword){
echo ("You're in! <a href='index.php'>Home</a>");
$_SESSION['username'] = $dbusername;
session_register("username");
header("Location: index.php");
}
else echo ("Incorrect Password");
}
}
else header("Location: login.php");
}
else echo ('Login Failed');
}
?>
以下是登录时重定向到的页面的代码:
<?php
session_start();
if ($_SESSION['username']){
echo "<section id='login'><a href='logout.php'>Logout</a></section>";
$user = $_SESSION['username'];
}
else header("Location: login.php");
?>
我每次都被踢回登录页面。任何帮助都是极好的。谢谢!