[英]Not able to retrieve array from cookie on page load after url rewritting in htaccess using php
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /roots_web/
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteRule ^contact-us.html/$ contact-us.php
RewriteRule ^contact-us.html$ contact-us.php
我使用以下方法设置Cookie:
setcookie("reports_cookie[".$report_id."][report]", $report_id);
当我转到其他页面并检查
if(isset($_COOKIE['reports_cookie']) && count($_COOKIE['reports_cookie'])>0)
{
}
它不认为此条件为真。
确保
setcookie("reports_cookie[".$report_id."][report]", $report_id, time()+3600,"/");
在对浏览器完成任何输出之前使用。
例如
<html>
<head>
<title>
hello world
</title>
</head>
<?php
setcookie("reports_cookie[".$report_id."][report]", $report_id, time()+3600,"/");
?>
</html>
将使其失败,因为html输出将在cookie标头之前发送。
Cookies由所谓的标题字段设置和读取。 这些需要在任何输出之前发送。
即使在打开php标记之前有空格也会使它失败。 即使在调用setcookie函数之前“包含”文件中的空格。
<?php setcookie("reports_cookie[".$report_id."][report]", $report_id, time()+3600,"/");
^-- a space making your cookies fail
如果您没有看到空格,则可能在php文档的开头有一个UTF8 BOM字段。 建议将文本编辑器设置为其他文本编码(例如ANSI),以便能够编辑UTF8 BOM,以便可以将其删除然后保存并恢复为没有BOM的UTF8。
另外,请确保将Cookie设置在整个域中。 如果您没有指定cookie,则只能在其设置页面上读取cookie。
还要记住设置一个过期时间,在这种情况下,我将Cookie设置为5分钟。
setcookie("reports_cookie[".$report_id."][report]", $report_id, time()+3600,"/");
另外,请考虑一下为什么要将报表ID存储为数组键和值...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.