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

报告相同问题?

悬赏问题

  • ¥200 相机拍直接转存到电脑上 立拍立穿无线局域网传
  • ¥15 (关键词-电路设计)
  • ¥15 如何解决MIPS计算是否溢出
  • ¥15 vue中我代理了iframe,iframe却走的是路由,没有显示该显示的网站,这个该如何处理
  • ¥15 操作系统相关算法中while();的含义
  • ¥15 CNVcaller安装后无法找到文件
  • ¥15 visual studio2022中文乱码无法解决
  • ¥15 关于华为5g模块mh5000-31接线问题
  • ¥15 keil L6007U报错
  • ¥15 webapi 发布到iis后无法访问