I am trying to retrieve, not set, a Cookie that exists for a certain site. The Cookie exists and was set during a prior session with an expiration date well into the future. It is not set with HttpOnly. It is set with path=/ and secure.
So, is it possible to be able to retrieve the cookie on future initial/first page loads? I am wondering how sites, like ecommerce sites, etc., know that I have items in my cart when I have not created an account, etc.your text
WHAT I TRIED
Endless Google Searches.
Reviews of Stack Overflows threads (they only address setcookie() and not how to retrieve a cookie on initial page load.
I have tried testing the existence of the cookie in PHP and if it does not exists I use Location: https://my.example.com/load-the-cookie.html
where the request simply has a JavaScript that sends the request back to the server in hopes that, since I came to client-side, PHOP would somehow now know there are cookies available.
My Initial PHP Script (Note: I have an .htaccess directive that parses .html file against the PHP engine)
/**
* Set the Timezone
*/
if( !isset( $_COOKIE['timezone'] ) && !isset( $_GET['reload'] ) ) {
header( 'Location: https://my.example.com/load-the-cookie.html' );
exit();
}
elseif( !isset( $_COOKIE['timezone'] ) ) {
date_default_timezone_set( DEFAULT_TIMEZONE ); // set the server time zone to the app default
}
else {
date_default_timezone_set( $_COOKIE['timezone'] ); // set the server time zone to user's local timezone
}
My Subsequent/Second Page Request
<!DOCTYPE html>
<html>
<head>
<meta name="robots" content="noindex, nofollow">
<script>
//const ck = document.cookies;
//alert("* "+ck+" *");
window.location.href = "https://my.example.com";
</script>
</head>
<body>
</body>
</html>
WHAT I EXPECT
I am trying to get a PHP script to retrieve an existing cookie on initial/first page load.
Before you close this question directing me to other SO threads that only seem to address setcookie(), can you simply respond with YES, this is possible or NO, this is not possible to do? Thanks…I’ll continue to try to come up with some sort of way for a subsequent web session to continue on from where it left off on its last vist.
dott is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.