PHP Get Set User access page language class

Source: Internet
Author: User
Tags php class set cookie

PHP implementation gets and sets the user Access page language class, can be implemented to get/set user access to the page language, if the user does not set the access language, then read accept-language.

A PHP get and set User access page language class, specific analysis:
The instance user Language Class gets/sets the page language that the user accesses, and if the user does not set the access language, reads Accept-language.

Display the corresponding page according to the language selected by the user (English, Simplified Chinese, Traditional Chinese)
The UserLang.class.php class file is as follows:

<?PHP/** User Language Class gets/sets the language of the page that users access, if the user does not set the access language, read Accept-language * date:2014-05-26 * author:fdipzone * ver:1.0 * www.jbxue.com* Func: * Public get get user access language * Public set Set User access language * Private getacceptlanguage get http_accept_language */classuserlang{//class StartPrivate $name= ' Userlang ';//Cookie NamePrivate $expire= 2592000;//Cookie Expire/** Initialize * @param String $name cookie name * @param int $expire cookie expire*/ Public function__construct ($name= ",$expire=NULL){ //Set cookie nameif($name! = "){ $this->name =$name; } //Set Cookie expireif(Is_numeric($expire) &&$expire>0){ $this->expire =intval($expire); } } /** Get user access language*/ Public functionget () {//determine if the user has a language setif(isset($_cookie[$this-name])) { $lang=$_cookie[$this-name]; }Else{ $lang=$this-getacceptlanguage ();} return $lang; } /** Set User access language * @param String $lang user Access language*/ Public functionSet$lang= ' '){ $lang=Strtolower($lang); //can only be in English, Simplified Chinese, Traditional Chineseif(In_array($lang,Array(' en ', ' SC ', ' TC ')))){ Setcookie($this->name,$lang, Time()+$this-expire); } } /** Get Http_accept_language*/Private functionGetacceptlanguage () {$lang=Strtolower($_server[' Http_accept_language ']); if(In_array(substr($lang, 0,5),Array(' Zh-tw ', ' zh_hk '))){ $lang= ' TC '; }ElseIf(In_array(substr($lang, 0,5),Array(' Zh-cn ', ' ZH-SG '))){ $lang= ' SC '; }Else{ $lang= ' en '; } return $lang; } } //class End?>

Demo Example:

<? PHP require "UserLang.class.php"$objnew Userlang (' Sitelang ', 3600Echo $obj->get (). ' <br> '?>

PHP Get Set User access page language class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.