根据存储的cookie信息将用户重定向到网站

use*_*644 4 html css php cookies html5

我有一个网站,我的目标网页可以将用户发送到我们的某个位置.我想知道是否有办法存储他们的位置选择,并根据我存储的信息将它们重定向到正确的位置.我之前在许多商店页面上看到过这种情况,并且让您浏览本地商店.Cookie是否是存储此信息的最佳方式?

Zev*_*cht 9

Cookie适用于这种情况.只需记录位置ID,然后下次重定向到该位置!

// 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']]);
}
Run Code Online (Sandbox Code Playgroud)