【问题标题】:PHP avoid redirectPHP避免重定向
【发布时间】: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

【问题讨论】:

    标签: php redirect cookies


    【解决方案1】:

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

    我相信重定向/刷新确实是解决这个问题的唯一方法。

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

        也许你可以试试这样的东西

        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);
            }
        

        ...之后就可以解析内容了。

        【讨论】:

        • 嗯,我理解你的观点和你的代码。我试试看。
        • 它没有工作,另外,页面需要更多时间来加载内容(因为它调用了 firstTime() 两次),这需要大约 6 秒的时间来阅读页面。重定向需要 2 秒。
        • 好的,但是为什么两次,您是否以其他方式调用了 firstTime() 函数?我会重写代码并再次测试。
        猜你喜欢
        • 2011-11-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-08-24
        相关资源
        最近更新 更多