Set cookie and redirect to original requested url

Beginner at php. Have an age gate page where users have to click on age verification before proceeding to site. Currently everything works, but need redirect to work for urls that are provided to user. So if I provide a link to the about page they are redirected to that page after verifying age.

This is my current code

$cookie_name = "ageVerify";
$cookie_value = "1";
setcookie($cookie_name, $cookie_value, time() + (86400 * 3), "/"); // 86400 = 1 day

header("Location: index");

Here’s the snippet:

if(!$_COOKIE['ageVerify']) {
    header("Location: http://domain.com/age-gate");
    exit;
}

I’ve tried to store the url and call in the header but that doesn’t work

$referer = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";  
setcookie(‘age_referer’, $referer, 0, “/“);

header("Location: age_referer");

Any help would be greatly appreciated!

If you temporarily change the code to

$referer = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";  
/* debugging only */ 
echo $referer; 
exit: 
/* end debugging */ 
setcookie(‘age_referer’, $referer, 0, “/“); 

does it look correct?

There’s no output and it takes me to the blank page of the php file.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.