PHP Cookies not working

跟風遠走 提交于 2020-01-04 14:11:25

问题


I am building a website with logins. I have sessions working fine but I need to be able to keep them logged in if the click remember me. Currently I have the login script saving a cookie with the Username and Password they type to some cookies. I the below script $username and $password are set higher in the script. The $_SESSION variables are getting set fine. And I know the script is going into the if statement because before I place a alert box in there.

login.php

while($row = mysql_fetch_array($getLoginResults))
{
    $error = "Login Successful";
    $_SESSION['Username'] = $_POST['username'];
    $_SESSION['Password'] = $_POST['password'];
    if($keep == 1)
    {
        setcookie("Username", $username, time()+3600*24*30);
        setcookie("Password", $password, time()+3600*24*30);
    }
}

When I check the login, I have a javascript alert so I knoe the cookie is set but the alert box is coming up empty.

check_login.php

echo "<script>alert('".$_COOKIE['Username']."')</script>";

What am I missing???


回答1:


Try using setcookie with a a path specified, this used to catch me out, as it assumes the current path by default. Using / will make the cookie work for the whole domain

setcookie("Username", $username, time()+3600*24*30, '/');



回答2:


You are writing a script code with PHP. PHP can only know the data that is sent with the request.

If you first set a cookie and then fetch data from $_COOKIE, the data is not yet there. First the browser must accept the cookie, and then send it back on the next request.

If you use Javascript instead and fetch the documents cookies from the browser, it would work.

And now the disclaimer

Never ever store the login credentials (username and password) as plain text in permanent cookies!

If done right, you create a long random string from a cryptographically secure random number generator (that means you do NOT use rand() or mt_rand()), store it in the cookie, and also in the database. This random string now is a replacement for username and password when it comes to checking credentials.




回答3:


Seems like $keep came from nowhere. If you used a checkbox for $keep variable, you can do the following.

<?
$keep = isset($_POST['keep']);
if ($keep) {
    .... // cookie set up
}
?>



回答4:


Its better to use Jquery/Javascript to set cookie in browser. I find it easier in jquery than PHP.

Create, read, and erase cookies with jQuery




回答5:


See How cookie works

index.php

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>Himel Sarkar</title>
        <link rel="stylesheet" href="">
    </head>
    <body>

    <?php 


    setcookie("username","Himel Sarkar",time()+100);




    echo "This cookie is valied for 100 sec ";

     ?>
    <a href="welcome.php">Next Page</a>
    </body>
    </html> 

welcome.php

    <?php 

    echo $_COOKIE['username']."<br>";

     ?>
     <h4> If you go previous page then new cookie will genarate  <a href="index.php">Previous Page</a></h4>


来源:https://stackoverflow.com/questions/19260793/php-cookies-not-working

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!