【发布时间】:2014-02-26 11:04:43
【问题描述】:
我想知道下面我的问题是否有可能避免重定向。
index.php
$select = new select();
if(!isset($_COOKIE['country']) || !isset($_COOKIE['language'])){
$select->firstTime();
$content = $select->clients(); // this will get clients according to the cookies
}
第一次
public function firstTime(){
$ip = $_SERVER['REMOTE_ADDR'];
$xml = simplexml_load_file("http://freegeoip.net/xml/$ip");
$locale = $xml->CountryCode;
$country_f = "us"; // default
$locale_f = "en_US"; // default
/*
++ Select country from database according to the locale
$country_f = $variable_database
*/
setcookie('language', $locale_f, time() + (60*60*24*365), '/');
setcookie('country', $country_f, time() + (60*60*24*365), '/');
header('location: redirect.php');
exit();
}
redirect.php
header('location: index.php'); exit(); // back to index.php
好吧,问题是这样的:当用户第一次进入页面时它没有cookies,所以我检查了他的ip并设置了cookies。但是,如果用户不刷新页面,他将看到默认语言/国家(英语/美国)的页面内容,因此我进行此重定向以强制内容根据用户位置显示。
代码$content = $select->clients(); // this will get clients according to the cookies,会根据他的cookies选择客户端。如果我删除 redirect 代码,客户端将被选为默认的英语/美国,并且不应该因为 cookie 已经与函数 firstTime() 一起存储。
函数$select->clients()被读取AFTER $select->firstTime(),为什么没有检测到cookie?
客户
list($id_country, $id_language) = $this->get_country_language();
/*
... SELECT * FROM clients WHERE id_country = $id_country
*/
get_country_language
public function get_country_language(){
$id_country = 2; // United States
$id_language = 1; // English
if(isset($_COOKIE['country'])){
$id_country = $this->get_country_from_code($_COOKIE['country']);
}
if(isset($_COOKIE['language'])){
$id_language = $load->get_language_from_code($_COOKIE['language']);
}
return array($id_country, $id_language);
}
我尽量做到最明确。我只是问是否有可能在没有重定向的情况下这样做?因为谷歌说要避免重定向......当我用谷歌页面速度分析我的页面时,它也这么说。
您的页面有 2 个重定向。重定向引入了额外的延迟 在页面可以加载之前。 mywebsite.com/
mywebsite.com/redirect.php
mywebsite.com/index.php
【问题讨论】: