2014-02-26 92 views
0

我想知道是否有任何可能性避免重定向与我下面的问题。PHP避免重定向

的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 
} 

firstTime

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 

好了,PR问题是这样的:当用户第一次进入页面时,它没有cookie,所以我检查他的ip并设置cookie。但是,如果用户不刷新页面,他将在默认语言/国家(英语/美国)中看到页面内容,因此我将此重定向强制根据用户位置显示内容。

代码$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); 
} 

我一直在努力,是最明确的我可以。我只是问有没有可能做到这一点没有重定向?因为谷歌说,以避免重定向..当我分析我的网页谷歌pagespeed它也说。

您的页面有2个重定向。重定向在页面加载之前引入额外的延迟 。 mywebsite.com/

mywebsite.com/redirect.php

mywebsite.com/index.php

回答

0

setcookie在用户浏览器上设置cookie信息,但当前cookie信息仅在脚本开始时加载。

重定向/刷新是我相信的唯一方法。

0

,而不是重定向和退出()你为什么不返回数组($ locale_f, $ id_language);

0

也许你可以尝试这样的事情

public function get_country_language(){ 
    $id_country = 2; // United States 
    $id_language = 1; // English 
    $ids = array(); 

    if(isset($_COOKIE['country']) && isset($_COOKIE['language'])){ 
     $id_country = $this->get_country_from_code($_COOKIE['country']); 
     $id_language = $load->get_language_from_code($_COOKIE['language']); 
     $ids = array($id_country, $id_language); 
    }  
    else{ 
     $ids = $this->firstTime(); 
    } 
    return $ids; 
    } 

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), '/'); 

    return array($country_f, $locale_f); 
    } 

...在这之后,你可以解析的内容。

+0

Humm,我了解你的观点和你的代码。我会试一试。 – Linesofcode

+0

好吧,它没有工作,再加上,页面需要更多的时间来加载内容(因为它调用了两次firstTime(),这需要6秒的时间来读取页面。 重定向需要2秒。 – Linesofcode

+0

okey,但为什么两次,您是否以其他方式调用firstTime()函数? 我会重写代码并再次测试。 – user3351733