我已经尝试了2个小时在我的页面上设置cookie并且在网上阅读了很多,我仍然无法找出我做错了什么。 这是我的代码:
<?php
$cookie_name = "user";
$cookie_value = "MyUserName";
$days = 86400*30;
$cookie_time = $days+time();
setcookie($cookie_name, $cookie_value, $cookie_time, '/');
if (!isset($_COOKIE[$cookie_name]))
{
echo "Cookie named '" . $cookie_name . "' is not set!";
}
else
{
echo "Cookie '" . $cookie_name . "' is set <br/>";
echo "Value is: " . $_COOKIE[$cookie_name];
?>
我的Echo的结果是“未命名Cookie'用户'!”每次! 有人能帮助我吗? :)
答案 0 :(得分:2)
你的代码中有语法错误你错过了colse the else statment
试试这个,然后你的代码将设置cookie
$cookie_name = "user";
$cookie_value = "MyUserName";
$days = 86400*30;
$cookie_time = $days+time();
setcookie($cookie_name, $cookie_value, $cookie_time, '/');
if (!isset($_COOKIE[$cookie_name]))
{
echo "Cookie named '" . $cookie_name . "' is not set!";
}
else
{
echo "Cookie '" . $cookie_name . "' is set <br/>";
echo "Value is: " . $_COOKIE[$cookie_name];
}
答案 1 :(得分:0)
您忘记关闭else
声明:
else
{
echo "Cookie '" . $cookie_name . "' is set <br/>";
echo "Value is: " . $_COOKIE[$cookie_name];
提示:您应该通过将此代码添加到PHP文件的顶部来启用Error Reporting,这将有助于您查找错误。
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);