[英]PHP cookies redirect issue
我不確定問題出在哪里,但Cookie無法用於子域。 有沒有解決的辦法,還是有更好的方法來設置cookie域? 謝謝!
<?php
ini_set("session.cookie_domain", ".example.com");
// Set cookie and redirect when user change city
if( isset($_POST['city']) && $_POST['city'] != '' ){
$cookie_expire = time() + 50400;
setcookie('city', $_POST['city'], $cookie_expire, '/');
header("Location: http://".$_POST["city"].".example.com");
die();
}
// Redirect if user selected default city
if (isset($_COOKIE["city"])) {
$subdomain = array_shift(explode(".",$_SERVER['HTTP_HOST']));
if ($_COOKIE["city"] != $subdomain) {
header("Location: http://".$_COOKIE["city"].".example.com");
die();
}
}
嘗試使用setcookie的$ domain參數。
setcookie('city', $_POST['city'], $cookie_expire, '/', '.example.com'); // With leading dot for old browsers
看到這里: http : //php.net/setcookie
如果Cookie路徑為http://www.example.com/,並且您重定向到http://www.example.com (請注意缺少尾部/),請檢查Cookie上的路徑,某些瀏覽器會刪除Cookie-在我的經驗中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.