Why are my cookies not setting?

Viewed 86225

I have the following PHP function:

function validateUser($username){
    session_regenerate_id (); 
    $_SESSION['valid'] = 1;
    $_SESSION['username'] = $username;
    setcookie('username2',$username,time()+60*60*24*365);
    header("Location: ../new.php");
}

And then I fetch the cookie:

echo $_COOKIE['username2']; exit();

(I only put exit() for debugging purposes)

Only problem, it's coming out blank. Any ideas?

UPDATE: This is how the function is called:

    if(mysql_num_rows($queryreg) != 0){
    $row = mysql_fetch_array($queryreg,MYSQL_ASSOC);
    $hash = hash('sha256', $row['salt'] . hash('sha256', $password));
    if($hash == $row['password']) {
        if($row['confirm'] == 1){
            if(isset($remember)){
                setcookie('username',$username,time()+60*60*24*365);
                setcookie('password',$password,time()+60*60*24*365);
            } else {
                setcookie('username','',time()-3600);
                setcookie('password','',time()-3600);
            }
            validateUser($username);

I didn't include all the if() statements to save some space.

9 Answers

This requires that you place calls to this function prior to any output, including and tags as well as any whitespace.

this is how the structure looks

<?php
$cookie_name = "user";
$cookie_value = "Ahmed Moftah";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day
?>
<?php
if(!isset($_COOKIE[$cookie_name])) {
    echo "Cookie named '" . $cookie_name . "' is not set!";
} else {
    echo "Cookie '" . $cookie_name . "' is set!<br>";
    echo "Value is: " . $_COOKIE[$cookie_name];
}
?>
<html>
<body>



</body>
</html>

It might be a cache problem. Try closing the browser and opening a new one with the localhost file path. I had the same issue and my page was cached so the cookies weren't working, even though I could put in new code and see a change on the page. Very weird... Cleaning your cache might help, try that first. Then try a new browser, then try to go to your localhost:8080 index and hit refresh to see when the last page was modified.

If that doesn't fix it, try restarting LAAMP or XAAMP or whatever you are using.

This happens with the session cookies are disabled.

You can navigate to your php.ini file(changes depending on server. Ubuntu 20.04's default is /etc/php/{X.x}/{apache2|[others]}/php.ini) and ensure that session.use_cookies=1

Restart your server and then try to set the cookie. They should immediately be available.

You probably have sent header hereby making it impossible to set header for cookie. It might be a simple solution of ob_start() at the start of your page.

[Solution tested in XAMPP for PHP 8.0.19] I modified the file php.ini and set session.auto_start=1. Keep in mind that i'm calling session_set_cookie_params([param => value]) just before session_start() every time this functions appears in my code, in every file.

session.auto_star: Initialize session on request startup.

http://php.net/session.auto-start

Still studying and analysing the reason and functionality of this, it was kind of a serendipity. Probably not the most secure solution.

Related