duande3134 2013-09-01 09:30
浏览 129
已采纳

PHP:无法设置Cookie

For some reason, I can't seem to set a cookie in one of my PHP files. All of the code works fine, except it refuses to set the cookie. I've placed different versions of cookie setting with different arguments, but it doesn't seem to make a difference. On top of that, I can set a cookie using that same line of code in a separate PHP file in the same directory. I've tried placing setcookie() at different places and I still get the same result. Am I missing something?

<?php
$table_name="lfgs";
$name=$_POST['name'];
$event="[";
$level=$_POST['level'];
$comments=$_POST['comments'];
$hours=$_POST['hours']*60*60;
$minutes=$_POST['minutes']*60;
$time=$hours+$minutes+time();

setcookie("remember", $name, $time, 'www.domain.com', '/');

if(isset($_POST['event'])){
    if (is_array($_POST['event'])) {
        foreach($_POST['event'] as $value){
            $event = $event . "\"" . $value . "\",";
        }
    } else {
        $value = $_POST['event'];
        $event = $event . "\"" . $value . "\"]";
    }
} else {
    $event = "";
}

if($event[strlen($event)-1] == ',') {
    $event = substr_replace($event ,"]",-1);
}

$con=mysqli_connect("domain.com","username","password","database");

$req="INSERT INTO $table_name(name, event, level, comments, time) VALUES ('$name', '$event', '$level', '$comments', '$time')";
mysqli_query($con,$req);

mysqli_close($con);

foreach($_COOKIE as $c) {
    echo $c . "<br />";
}
?>

Edit: This is ALL the code for the entire file.

  • 写回答

5条回答 默认 最新

  • dongmu1989 2013-09-01 09:46
    关注

    According to the php reference, the correct way to use the setcookie function is

    bool setcookie ( string $name [, string $value [, int $expire = 0 [, string $path [, string $domain [, bool $secure = false [, bool $httponly = false ]]]]]] )
    

    Didn't you swaped the $path and $domain argument?

    Try

    setcookie("remember", $name, $time, '/', 'www.domain.com');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事: