Search code examples
phpwordpresscookiessetcookie

how to get cookie php/wordpress


Users come to my site thorough a referral link.

I can successfully set the cookie using either of the following code snippets: (I know it is successful because I can see the cookie file saved on my computer)

function set_my_cookie() { 
if (empty($_COOKIE['OREF2']))
{
wp_set_auth_cookie('OREF2', $_SERVER['QUERY_STRING'], time()+5200, "/", ".website.com");
}
}
add_action( 'init', 'set_my_cookie');

or

function set_newuser_cookie() {
    if (!isset($_COOKIE['OREF2'])) {
        setcookie('OREF2', $_SERVER['QUERY_STRING'], time()+5200, "/", ".website.com");
}
}
add_action( 'init', 'set_newuser_cookie');

Now the user clicks to a different page in my site. They click a form which should pass the cookie info when they click submit.

I am using the following code snippets:

function get_my_cookie() { 
//$_COOKIE['OREF2'];
//var_dump($_COOKIE);
echo $_COOKIE['OREF2'];
}
add_shortcode('cookies', 'get_my_cookie');

This first bit of code has commented lines where I have attempted different ways to get my cookie. None of the 3 ways worked. I then moved on to trying to read if my cookie is set with the following code:

function get_my_cookie() { 
if(!isset($_COOKIE['OREF2'])) {
    echo "Cookie named '" . OREF2 . "' is not set!";
} else {
    echo "Cookie '" . OREF2 . "' is set!<br>";
    echo "Value is: " . $_COOKIE['OREF2'];
}}
add_shortcode('cookies', 'get_my_cookie');

When using this code I am ALWAYS met with "cookie is NOT set" Even though I can clearly see the cookie file on my computer. If the cookie is not set my shortcode can't be used to display anything...

All I want is a shortcode to display my cookie. What am I doing wrong? Any help would be greatly appreciated!


Solution

  • If you're using the following code:

    setcookie('OREF2', $_SERVER['QUERY_STRING'], time()+5200, "/", ".website.com");
    

    This may not setup the cookie properly. One test would be to clear the cookie and test with :

    setcookie('OREF2', $_SERVER['QUERY_STRING'], time()+5200);
    

    These parameters, Path and Domain, are optional. If they are not set correctly, even slightly, you (actually the browser) will not be able to read the Cookie properly as the HTTP URL may not match properly.

    As suggested in the comments, you may want to examine this more: http://php.net/manual/en/function.setcookie.php

    Something that wasn't made clear to me here and totally confused me for a while was that domain names must contain at least two dots (.), hence 'localhost' is invalid and the browser will refuse to set the cookie! instead for localhost you should use false.

    To make your code work on both localhost and a proper domain, you can do this:

    <?php

    $domain = ($_SERVER['HTTP_HOST'] != 'localhost') ? $_SERVER['HTTP_HOST'] : false;

    setcookie('cookiename', 'data', time()+60*60*24*365, '/', $domain, false);

    ?>

    You may want to consider using this code going forward:

    function set_newuser_cookie() {
      if (!isset($_COOKIE['OREF2'])) {
        $domain = ($_SERVER['HTTP_HOST'] != 'localhost') ? $_SERVER['HTTP_HOST'] : false;
        setcookie('OREF2', $_SERVER['QUERY_STRING'], time()+5200, "/", $domain);
      }
    }