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