This article describes the implementation of PHP to obtain and set the user Access page language classes, to share with you for your reference. The specific analysis is as follows:
The instance 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)
The UserLang.class.php class files are as follows:
<?php/** user Language Class gets/sets the page language accessed by the user and reads Accept-language * date:2014-05-26 * author:fdipzo If the user does not have an access language set NE * 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 $e Xpire = 2592000;
Cookie expire/** initialization * @param String $name cookie name * @param int $expire cookie Expire * * Public function __construct ($name = ', $expire =null) {//Set cookie name if ($name!= ') {$this->name
= $name; }//Set cookie expire if (Is_numeric ($expire) && $expire >0) {$this->expire = intval ($expire
);
/** Gets the user access language/Public function get () {//Determines 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 ($lan
g); can only be in English, Simplified Chinese, Traditional Chinese if (In_array ($lang, Array (' en ', ' SC ', ' TC ')) {Setcookie ($this->name, $lang, Time () + $this-&
Gt;expire); /** get Http_accept_language/Private Function Getacceptlanguage () {$lang = Strtolower ($_server[' H
Ttp_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?>
The Demo sample program is as follows:
<?php
require "UserLang.class.php";
$obj = new Userlang (' Sitelang ', 3600);
echo $obj->get (). ' <br> ';
? >
I hope this article will help you with the learning of PHP programming.