PHP implementation get and set User access page language class

Source: Internet
Author: User
Tags php class

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:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 <?php /** user Language Class gets/sets the page language that the user accesses, and reads accept-language  *  if the user does not have the access language set up date:  2014-05-26  *  author:fdipzone  *  ver:  1.0  *  *  Func:  *  public ge t        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   &nbsp ;p rivate $expire = 2592000; Cookies expire days            /** initialization     * @param strin G $name   Cookies name    * @param int  $expire cookies expire    */   &NB Sp;public function __construct ($name = ', $expire =null) {          //Setting cookies name    &Nbsp; if ($name!= ') {         $this->name = $name;       }          //Set Cookies expire      if ( Is_numeric ($expire) && $expire >0) {         $this->expire = intval ($ Expire);      }    }        /** Get user access language * *     public function Get () {          //to determine if 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, Time () + $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 ?>

The Demo sample program is as follows:

1 2 3 4 5 6 7 <?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.