Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect user to website based on stored cookie information

I have a website where my landing page could send the user to one of our locations. I am wondering if there is a way to store their location choice and redirect them to the correct location based on information I stored. I have seen this before on a lot of store pages where they keep and let you browse your local store. Would a cookie be the best way to store this information?

like image 681
user2044644 Avatar asked Jan 14 '23 07:01

user2044644


1 Answers

Cookies would be perfect for such a situation. Simply record the location id and then redirect to that place next time!

// store an array of location cookie names and there location values
$places = array('us'=>'/united-states.php');

// After user chooses a location, store the cookie based on his choice: in this case, us!
setcookie('location','us', time() + (3600 * 24 * 7));

// On a new page check the cookie is set, if it is then redirect users to the value of that cookie name in the array!
if(isset($_COOKIE['location'])){
    header('Location: '.$places[$_COOKIE['location']]);
}
like image 55
Zevi Sternlicht Avatar answered Jan 22 '23 23:01

Zevi Sternlicht