cookie未设置。setcookie()结构在我看来是正确的,但由于某种原因,我无法使它工作。下面是我使用的表单:
if(isset($_POST['LoginBtn']))
{
$username = mysql_escape_string($_POST['username']);
$password = mysql_escape_string($_POST['password']);
if($_POST['remember_me']==1){
$twoDays = 60 * 60 * 24 * 300 + time();
setcookie('UserLogin', $username, $twoDays);
setcookie('UserPass', $password, $twoDays);
}
echo '<script>window.location="index.php"</script>';
}
?>
<html>
<head>
<title>Log In</title>
</head>
<body>
<form method="post" action="login2.php">
<input type="text" name="username" value="<?php echo $_COOKIE['UserLogin'];?>" />
<input type="password" name="password" value="<?php echo $_COOKIE['UserPass'];?>" />
<input type="checkbox" name="remember_me" value="1"/> Remember Me
<input type="hidden" name="LoginBtn" value="1" />
<button type="submit">Login Now</button>
</form>
这里是索引页,我试图响应cookie:
<?php
echo "<br />cookie user_name: ".$_COOKIE['UserLogin'];
echo "<br />cookie user_pass: ".$_COOKIE['UserPass'];
echo "<br /><a href='login2.php'>Back</a>";
?>
只是一个小小的改变
$twoDays = 60 * 60 * 24 * 30 + time();
把时间用在类似的事情上
$twoDay = time() + (86400 * 30), "/"); // 86400 = 1 day
cookie未设置,因为过期时间错误
正如我上面提到的,当我错误地设置cookie时间时,我得到了一个500错误,甚至在提交表单之前。因此,我将表单动作更改为单独的页面(login_ac.php),它工作了。
//表单动作:
<?php
$username = mysql_escape_string($_POST['username']);
$password = mysql_escape_string($_POST['password']);
if($_POST['remember_me']==1){
$twoDays = 60 * 60 * 24 * 30 + time();
setcookie('UserLogin', $username, $twoDays);
setcookie('UserPass', $password, $twoDays);
}
echo '<script>window.location="index.php"</script>';
?>