0

setcookie doesn't work when I use with thinkphp framework. just $a = setcookie('a','a'); no path no domain. var_dump($a) returns bool(false); no output before this, and no warning.

but when I didn't use the framework , it works.

Álvaro González
  • 142,137
  • 41
  • 261
  • 360

4 Answers4

3

The documentation for setcookie() says that "If output exists prior to calling this function, setcookie() will fail and return FALSE." So be sure to put setcookie() before any statements that might write to STDOUT.

When this happens, PHP will log a warning in your error log: "PHP Warning: Cannot modify header information - headers already sent by..." So check your error log file.

Trott
  • 66,479
  • 23
  • 173
  • 212
  • but it doesn't warn anything. i think it would make some warning if anything output before setcookie. thank you. – nicholas.chan Dec 06 '11 at 15:54
  • 1
    It will log a warning in your error log: "PHP Warning: Cannot modify header information - headers already sent by..." Check your error log file. – Trott Dec 06 '11 at 17:04
0

use cookie("name", "value", time)

time is optional, second

user2538840
  • 47
  • 1
  • 1
  • 8
0

You must set cookies before sending any data. Before any echo and so on. So framework maybe have its own cookie handler. Check its documentation.

Or you can use extension:

<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2009 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
// $Id$

/**
 +------------------------------------------------------------------------------
 * Cookie管理类
 +------------------------------------------------------------------------------
 * @category   Think
 * @package  Think
 * @subpackage  Util
 * @author    liu21st <liu21st@gmail.com>
 * @version   $Id$
 +------------------------------------------------------------------------------
 */
class Cookie extends Think
{
    static function is_set($name) {
        return isset($_COOKIE[C('COOKIE_PREFIX').$name]);
    }

    static function get($name) {
        $value   = $_COOKIE[C('COOKIE_PREFIX').$name];
        $value   =  unserialize(base64_decode($value));
        return $value;
    }

    static function set($name,$value,$expire='',$path='',$domain='') {
        if($expire=='') {
            $expire =   C('COOKIE_EXPIRE');
        }
        if(empty($path)) {
            $path = C('COOKIE_PATH');
        }
        if(empty($domain)) {
            $domain =   C('COOKIE_DOMAIN');
        }
        $expire =   !empty($expire)?    time()+$expire   :  0;
        $value   =  base64_encode(serialize($value));
        setcookie(C('COOKIE_PREFIX').$name, $value,$expire,$path,$domain);
        $_COOKIE[C('COOKIE_PREFIX').$name]  =   $value;
    }

    static function delete($name) {
        Cookie::set($name,'',time()-3600);
        unset($_COOKIE[C('COOKIE_PREFIX').$name]);
    }

    static function clear() {
        unset($_COOKIE);
    }
}
?>
Ernestas Stankevičius
  • 2,420
  • 2
  • 24
  • 30
  • Note that the `Cookie` object still is just using `setcookie()` under the hood (see the `set()` method), so you still need to make sure that you don't send any output before setting the cookie. – Trott Dec 06 '11 at 15:36
-3

cuz,thinkphp rewrite a function cookie,if you want to use cookie with thinkphp.you should write cookie(name,value),no more setcookie!

from China QQ:1720036678

kevin
  • 1