简体   繁体   English

未使用 PHP setcookie 函数设置 Cookie

[英]Cookies not set using PHP setcookie function

I'm using the setcookie php function to set some cookies in my browser.我正在使用setcookie php 函数在我的浏览器中设置一些 cookie。 I try to set cookies in my php code and then I check it using print_r($_COOKIE) .我尝试在我的 php 代码中设置 cookie,然后我使用print_r($_COOKIE)检查它。 Cookies are not displayed, however if I try to set cookie in another file they will be displayed correctly. Cookie 不会显示,但是如果我尝试在另一个文件中设置 cookie,它们将正确显示。

if (isset($_POST['username']) && isset($_POST['password']))
{
    global $username,$password;
    $username = $_POST['username'];
    $password = sha1($_POST['password']);
    setcookie('username', $username, time()+3600); //cookie not set
    setcookie('password', $password, time()+3600); //cookie not set
    $database = connect_to_database($db_path);
    $result = $database->query("SELECT * FROM users WHERE username = '$username' AND password = '$password'");
    while (true)
    {
        $response = $result->fetchArray(SQLITE3_ASSOC);
        if (!$response)
        {
            unset($_COOKIE['username']);
            unset($_COOKIE['password']);
            break;
        }
        if (($response['username'] == $username) && $response['password'] == $password)
        {
            header("location: ../index.php");
        }
    }

}

I expected cookies to be set, but using print_r($_COOKIE);我希望设置 cookie,但使用print_r($_COOKIE); returns me array()返回我array()

Notes笔记

  • Cookies are allowd in my browser settings我的浏览器设置中允许使用 Cookie
  • $_POST['username'] & $_POST['password'] sent to this page via form in another page $_POST['username'] & $_POST['password']通过另一个页面的表单发送到这个页面
  • $_POST['username'] & $_POST['password'] are set with true value. $_POST['username'] & $_POST['password']设置为真值。

Make sure you have a domain that is known by both server and client.确保您有一个服务器和客户端都知道的域。 echo $_SERVER['HTTP_HOST'] should tell you the exact same domain that your browser has. echo $_SERVER['HTTP_HOST']应该告诉您浏览器拥有的完全相同的域。 If not, cookie will not be accepted by the browser.否则,浏览器将不会接受cookie。

Make sure your server and client time is perfectly correct.确保您的服务器和客户端时间完全正确。 Browser will reject a cookie with a wrong datetime.浏览器将拒绝日期时间错误的 cookie。

Do not write any other code and just do:不要编写任何其他代码,只需执行以下操作:

<?php
$cookie_name = "user";
$cookie_value = "John Doe";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day 
// expiration
echo date("H:i:s d.m.Y")."<br>";
echo $_SERVER['HTTP_HOST']."<br>";
var_dump($_COOKIE);
?>

and refresh the page twice.并刷新页面两次。

Also check out manual at: https://www.php.net/manual/en/features.cookies.php另请查看手册: https : //www.php.net/manual/en/features.cookies.php

use ob_start() and ob_end_flush() in your code .在您的代码中使用ob_start() and ob_end_flush()

Check out manual for ob_start() ob_end_flush()查看 ob_start() ob_end_flush()手册

` `

ob_start();
setcookie("name", "data");
ob_end_flush();

` `

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM