User Language Class gets/sets the page language that the user accesses, and reads Accept-language if the user does not have an access language set. Display the corresponding page according to the user's chosen language (English, Simplified Chinese, Traditional Chinese)
UserLang.class.php
<?php/** user Language Class gets/sets the page language accessed by the user and reads Accept-language * date:2014-05-26 * AUTHOR:FD If the user does not have an access language set Ipzone * ver:1.0 * * Func: * Public gets get user access language * Public set Set User access language * Private Getacceptlanguage get Http_accept_language/class userlang{/class start private $name = ' Userlang '; Cookie name Private $expire = 2592000; Cookies expire/** initialization * @param String $name Cookie name * See more highlights in this column: http://www.b ianceng.cnhttp://www.bianceng.cn/webkf/php/* @param int $expire Cookie expire */Public function __constr UCT ($name = ', $expire =null) {//Set cookie name if ($name!= ') {$this->name = $na
Me }//Set cookie expire if (Is_numeric ($expire) && $expire >0) {$this->e
Xpire = Intval ($expire);
}
}/** Get user Access language */Public function getting () {//To determine whether the user has a set language if (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 function set ($lang = ') {
$lang = Strtolower ($lang); can only be in English, Simplified Chinese, Traditional Chinese if (In_array ($lang, Array (' en ', ' SC ', ' TC ')) {Setcookie ($this->name, $lang, TI
Me () + $this->expire);
/** get Http_accept_language/Private Function getacceptlanguage () {
$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
<?php
require "UserLang.class.php";
$obj = new Userlang (' Sitelang ', 3600);
echo $obj->get (). ' <br> ';
? >