PHP implementation to get and set User access page language class _php skills

Source: Internet
Author: User

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.

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.