PHP删除PHP类中的cookie


PHP to delete cookie in php class

我即将控制我的页面内容。因此我需要添加或删除会话/cookie。我制作了一个运行良好的类,但在cookie问题上,它运行不好。我在windows7中查看了firefox18和ubuntu 12.04 LTS。cookie不会使用删除

        setcookie(name, '', time()-9600) 

        foreach($this->_ck as $cookie)
        {
        $hrs=0;
            if($plus)
            {
        $hrs=3600*$cfg_cookie_time;
            }
            //setcookie('testcookie13', '', time()-3600*6);
        header("Set-Cookie: ".$cookie."=deleted; expires=Sun, 01-Jul-2012 08:59:12 GMT;");
        }

等等。。。

我的课是:

<?php
class headers{
    var $new;
    var $vars;
    var $ss;
    var $ck;
    var $_ss;
    var $_ck;
    var $error;
    var $catchs;
    function _construct()
    {
        $this->new=false;
        $this->error=false;
        $this->ss=array();
        $this->ck=array();
        $this->_ss=array();
        $this->_ck=array();
        $this->catchs=true;
    return $this->catchs;
    } //f
    function headers($hs = array(
    "set" => array(
        "ss" => array(),
        "ck" => array()
        )
    ))
    {
    if(isset($hs['send']))
    {
    $this->new=$hs['send'];
    $this->catchs=true;
    }
    if(is_array($hs['set']))
    {
        if(is_array($hs['set']['ss']))
        {
        $this->ss = $hs['set']['ss'];
        }
        if(is_array($hs['set']['ck']))
        {
        $this->ck = $hs['set']['ck'];
        }
    }
    if(is_array($hs['unset']))
    {
        if($hs['unset']['ss'])
        {
        $this->_ss = $hs['unset']['ss'];
        }
        if(is_array($hs['unset']['ck']))
        {
        $this->_ck = $hs['unset']['ck'];
        }
    }
    return $this->catchs;
    } //f
    function send(
    $cfg_cookie_time=6,
    $plus=true
    )
    {
    $cookie='';
        if(is_array($this->ss))
        {
            session_start();
            foreach($this->ss as $session){
            $_SESSION['session'] = $session;
            }
        }
        if($this->_ck)
        {
foreach ($_COOKIE as $name => $value) {
    setcookie($name, '', 1);
}
        }
        if($this->ck)
        {
            foreach($this->ck as $cookie => $val)
            {
            //$this->ck=$cookie.$val;
            $hrs=0;
                if($plus)
                {
            $hrs=3600*$cfg_cookie_time;
                }
            header("Set-Cookie: ".$cookie."=".$val."; path=/; domain=".$_SERVER['HTTP_HOST']."; expires=".gmstrftime("%A, %d-%b-%Y %H:%M:%S GMT;", time()+$hrs));
            }
        }
        if($this->new)
        {
        header("location: ".$this->new);
        $this->catchs=false;
        }
        header("X-Powered-By: PHP ".phpversion()."/FxPHP");
        //header("HTTP/1.0 404 Not Found");
    return $this->ck;
    } //f
} // class
            setcookie('H',  '',  -3600);
/*$hr = new HEADERS( array
(
    "set" => array
    (
        "ck"=> array(),
        "ss"=> array() 
    ),
    "unset" => array
    (
        "ck"=> array
        (
        "H" => "H"
        ),
        "ss"=> array() 
    )
)
    );
print_r( $_COOKIE).print_r($hr->send());
/*
             " f" => "" ,
             " sf" => "",
            "my"=> "" ,
print_r(getallheaders());
print_r(setcookie('sd', 'dsds', 3600*6));
*/
?>

你能帮忙吗?

试试看:

<?php

function del_cookie($_cookie = array())
{
    foreach($_cookie as $k => $kv)
        {
        setcookie($k, '', time()-3600);
        }
        return;
}

function add_cookie($_cookie)
{
        foreach($_cookie as $k => $kv)
        {
        setcookie($k, $kv, time()+3600*24*6);
        }
        return;
}

class headers{
    var $new;
    var $vars;
    var $ss;
    var $ck;
    var $_ss;
    var $_ck;
    var $_ak;
    var $_dk;
    var $error;
    var $catchs;
    function _constract()
    {
        $this->new=false;
        $this->error=false;
        $this->ss=array();
        $this->ck=array();
        $this->_ss=array();
        $this->_ck=array();
        $this->catchs=true;             
        $this->_ak = false;             
        $this->_dk = false;
    return $this->catchs;
    } //f
    function headers($hs = array(
    "set" => array(
        "ss" => array(),
        "ck" => array()
        )
    ))
    {
    if(isset($hs['send']))
    {
    $this->new=$hs['send'];
    $this->catchs=true;
    }
    if($hs['set']['ck']['true'])
    {
    $this->ck = $hs['set']['ck'];               
    $this->_ak = true;
    }
        /*if($hs['unset']['ss'])
        {
        $this->_ss = $hs['unset']['ss'];
        }
        */
    if($hs['unset']['ck']['true'])
    {
        $this->_ck = $hs['unset']['ck'];        
        $this->_dk = true;      
    }
    return $this->catchs;
    } //f
    function send(
    $cfg_cookie_time=6,
    $plus=true
    )
    {
        if(is_array($this->ss))
        {
            session_start();
            foreach($this->ss as $session){
            $_SESSION['session'] = $session;
            }
        }
        if($this->_dk)
        {
del_cookie($this->_ck);
        }
        if($this->_ak)
        {
add_cookie($this->ck);
        }
        if($this->new)
        {
        header("location: ".$this->new);
        $this->catchs=false;
        }
        header("X-Powered-By: PHP ".phpversion()."/FxPHP");
        //header("HTTP/1.0 404 Not Found");
    return $this->catchs;
    } //f
} // class
$hr = new HEADERS( array
(
    "set" => array
    (
        "ck"=> array(
        "true" => ""
        ),
        "ss"=> array
        (
        "true" => "") 
    ),
    "unset" => array
    (
        "ck"=> array
        (
        "true" => "YES",
        "Test" => "1"
        ),
        "ss"=> array
        (
        "true" => "") 
    )
)
    );

print_r( $_COOKIE).print_r($hr->send());
/*
             " f" => "" ,
             " sf" => "",
            "my"=> "" ,
print_r(getallheaders());
print_r(setcookie('sd', 'dsds', 3600*6));
*/
?>

我在您的代码中看到的一个错误是

    function _constract()

应该是

    function __construct()
setcookie($cookie_name, null, null);

无论如何都会删除cookie。在任何逻辑验证之前,在脚本开头的纯php中使用它。如果你正在使用这个,而你的cookie没有被删除,那么你要么在使用xml请求,要么你的逻辑有一些问题。祝你好运