无法设置CodeIgniter Cookie,但会话正常工作吗? [英] CodeIgniter Cookie won't set but Session is working?
问题描述
我正在与CodeIgniter一起使用Twitter和Facebook设置一些用户登录功能到我的网站,这部分工作正常,并且会话工作正常。
I'm working with CodeIgniter to set up some user login functions to my site using Twitter and Facebook, this parts working and the session works fine.
当我尝试设置一个无效的cookie
When I try to set a cookie it doesn't work
//Setup a guid
$guid = uniqid();
//Setup a random cookie and point it to db user
$cookie = array(
'name' => 'TheCookieName',
'value' => $guid,
'expire' => 100,
'domain' => BASE_URL,
'secure' => TRUE
);
set_cookie($cookie);
var_dump(get_cookie('TheCookieName')); // bool(false)
我的自动加载文件非常简单
My autoload file is simple enough
$autoload['helper'] = array('paging_helper','url','cookie');
我显然缺少一些琐碎的东西了吗?有任何线索吗?
I'm obviously missing something trivial? Any clue?
谢谢
推荐答案
问题可能出在您的内部域变量
BASE_URL不是CI常量的一部分,可能不包含您期望的内容或cookie初始化所需的内容。
The problem is probably within your domain variable BASE_URL, which isn't a part of CI constants, probably doesn't contain what you expect or what a cookie initialization requires.
尝试做像这样:
//Setup a guid
$guid = uniqid();
//Setup a random cookie and point it to db user
$cookie = array(
'name' => 'TheCookieName',
'value' => $guid,
'expire' => 86500, // have a high cookie time till you make sure you actually set the cookie
'domain' => '.example.org', // the first . to make sure subdomains isn't a problem
'path' => '/',
'secure' => TRUE
);
set_cookie($cookie);
请记住,在发出新请求之前,cookie永远不可用。
重定向到另一个Cookie设置中指定的域上的页面,然后再次检查Cookie。
Remember that cookies will never be available until a new request have been made.
Redirect to another page on the domain specified in the cookie setup and check for the cookie again.
这篇关于无法设置CodeIgniter Cookie,但会话正常工作吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!