php cookie没有设置

时间:2014-03-03 14:57:22

标签: php cookies

我在使用PHP设置cookie时遇到问题。

private function setCookie()
{

    if(isset($_COOKIE['billForm']))
    {
        setcookie("billForm", "", time()-3600);

        $cookie = $this->group . ',' . $this->month . ',' . $this->year;
        setcookie("billForm", $cookie, time()+3600);

    } else {

        $cookie = $this->group . ',' . $this->month . ',' . $this->year;
        setcookie("billForm", $cookie, time()+3600);

    }

}

我控制了else语句正在运行。我也验证了$ cookie,试图将它设置为$ cookie ='foo';以及。我查看了两个不同的浏览器chrome和firefox以及不同的cookie浏览器,但没有跟踪cookie。我也试过另一台电脑。

nginx error.log

中也没有

有什么建议吗?

3 个答案:

答案 0 :(得分:0)

您应该尝试设置Cookie的域名

setcookie("billForm", $cookie, time()+3600, 'path_on_server', 'yourdomain.com');

答案 1 :(得分:0)

在我的本地,我得到了与函数名称的冲突,appache说函数setCookie()重新声明setcookie()。解决方法是将其重命名为setMyCookie()
您确定自己的数据$this->group . ',' . $this->month . ',' . $this->year; 吗?
对我而言,它正在工作(但我用硬编码的字符串替换你的数据)

答案 2 :(得分:0)

以下是一些经过测试的代码,根据您的代码,根据需要发送,显示和删除'biilForm'cookie。

运行时环境:PHP 5.3.18,Windows XP上的Apache(XAMPP)为“localhost”。

希望它有所帮助。

我在浏览器中的典型输出:

did cookie "billForm" arrive? 
string 'notset,march,2014, newCookie: 1393862228' (length=40)
cookie "billForm" sent...

代码:

<?php
// Q22149976


/*
 *  I need a test class to run your code...
 */
class CookieTest {

private $group   = 'notset';
private $month   = 'march';
private $year    = 2014;

public function __construct($group = 'notset', $month = 'march', $year = 2014)
{
  $this->group = $group;
  $this->month = $month;
  $this->year  = $year;
}

public function setCookie() // name not important
{
    if(isset($_COOKIE['billForm']))
    {
        setcookie("billForm", "", time()-3600);

        $cookie = $this->group . ',' . $this->month . ',' . $this->year . ', newCookie: '. time();
        setcookie("billForm", $cookie, time()+3600);

    } else {

        $cookie = $this->group . ',' . $this->month . ',' . $this->year;
        setcookie("billForm", $cookie, time()+3600);
    }

}

} // CookieTest end -------------------------------------

echo 'did cookie "billForm" arrive? <br/>';

if (isset($_COOKIE['billForm'])) {
   var_dump($_COOKIE['billForm']); // you can see what comes in!
}
else {
  echo '<br />billform Cookie: IS MISSING!<br />';
}

/*
 * send the cookie...
 */

$cookieTest = new CookieTest();

if (isset($_POST['delcookie'])) {
    setcookie("billForm", "", time() - 24 * 60 * 60);
    echo 'cookie "billForm" deleted...<br />';
}
else {
    $cookieTest->setCookie();
    echo 'cookie "billForm" sent...<br />';
}

?>
<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Q22149976 - Cookie issues</title>
  </head>

  <body>
    <div class="main" id="main">

        <strong><?php echo 'Cookie testing..'?></strong><br/>

        <form method="POST" action="">
          delete cookie: <input type ="checkbox" name="delcookie" value="delcookie">

          <br /><input type="submit" value="GO"/>
        </form>
    </div>
  </body>
</html>