問題描述
我進行了一個登錄,它設置了一個 cookie 與推算的電子郵件地址的值,因此在 global.php 文件中,它使用以下方法存儲用戶數據的數組:
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:
$email = $_COOKIE["PeopleHub"];
$getuserdata = mysqli_query($con, "SELECT * FROM Earth WHERE email='$email'");
$userdata = mysqli_fetch_array($getuserdata, MYSQLI_ASSOC);
沒有設置cookie,我知道這是因為我做了一個測試文件:
The cookie isn't being set, I know this because I made a test file:
echo $_COOKIE["PeopleHub"];
它只是做了一個空白頁.
It just made a blank page.
登錄代碼(設置cookie的地方):
The login code (where the cookie is set):
<?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>
推薦答案
您必須在發送任何標題之前設置 cookie.
You have to set cookies before any headers are sent out.
來自手冊:
setcookie() 定義了一個與其他 HTTP 標頭一起發送的 cookie.與其他標頭一樣,cookie 必須在腳本的任何輸出之前發送(這是協議限制).這要求您在任何輸出之前調用此函數,包括和標簽以及任何空格.
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.
這意味著您需要研究輸出緩沖如果您希望按原樣使用此代碼.
This means you will need to look into output buffering if you wish to use this code as is.
<?php
ob_start();
echo "Hello
";
setcookie("cookiename", "cookiedata");
ob_end_flush();
?>
根據global.php
的內容,這可能適合您.我所做的只是在調用 setcookie()
之前刪除任何輸出.如果 global.php
包含任何空格或 HTML 輸出,這將不起作用:
Depending on the contents of global.php
, this might work for you. All I did was remove any output before setcookie()
is called. If global.php
contains any whitespace or HTML output in it this won't work:
<?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>
這篇關于PHP - setcookie();不工作的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!