I want a script that firsts verifies that $_COOKIE['location']
is set. If $_COOKIE['location']
is not set, I want to save the entrance URL into $_COOKIE['EntryURL']
and set $_COOKIE['location']
to 'TRANS' while redirecting user to enter their zip or manually select their location.
So I have generated the following PHP:
if(!isset($_COOKIE['location'])) {
$EntryURL = $_SERVER['REQUEST_URI'];
if($EntryURL == '/') {
$EntryURL = '/index.php?pg=Home';
}
setcookie('EntryURL', $EntryURL);
setcookie('location', 'TRANS');
header("Location: MYSITE/index.php?pg=" . urlencode('Please Select Your Location'));
}
The problem that I am encountering, however, is that all of this code is being executed with or without $_COOKIE['location']
being set. The page opens, sees $_COOKIE['location']
is not set, sets $_COOKIE['EntryURL']
and sets $_COOKIE['location']
and properly redirects.
However, on the redirect page (which is included from the index, meaning that the same code will be called again) it STILL executes the same script, generating a second cookie for each and prevents the redirect code on my location query page from working properly (as it is accessing the $_COOKIE['EntryURL']
which has been rewritten, rather generated again as it is saved twice, to reflect the $_SERVER['REQUEST_URI']
from the location page.
I CANNOT understand how it is possible to execute this code block with or without location cookie being set. Hopefully I am doing something stupid that someone can quickly identify.