Set cookie wih JS, read with PHP problem

javascriptphpcookiessetcookie

Last Update : 2023-09-22 UTC 12:40:33 PM

Answers of > Set cookie wih JS, read with PHP problem

w3coded php setcookie w3coded php setcookie Meta Stack Overflow w3coded php setcookie , w3coded php setcookie Stack Overflow for Teams w3coded php setcookie Where developers & w3coded php setcookie technologists share private knowledge with w3coded php setcookie coworkers w3coded php setcookie , w3coded php setcookie GitLab launches Collective on w3coded php setcookie Stack Overflow , w3coded php setcookie Stack w3coded php setcookie Overflow Public w3coded php setcookie questions & answers w3coded php setcookie

So your cookie is not giving the relevant information to the browser for it to be accessible across subdomains and/or the directory path.

document.cookie = 'ppkcookie1=testcookie; expires=Thu, 2 Aug 2001 20:47:11 UTC; path=/; ;domain=.example.com'

Note, .example.com is just an example domain (you need yours in there), and you do not need a wildcard other than the initial . for it go across all subdomains. And you need to generate an expires= date. From QuirksMode:

function createCookie(name,value,days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    } else {
        var expires = "";
    }
    document.cookie = name+"="+value+expires+"; path=/; domain=.example.com";
}

http://example.com/test.php

function createCookie(name,value,days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/; domain=.example.com";
}

createCookie('cookieee','stuff','22');

http://example.com/test/test.php

<pre>
<?php 

print_r($_COOKIE);

?>

Can you give some more information? Are they both on the same domain just different files? Is the line

document.cookie = cookieName+"="+cookieValue;

If you want to expand the domain available to the cookie you need to specify it as part of the cookie:

document.cookie = cookieName + '=' + cookieValue + '; path=/;';

For setting the cookie:

document.cookie = "key=value; expires=Fri, 03 Aug 2018 12:00:00 UTC; path=/";

For setting the cookie:

setcookie('key', 'value', (time() + (3600*2)), '/');

For accessing:

if (isset($_COOKIE['key'])) echo $_COOKIE['key'];

Current topics : Set cookie wih JS, read with PHP problem

Newly Added Questions

Similar Questions

Questions :

First Object Key Value Compare With The Another Object Key?

Last Update : 2023-09-22 UTC 13:52:11 PM

Questions :

Do I Need To Purge Data Cache In Swr?

Last Update : 2023-09-22 UTC 13:52:05 PM

Questions :

Accessing The Nested Object Items

Last Update : 2023-09-22 UTC 13:51:59 PM

Questions :

React 'TypeError: Items Is Not Iterable'

Last Update : 2023-09-22 UTC 13:51:51 PM

Questions :

How To Pass Html Tags As Part Of JavaScript Argument

Last Update : 2023-09-22 UTC 13:51:32 PM

Questions :

Callback Function Call Before Calling Another Function

Last Update : 2023-09-22 UTC 13:51:26 PM

Questions :

Cannot Export React Class

Last Update : 2023-09-22 UTC 13:51:21 PM

Questions :

React Native Js. Nothing Shows When Pressing The Start Button

Last Update : 2023-09-22 UTC 13:51:05 PM

Questions :

Removing Element From DOM After Set Amount Of Time

Last Update : 2023-09-22 UTC 13:50:52 PM

Top
© 2023 W3CODED - All Rights Reserved.