会话在注销时销毁cookie

时间:2015-03-05 10:49:21

标签: php session cookies

我遇到了会话cookie的问题,我试图在会员注销时删除所有cookie,但我似乎无法让它工作。这是我目前在logout.php

中的内容
<?php 
session_start();
if( session_unregister('loginid') == true && session_unregister('username')==true ) {
if (isset($_SERVER['HTTP_COOKIE'])) {
    $cookies = explode(';', $_SERVER['HTTP_COOKIE']);
    foreach($cookies as $cookie) {
        $parts = explode('=', $cookie);
        $name = trim($parts[0]);
        setcookie($name, '', time()-1000);
        setcookie($name, '', time()-1000, '/');
    }
}
    session_destroy();
    header('Location: login.php');
  } else {
   unset($_SESSION['loginid']);
   unset($_SESSION['username']);
   session_destroy();
   header('Location: login.php');
}
?>

如果有人能为我发光,那将是非常感激的。谢谢

2 个答案:

答案 0 :(得分:1)

你可以试试这个

session_start();
 if (isset($_COOKIE['cookie1'])) {
unset($_COOKIE['hi']);
session_destroy();
unset($_COOKIE['test1']);
setcookie('hi', null, -1, '/');
setcookie('test1', null, -1, '/');
header('Location:login.php');
return true;
exit;
} else {
return false;
}

答案 1 :(得分:0)

这是我登录部分的所有代码。

<?
unset($login,$pwrd,$id);
session_start();
session_register("login","pwrd","id");

if($logout==1){
    session_destroy();
    header("Location: ".$PHP_SELF);
}

require('config_inc.php');
require('error_inc.php');


//require('error_inc.php');
//require('config_inc.php');

/*
function auth($log,$pass){
    global $t_user;
    $query = "select id from ".$t_user." where email=\"".$log."\" and pass=\"".$pass."\" ";      
    $result = MYSQL_QUERY($query);
    if(mysql_num_rows($result)>0){
        $id=mysql_result($result,0,"id");
        @mysql_free_result($result);  
        return $id;
    }else{
        @mysql_free_result($result); 
        return 0;
    }
}
*/


if(auth($login,$pwrd)!=0){
    $query = "select * from ".$t_user." where id=".$id." ";      
    $result = MYSQL_QUERY($query);
    $credits=mysql_result($result,0,"credits");
    $email=mysql_result($result,0,"email");
    $flag=true;
    if($REQUEST_METHOD=="POST"){
        if(isset($edit)){
            if($name==""){
                $er[]=$err[3]."'Name'";
                $flag=false;
            }
            $name=htmlspecialchars($name);
            $br=intval($br);
            $share=intval($share);

            for($i=0,$summa=0;$i<sizeof($h);$i++){
                $summa=$summa+$cr[$i];
            }
            if($summa>$credits){
                require('header_inc.php');
                print $err[6]."<br>";
                require('footer_inc.php');
                exit;
            }
            $query="delete from ".$t_idu_idc." where idu=".$id;
            if(!@mysql_query($query)){                
                print $err[9];
            }           
            for($i=1;$i<=3;$i++){
                $query="insert into ".$t_idu_idc." set  idu=".$id.", idc=".intval($sel[$i]);
                if(!@mysql_query($query)){                
                    print($err[9]);
                } 
            } 

            for($i=0;$i<sizeof($h);$i++){
                $query="update ".$t_site." set credits=credits+".intval($cr[$i])." where id=".$h[$i];
                if(!@mysql_query($query)){                
                    print $err[9];
                } 
                $query="update ".$t_user." set credits=credits-".intval($cr[$i])." where id=".$id;
                if(!@mysql_query($query)){                
                    print $err[9];
                } 
            }

            $query="update ".$t_user." set  name=\"".$name."\", share=".$share.", br=".$br;
            if(($pass!="")and($pass1!="")){
                if(($pass=="")and($pass!=htmlspecialchars($pass))){
                    $er[]=$err[5];
                    $flag=false;
                }
                if(($pass1=="")and($pass1!=htmlspecialchars($pass1))){
                    $er[]=$err[5];
                    $flag=false;
                }
                if($pass1!=$pass){
                    $er[]=$err[1];
                    $flag=false;
                }
                $query=$query.",pass=\"".$pass."\" ";
            }
            if($flag==true){
                $query=$query." where id=".$id; 
                if(!@mysql_query($query)){                
                    print $err[9];
                } 

                if(($pass!="")and($pass1!="")){
                    $pwrd=$pass;
                    @mail($email,$subject[3],$body[2].$pass,"From: $admin_mail");
                }
            header("Location: user_menu.php?PHPSESSID=".$PHPSESSID);
            }else{
                require('header_inc.php');
                $str=join("<br>",$er);
                print "<p class=\"error\">".$str."</p><p class=\"smlink\">Please return to <a href=\"javascript: history.back()\">back</a> and check your form.</p>";
                require('footer_inc.php');
                exit;
            }
        }
    }