PHP:无法设置Cookie [英] PHP: Can't Set Cookie
问题描述
由于某种原因,我似乎无法在我的一个PHP文件中设置cookie.除了拒绝设置cookie外,所有代码都可以正常工作.我用不同的参数放置了不同版本的cookie设置,但这似乎没有什么不同.最重要的是,我可以使用同一行代码在同一目录中的单独PHP文件中设置Cookie.我尝试将setcookie()放在不同的位置,但仍然得到相同的结果.我想念什么吗?
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 />";
}
?>
编辑:这是整个文件的所有代码.
This is ALL the code for the entire file.
推荐答案
根据php参考,使用 setcookie
函数的正确方法是
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 ]]]]]] )
您不是交换了$ path和$ domain参数吗?
Didn't you swaped the $path and $domain argument?
尝试
setcookie("remember", $name, $time, '/', 'www.domain.com');
这篇关于PHP:无法设置Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!