0

我即将控制我的页面内容。所以我需要添加或删除会话/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;");
        }

ETC...

我的课是:

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

?>

你能帮忙吗?

4

3 回答 3

0

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

    function _constract()

应该是

    function __construct()
于 2013-07-01T11:08:20.777 回答
0
setcookie($cookie_name, null, null);

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

于 2013-07-01T12:02:46.007 回答
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));
*/

?>
于 2013-07-01T12:32:52.800 回答