繁体   English   中英

无法设置CodeIgniter Cookie,但会话正常工作吗?

[英]CodeIgniter Cookie won't set but Session is working?

我正在与CodeIgniter一起使用Twitter和Facebook设置一些用户登录功能到我的网站,此部分正常工作,并且会话正常进行。

当我尝试设置Cookie时不起作用

//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)

我的自动加载文件非常简单

$autoload['helper'] = array('paging_helper','url','cookie');

我显然缺少一些琐碎的东西? 有什么线索吗?

谢谢

确保仅对https设置secure => TRUE

问题可能出在您的域变量BASE_URL中,该变量不是CI常量的一部分,可能不包含您期望的内容或cookie初始化所需的内容。

尝试这样做:

//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。

您不能在同一实例中设置和访问Cookie。 您应该在设置Cookie后重定向或刷新。 这就是var_dumping get_cookie总是返回false的原因。 您还应该设置其余参数。 setcookie

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM