PHP在php类中删除cookie

时间:2013-07-01 11:02:03

标签: php cookies header

我即将控制我的网页内容。所以我需要添加或删除会话/ cookie。我制作了一个工作正常的课程,但在cookie问题上,它不能正常工作。我在windows7中使用firefox 18和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));
*/

?>

你能帮忙吗?

3 个答案:

答案 0 :(得分:0)

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

    function _constract()

应该是

    function __construct()

答案 1 :(得分:0)

setcookie($cookie_name, null, null);

无论如何都会删除cookie。在任何逻辑验证之前,在脚本开头的普通php中使用它。如果您正在使用此cookie并且您的cookie未被删除,那么您要么使用xml请求,要么您的逻辑中存在一些问题。祝你好运!

答案 2 :(得分:0)

试一试:

<?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));
*/

?>