PHP - setcookie(); 不工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20316870/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP - setcookie(); not working
提问by user2999920
I've made a login, that sets a cookie with a value of the imputed email address, so in the global.php file, it stores an array of the users data using:
我已经进行了登录,它设置了一个 cookie 与推算的电子邮件地址的值,因此在 global.php 文件中,它使用以下方法存储用户数据的数组:
$email = $_COOKIE["PeopleHub"];
$getuserdata = mysqli_query($con, "SELECT * FROM Earth WHERE email='$email'");
$userdata = mysqli_fetch_array($getuserdata, MYSQLI_ASSOC);
The cookie isn't being set, I know this because I made a test file:
cookie 没有被设置,我知道这是因为我做了一个测试文件:
echo $_COOKIE["PeopleHub"];
It just made a blank page.
它只是做了一个空白页。
The login code (where the cookie is set):
登录代码(设置 cookie 的位置):
<?php
include "global.php";
?>
<h2>Login</h2>
<?php
echo "We currently have <b>" . $usercount . "</b> members, <b>" . $onlinecount . "</b> of which are online. ";
?>
<br>
<br>
<?php
if(isset($_POST["email"])){
$email = $_POST["email"];
$password = sha1($_POST["password"]);
$check = mysqli_query($con, "SELECT * FROM Earth WHERE `email`='$email' AND `password`='$password'");
$check = mysqli_num_rows($check);
if($check == 1){
setcookie("PeopleHub", $email, 0, '/');
echo "We logged you in!";
}
else {
echo "We couldn't log you in!";
}
}
?>
<form action="<?php echo $_SERVER['REQUEST_URI']; ?>" method="post">
Email <input name="email" placeholder="Email Address" required="" type="text"><br>
Password <input name="password" placeholder="Password" required="" type="password"><br>
<input type="reset" value="Start Over">
<input type="submit" value="Login">
</form>
回答by John Conde
You have to set cookies beforeany headers are sent out.
您必须在发送任何标头之前设置 cookie 。
From the manual:
从手册:
setcookie() defines a cookie to be sent along with the rest of the HTTP headers. Like other headers, cookies must be sent before any output from your script (this is a protocol restriction). This requires that you place calls to this function prior to any output, including and tags as well as any whitespace.
setcookie() 定义了一个与其余 HTTP 标头一起发送的 cookie。与其他标头一样,cookie 必须在脚本的任何输出之前发送(这是协议限制)。这要求您在任何输出之前调用此函数,包括和标签以及任何空格。
This means you will need to look into output bufferingif you wish to use this code as is.
这意味着如果您希望按原样使用此代码,则需要查看输出缓冲。
<?php
ob_start();
echo "Hello\n";
setcookie("cookiename", "cookiedata");
ob_end_flush();
?>
Depending on the contents of global.php, this mightwork for you. All I did was remove any output before setcookie()is called. If global.phpcontains any whitespace or HTML output in it this won't work:
根据 的内容global.php,这可能对您有用。我所做的只是在setcookie()调用之前删除任何输出。如果global.php其中包含任何空格或 HTML 输出,这将不起作用:
<?php
include "global.php";
if(isset($_POST["email"])){
$email = $_POST["email"];
$password = sha1($_POST["password"]);
$check = mysqli_query($con, "SELECT * FROM Earth WHERE `email`='$email' AND `password`='$password'");
$check = mysqli_num_rows($check);
if($check == 1){
setcookie("PeopleHub", $email, 0, '/');
echo "We logged you in!";
}
else {
echo "We couldn't log you in!";
}
}
?>
<h2>Login</h2>
<?php
echo "We currently have <b>" . $usercount . "</b> members, <b>" . $onlinecount . "</b> of which are online. ";
?>
<br>
<br>
<form action="<?php echo $_SERVER['REQUEST_URI']; ?>" method="post">
Email <input name="email" placeholder="Email Address" required="" type="text"><br>
Password <input name="password" placeholder="Password" required="" type="password"><br>
<input type="reset" value="Start Over">
<input type="submit" value="Login">
</form>
回答by Robert
Just wanted to point out, I had an issue with setcookie not working. When I investigated the file further it was encoded as UTF-8 with BOM. When I re-encoded it as UTF-8 without BOM setcookie worked fine, so the BOM was being written before my first php tag was encountered. I guess enabling buffering in my php.ini file probably would fix this too.
只是想指出,我遇到了 setcookie 不起作用的问题。当我进一步调查该文件时,它被编码为带有 BOM 的 UTF-8。当我将它重新编码为没有 BOM setcookie 的 UTF-8 工作正常时,所以 BOM 是在遇到我的第一个 php 标签之前编写的。我想在我的 php.ini 文件中启用缓冲也可能会解决这个问题。
Someone may eventually find this information helpful.
最终有人可能会发现这些信息很有帮助。
回答by ArturOlszak
I had another problem with cookie update using setcookie function.
我在使用 setcookie 函数更新 cookie 时遇到了另一个问题。
So I've set cookie string made from array using php serialize function. From now on I was not able to update that cookie - setcookie function was simply not working, wheather is was setting serialized string or any other simple string.
所以我已经使用 php 序列化函数设置了由数组制成的 cookie 字符串。从现在开始我无法更新那个 cookie - setcookie 函数根本不起作用,是设置序列化字符串还是任何其他简单的字符串。
Then I've set another cookie with new cookie key, this time data was encoded with json_encode function. This time I was able to set the cookie and update it :-)
然后我用新的 cookie 键设置了另一个 cookie,这次数据是用 json_encode 函数编码的。这次我能够设置cookie并更新它:-)
回答by Vahid Amiri
I had the same problem and it turned out that it was caused because the domain I was passing to the function had a custom port (which is not allowed).
我遇到了同样的问题,结果证明这是因为我传递给函数的域有一个自定义端口(这是不允许的)。

