CI如何在MY_Lang中检索cookie值

时间:2018-10-31 13:10:47

标签: php codeigniter

我已经扩展了CI_Lang以处理多语言。 现在我想从cookie中获取site_lang,看看是否已经选择了一种语言

 class MY_Lang extends CI_Lang
{

    var $currentLanguage;

    function __construct() {

        global $URI, $CFG, $IN;

        $config =& $CFG->config;

        /*Get array with available languages*/
        $index_page    = $config['index_page'];
        $default_abbr  = $config['lang_abbr'];
        $lang_uri_abbr = $config['lang_uri_abbr'];

        /* get the language abbreviation from uri */
        $uri_abbr = $URI->segment(1);

        /* adjust the uri string leading slash */
        $URI->uri_string = preg_replace("|^\/?|", '/', $URI->uri_string);

        /* get the language_abbreviation from cookie */
        $lang_abbr = $IN->cookie('site_lang');
// check validity against config array
        if (isset($lang_uri_abbr[$uri_abbr])) {

            // reset uri segments and uri string
            $URI->segment(array_shift($URI->segments));
            $URI->uri_string = preg_replace("|^\/?$uri_abbr|", '', $URI->uri_string);

            if(!empty(LOCATION)){
                $this->currentLanguage = strtolower('nl');

                // set config language values to match the user language
                $config['language'] = $this->currentLanguage;
                $config['lang_abbr'] = 'nl-be';
            }else{
                $this->currentLanguage = strtolower($lang_uri_abbr[$uri_abbr]);
                // set config language values to match the user language
                $config['language'] = $lang_uri_abbr[$uri_abbr];
                $config['lang_abbr'] = $uri_abbr;
            }

            // check and set the uri identifier
            $index_page .= empty($index_page) ? $uri_abbr : "/$uri_abbr";

            // reset the index_page value
            $config['index_page'] = $index_page;

        } else {
            $config['lang_splash_ignore'] = FALSE;
        }
    }

现在$ lang_abbr的值是

string '9e25c0674e5616408f3b1fedd0c43243c0e25335dc92962238f6b2d5fcd0ed5b34d78fe5eceef80e447eaef904965f18699a58c082fde710d0e9976afc7373e9RL3VywiN5Ga7GDnac/vhphR+Ls4NnEi6fHZNm0bGrsQ=' (length=172)

$ _ COOKIE的var_dump

    array (size=4)
  'httpTokens' => string 'd18e3a9aff2ee16e7366ac2bf15c7332d1a6afde32ce9db3be4f9193459706feea8925a54ba4369404adbbaf3803e470cd435b3beaf9d8ba3bff9134d351f79f26iSUDpBMwABR6eElE9igxS55I+KEvntC5NgVeQB768=' (length=172)
  'httpUser' => string '74f0cc1d2fb1fd22d963bada94a7ea7cd3221cb8f4c023a66cc337986de0f26c87f44c155acb66d9dc56427e762c0b6906650d9e9c56ba83434e89cc06adc8fbNvdll6arPiQsdRJt/yaOL3J+WZWNfizLE9SRQ2KgyNU+qOz9bqPhVLhfB1yEVElXaw9gHoGp6WwqbsbUhH8Ca+g+dIACtHY1fAJUhc0Xpbs=' (length=236)
  'ci_session' => string '69efc00e94d5eea572c53070ae32e831395c911e' (length=40)
  'site_lang' => string '9e25c0674e5616408f3b1fedd0c43243c0e25335dc92962238f6b2d5fcd0ed5b34d78fe5eceef80e447eaef904965f18699a58c082fde710d0e9976afc7373e9RL3VywiN5Ga7GDnac/vhphR+Ls4NnEi6fHZNm0bGrsQ=' (length=172)

通常应该是“ en”或“ fr”之类的。 如何获取该实际值?

编辑

我也将控制器扩展到了MY_Controller中:

    class MY_Controller extends Auth_Controller
{

    var $languages;

    var $currentLanguage;

    var $currentLanguageAbbr;

    var $currentLanguageItem;

    /**
     * Class constructor
     */
    public function __construct()
    {
        parent::__construct();
        $this->is_logged_in();

        $this->setCurrentLanguage();
        $this->setSubdomainBackgroundColorGlobalVariable();

        $this->setSupportedLanguages();
    }

    public function setCurrentLanguage(){
        $ci =& get_instance();

        //Get current language from the config
        $currentLang = $ci->config->item('language');
        $currentLangAbbr = $ci->config->item('lang_abbr');

        //Force Dutch when local platform
        if(!empty(LOCATION)){
            $this->currentLanguage = strtolower('nl');
            $this->currentLanguageAbbr = strtolower('nl-be');
        }else{
            $this->currentLanguage = strtolower($currentLang);
            $this->currentLanguageAbbr = strtolower($currentLangAbbr);
        }

        //Set cookie and session with correct language
        $this->session->set_userdata('site_lang', $this->currentLanguage);
        $cookie = array(
            'name'   => 'site_lang',
            'value'  => $this->currentLanguage,
            'expire' => 259200,
            'secure' => COOKIE_XSS_FILTERING
        );
        $this->input->set_cookie($cookie);

        defined('CURRENT_LANG')                       OR define('CURRENT_LANG', $this->currentLanguage);
    }

0 个答案:

没有答案