setcookie()无法使用登录表单

时间:2015-10-07 04:17:32

标签: php cookies

未设置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>";
?>

3 个答案:

答案 0 :(得分:1)

只是一个小小的改变

$twoDays = 60 * 60 * 24 * 30 + time();

答案 1 :(得分:0)

将时间用于similer to this

$twoDay = time() + (86400 * 30), "/"); // 86400 = 1 day

由于错误的到期时间而未设置您的Cookie

答案 2 :(得分:0)

正如我上面提到的,当我错误地设置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>';
        ?>
相关问题