user979331
user979331

Reputation: 11841

Cookie is not setting in PHP

I am trying to set a cookie like so:

<?php

                $cookie_name = "user";
                $cookie_value = "James";
                setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day

            ?>

            <?php if(!isset($_COOKIE['user'])) { ?>
//Do Something

            <?php } ?>

but Its not setting, I am using chrome and when I check my cookies, they are not there :(

Upvotes: 0

Views: 213

Answers (3)

Mav
Mav

Reputation: 1190

$_COOKIE[] variable is set when the page loads, due to the stateless nature of the web.

Here's what the documentation says:

Once the cookies have been set, they can be accessed on the next page load with the $_COOKIE array. Cookie values may also exist in $_REQUEST.

But there are a number of word arounds to access it immediately after setting it.

  1. you can manually set the value for $_COOKIE[] right when you set the cookie to access it or you could use an intermediate variable like so:

    <?php
    
            $cookie_name = "user";
            $cookie_value = "James";
            setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day
            $_COOKIE[$cookie_name] = $cookie_value;
            ?>
    

    if(!isset($_COOKIE['user'])) will return true now.

  2. Another less efficient solution world be to reload the page right after setting the cookie. like:

            $cookie_name = "user";
            $cookie_value = "James";
            setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day
            header("Location: your_script.php");
        ?>
    

    When the page reloads, if(!isset($_COOKIE['user'])) will return true again.

Hope this helps! :)

Upvotes: 1

Muhammad Abdul-Rahim
Muhammad Abdul-Rahim

Reputation: 2010

From the setcookie documentation:

Once the cookies have been set, they can be accessed on the next page load with the $_COOKIE array. Cookie values may also exist in $_REQUEST.

You will not be able to immediately access cookies on the initial page load. Additionally, make sure that you are not outputting any HTML before setting the cookie (protocol restriction).

If you absolutely need to access the cookie immediately, you have a few options:

  • After setting the cookie, reload the page immediately with header
  • If this is not what you want to do, you can use JavaScript to set cookies, but you should note that some people disable JS in their browsers. I would not recommend this solution, personally.

Upvotes: 1

Jose B
Jose B

Reputation: 2130

As said in the comments, the cookie will not be visible within the same request that set it. You need to do a redirect in order to access the cookie, e.g.

setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day    
header('Location: ' . $your_script_location_path);
exit;

Also, the code inside your check

if(!isset($_COOKIE['user'])) {
    // Do something
}

will only get executed when the cookie is not set.

Upvotes: 0

Related Questions