PHP - setcookie();不工作

时间:2023-01-17
本文介绍了PHP - setcookie();不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我进行了一个登录,它设置了一个 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模板网!

上一篇:设置 cookie 永不过期 下一篇:在 cookie 中存储 PHP 数组

相关文章

最新文章