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 多电路系统共用电源的串扰问题
  • ¥15 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料