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.

图片转代码服务由CSDN问答提供 功能建议

出于某种原因,我似乎无法在我的一个PHP文件中设置cookie。 所有代码都可以正常工作,但它拒绝设置cookie。 我已经使用不同的参数设置了不同版本的cookie设置,但它似乎没有什么区别。 最重要的是,我可以在同一目录中的单独PHP文件中使用相同的代码行设置cookie。 我尝试在不同的地方放置setcookie(),我仍然得到相同的结果。 我错过了什么?

 &lt;?php 
 $ table_name =“lfgs”; 
 $ name = $ _ POST ['name']; 
 $ event =  “[”; 
 $的级别= $ _ POST [ '电平']; 
 $的注释= $ _ POST [ '评论']; \ N $小时= $ _ POST [ '小时'] * 60 * 60; 
 $的 分钟= $ _ POST ['分钟'] * 60; 
 $ time = $ hours + $ minutes + time(); 
 
setcookie(“记住”,$ name,$ time,'www.domain.com','  /'); 
 
if(isset($ _ POST ['event'])){
 if(is_array($ _ POST ['event'])){
 foreach($ _ POST ['event'] as $ 值){
 $ event = $ event。  “\”“。​​$ value。”\“,”; 
} 
}其他{
 $ 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”,  “数据库”); 
 
 $ req =“INSERT INTO $ table_name(名称,事件,级别,评论,时间)VALUES('$ name','$ event','$ level','$ comments',  '$ time')“; 
mysqli_query($ con,$ req); 
 
mysqli_close($ con); 
 
foreach($ _ COOKIE as $ c){
 echo $ c。  “&lt; br /&gt;”; 
} 
?&gt; 
   
 
 

编辑:这是所有代码 整个文件。

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

5条回答 默认 最新

相关推荐 更多相似问题