如何在codeigniter中将cookie中的语言值附加到URL

时间:2018-05-26 23:02:25

标签: php codeigniter cookies

如果在浏览器中设置了cookie,则获取cookie并附加到URL

如果Cookie language=english

重定向到 https://example.com/en

如果Cookie language=deutch

重定向到

https://example.com/de

如果可能,所有Accept-Language

表示cookie的每个值

Cookie language=/en/Accept-Language

重定向到他们的语言页面

查看

  <h5>Translate this page:</h5>
        <div class="lang-holder">
                          <a href="#" class="topbar-menu-toggle" data-toggle="button">

                             <img src="<?php echo base_url();?>assets/img/lang/<?php if ($this->input->cookie('language') != '') 
                            {
                               echo $this->input->cookie('language');
                            } 
                            else 
                            {
                            $language = $this->settings_lib->item('site.default_language');

                              echo $language;
                            } ?>.png">
                          </a>
                        </div>

<div id="topbar-dropmenu" class="animated animated-short slideInDown">
      <div class="topbar-menu row">

            <?php if ($handle = opendir('application/language/')) 
              {

                while (false !== ($entry = readdir($handle))) 
                {
                  if ($entry != "." && $entry != "..") 
                  { ;?>

                    <div class="col-xs-2 col-sm-1">
                      <a href="<?php echo base_url() ?>home/language/<?php echo $entry; ?>">
                        <img class="h30" src="<?php echo base_url();?>assets/img/lang/<?php echo $entry; ?>.png">
                        <span class="fs14 hidden-xs"><?php echo ucfirst($entry); ?></span>
                      </a>
                    </div>
                    <?php
                  }
                }

                closedir($handle);
              } ?>



      </div>

控制器

    public function language($lang = false)
{
    $folder = 'application/language/';
    $languagefiles = scandir($folder);
    if (in_array($lang, $languagefiles)) {
        $cookie = array(
            'name' => 'language',
            'value' => $lang,
            'expire' => '31536000',
        );

        $this->input->set_cookie($cookie);
    }
    redirect($this->input->server('HTTP_REFERER'));
}

1 个答案:

答案 0 :(得分:0)

使用这个概念:

.fq
RewriteEngine on

RewriteCond %{HTTP_COOKIE} ^lang=/en/en$ [NC]
RewriteRule .* https://example.com/en [R=301,L]

RewriteCond %{HTTP_COOKIE} ^lang=/en/ca$ [NC]
RewriteRule .* https://example.com/ca [R=301,L]