dongren4758 2014-08-06 16:58 采纳率: 0%
浏览 235
已采纳

使用cookie时注销

I am beginner in using php cookies and I am trying to make a simple login and logout form using cookies. everything was good but when I press logout link I can't logout. and to logout I have to delete the cookies from the browser.

log_in page

<?php
session_start();

if (isset($_COOKIE["Email"])){
    header("location: home.php");    
}
?>


<form method="post" action="log_in.php">   
<font size="6">Sign In</font>

Email Address: </b></font><input type="text" name="Email" id="email"  />  

password:  <input type="password" name="password" id="password"  />    

<input type="checkbox" name="rememberMe" value="1" id="check"/> Remember Me

<input type="submit" name="Login" id="sign" value="sign in" >

<?php 
include 'db.php';
if(isset($_POST['Login'])){
    $user_email = $_POST['Email'];  
    $password = $_POST['password'];

    $check_user = "SELECT * FROM user where user_email  = '$user_email' AND user_pass = '$password'";
    $run = mysql_query($check_user );

    if (mysql_num_rows($run) > 0){
        $_SESSION['Email']= $user_email;   

        $_SESSION['start'] = time(); 
        if(isset($_POST['rememberMe'])){
            $expire=time()+120;
            setcookie("Email", "Email", $expire);
        }
    else{
        $expire=time()+30;
        setcookie("Email", "Email", $expire);
    }
    echo "<script>window.open('home.php','_self')</script>";   
}
else {
    echo "<script>alert('email or password incorrect!')</script>";     
}}
?>

home page

<?php
if (isset($_COOKIE["Email"])){
  echo "Welcome " . $_COOKIE["Email"] . "!<br>";
    echo '<a href="logoutForm.php">logout</a>';
}
else{
    $now = time(); // Checking the time now when home page starts.
    if ($now > $expire) {
        session_destroy();
        header("location: log_in.php");    
}}

logout page

 <?php
 session_start();
 unset($_SESSION['Email']);
 session_destroy();
 header("Location: log_in.php");

 if(isset($_SESSION['Email'])):
     setcookie($_SESSION['Email'],'',time()-7000000,'/');
 endif;
 ?>
  • 写回答

2条回答 默认 最新

  • dongzha0813 2014-08-06 17:30
    关注

    Your home page (code) doesn't have session_start(); least not in what you posted; it's required when using session_destroy(); it doesn't work on its own.

    Give this a go:

    Sidenote: $expire is undefined for home page code, so you will need to use the same or similar method as you used for the other pages.

    <?php
    if (isset($_COOKIE["Email"])){
      echo "Welcome " . $_COOKIE["Email"] . "!<br>";
        echo '<a href="logoutForm.php">logout</a>';
    }
    else{
        $now = time(); // Checking the time now when home page starts.
        if ($now > $expire) { // $expire is undefined
            session_start(); // <= required
            session_destroy(); // <= does not work on its own
            header("location: log_in.php");    
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥50 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗