skip to Main Content

I am using PHP 8.0 and this is a wordpress site version 6.1.1

I have a plugin that is setting a cookie like so:

window.tourmaster_set_cookie = function( cname, cvalue, expires ){
        if( typeof(expires) != 'undefined' ){
            if( expires == 0 ){
                expires = 86400;
            }

            var now = new Date();
            var new_time  = now.getTime() + (parseInt(expires) * 1000);
            now.setTime(new_time);

            expires = now.toGMTString();
        }

        document.cookie = cname + "=" + encodeURIComponent(cvalue) + "; expires=" + expires + "; path=/";
    }

This is being called like so:

tourmaster_set_cookie('tourmaster-room-cart', JSON.stringify(cart_cookie), 31536000);

I can see in my console log that this cookie is being set.

However when I try to get the cookie in PHP:

$_COOKIE['tourmaster-room-cart']

I get this error:

Warning: Undefined array key "tourmaster-room-cart"

When I do a print_r on $_COOKIE the cookie "tourmaster-room-cart" is not there.

What is going wrong here and how can I fix it?

UPDATE

I created this simple cookie:

document.cookie = "username=John Doe; expires=Thu, 18 Dec 2025 12:00:00 UTC; path=/";

and it appears in my php $_COOKIE;

However this does not show up in $_COOKIE

window.tourmaster_set_cookie = function( cname, cvalue, expires ){
        expires = "Thu, 1 Jan 2026 12:00:00 UTC";
        if( typeof(expires) != 'undefined' ){
            if( expires == 0 ){
                expires = 86400;
            }

            var now = new Date();
            var new_time  = now.getTime() + (parseInt(expires) * 1000);
            now.setTime(new_time);

            expires = now.toGMTString();
        }

        document.cookie = cname + "=test; expires=Thu, 1 Jan 2026 12:00:00 UTC; path=/";

    }

ANOTHER UPDATE

I added a few more cookies to see if they would get displayed with $_COOKIES, they all did except for the last test with encodeURIComponent(cvalue) as the value. It must be something in my value? Here is the updated code and below that is the value.

window.tourmaster_set_cookie = function( cname, cvalue, expires ){
        if( typeof(expires) != 'undefined' ){
            if( expires == 0 ){
                expires = 86400;
            }

            var now = new Date();
            var new_time  = now.getTime() + (parseInt(expires) * 1000);
            now.setTime(new_time);

            expires = now.toGMTString();
        }
        document.cookie = "newusernameagain=John Doe; expires=Thu, 18 Dec 2025 12:00:00 UTC; path=/";
        document.cookie = "newusername-again=John Doe; expires=Thu, 18 Dec 2025 12:00:00 UTC; path=/";
        document.cookie = "new-username-again=" + encodeURIComponent(cvalue) + "; expires=Thu, 18 Dec 2025 12:00:00 UTC; path=/";
        document.cookie = cname + "=" + encodeURIComponent(cvalue) + "; expires=" + expires + "; path=/";

    }

And the value

%5B%7B%22start_date%22%3A%222023-02-05%22%2C%22end_date%22%3A%222023-02-06%22%2C%22room_amount%22%3A%221%22%2C%22adult%22%3A%5B%222%22%5D%2C%22children%22%3A%5B%220%22%5D%2C%22room_id%22%3A%2215701%22%2C%22post_type%22%3A%22room%22%7D%5D

I have no idea what is going wrong.

FINAL UPDATE

For some stupid reason when I remove the dashes from the cookie name and where it is referenced everything works. For some odd reason $_COOKIE does not like dashes for the cookie name on this godaddy server, very odd stuff.

2

Answers


  1. Chosen as BEST ANSWER

    For some stupid reason when I remove the dashes from the cookie name and where it is referenced everything works. For some odd reason $_COOKIE does not like dashes for the cookie name on this godaddy server, very odd stuff.


  2. If you have passed a correct value (e.g. "SO test") in cart_cookie, then the script should work.

    A normal way to set cookie in JS is like:

    document.cookie = "username=John Doe; expires=Thu, 1 Jan 2016 12:00:00 UTC";
    

    So assuming the JS script is:

    <script>
    window.tourmaster_set_cookie = function( cname, cvalue, expires ){
            document.cookie = cname + "=" + encodeURIComponent(cvalue) + "; expires=" + expires + "; path=/";
        }
    
    tourmaster_set_cookie('tourmaster-room-cart', JSON.stringify('SO test'), "Thu, 1 Jan 2026 12:00:00 UTC");
    
    alert("Done ! Now redirecting to show the cookie string");
    
    window.location.href="testSO5Feb2023c.php"; 
    
    </script>
    

    On running it , it will set the cookie, and then redirect to another page (I named it testSO5Feb2023c.php), which shows that the cookie is correctly set

    For testSO5Feb2023c.php :

    <?php
    
    echo "Cookie is now : <br>";
    echo $_COOKIE['tourmaster-room-cart']; 
    
    ?>
    

    You may see the DEMO

    Last but not least, please note that the cookie is retrievable ONLY after it is set, so make sure the page to display the cookie is different from the one you set the cookie, otherwise you may need a page refresh

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search