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.
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.
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.
use cookie("name", "value", time)
time is optional, second
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);
}
}
?>
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