Gets and sets the PHP class that the user accesses the page language

Source: Internet
Author: User
Tags php class set cookie set set

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> ';  
      
? >

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.