警告:setcookie()期望参数3很长,

时间:2011-11-05 02:13:32

标签: php cookies

我正在尝试这段代码:

setcookie('email', $first_email, 'hash', $this->hash, 'logout', $this->log, time()+60*60*24*100);

但我得到了:Warning: setcookie() expects parameter 3 to be long,

我希望setcookie有多个变量。怎么办?也许是阵列?

然后我会有这样的东西,与数据库中的值进行比较

if(empty($_COOKIE['email']) || empty($_COOKIE['hash']) || empty($_COOKIE['logout'])) {
    //something
}
else
//db

2 个答案:

答案 0 :(得分:3)

您无法在一个函数调用中分配3个不同的Cookie。您需要单独设置每个cookie,如下所示:

setcookie('email', $first_email, time()+60*60*24*100);
setcookie('hash', $this->hash, time()+60*60*24*100);
setcookie('logout', $this->log, time()+60*60*24*100);

答案 1 :(得分:1)

您可以设置Cookie数组。这来自http://php.net/manual/en/function.setcookie.php

手册
<?php
// set the cookies
setcookie("cookie[three]", "cookiethree");
setcookie("cookie[two]", "cookietwo");
setcookie("cookie[one]", "cookieone");

// after the page reloads, print them out
if (isset($_COOKIE['cookie'])) {
    foreach ($_COOKIE['cookie'] as $name => $value) {
        $name = htmlspecialchars($name);
        $value = htmlspecialchars($value);
        echo "$name : $value <br />\n";
    }
}
?>