Php implementation to obtain and set the user access page language class _ PHP Tutorial

Source: Internet
Author: User
Tags set cookie
Php is used to obtain and set the language class for accessing the page. Php implementation to obtain and set the language class for the user to access the page. This article describes how to obtain and set the language class for the user to access the page in php, and shares it with you for your reference. The specific analysis is as follows: php implements the acquisition and setting of user access page language classes

The example in this article describes how to obtain and set the language class for the user to access the page in php and share it with you for your reference. The specific analysis is as follows:

The User Language Class of this instance obtains/sets the page Language accessed by the User. if the User does not set the access Language, the Accept-Language is read. Display the corresponding page (English, Simplified Chinese, Traditional Chinese) based on the language selected)

The UserLang. class. php class file is 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

43

44

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

/** User Language Class: get/set the page Language accessed by the User. if the User does not set the access Language, read the Accept-Language

* Date: 2014-05-26

* Author: fdipzone

* Ver: 1.0

*

* Func:

* Public get obtains the user access language

* Public set: set the user access language

* Private getAcceptLanguage: HTTP_ACCEPT_LANGUAGE

*/

Class UserLang {// class start

Private $ name = 'userlang '; // cookie name

Private $ expire = 2592000; // cookie expire 30 days

/** Initialization

* @ Param String $ name cookie name

* @ Param int $ expire cookie expire

*/

Public function _ construct ($ name = '', $ expire = null ){

// Set the cookie name

If ($ name! = ''){

$ This-> name = $ name;

}

// Set cookie expire

If (is_numeric ($ expire) & $ expire> 0 ){

$ This-> expire = intval ($ expire );

}

}

/** Obtain the user access language */

Public function get (){

// Determine whether the user has set a language

If (isset ($ _ COOKIE [$ this-> name]) {

$ Lang = $ _ COOKIE [$ this-> name];

} Else {

$ Lang = $ this-> getAcceptLanguage ();

}

Return $ lang;

}

/** Set the user access language

* @ Param String $ lang user access language

*/

Public function set ($ lang = ''){

$ Lang = strtolower ($ lang );

// Only English, Simplified Chinese, and traditional Chinese

If (in_array ($ lang, array ('en', 'scs', '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,), array ('zh-tw ', 'zh _ hk '))){

$ Lang = 'TC ';

} Elseif (in_array (substr ($ lang,), array ('zh-cn', 'zh-sg '))){

$ Lang = 'scs ';

} Else {

$ Lang = 'en ';

}

Return $ lang;

}

} // Class end

?>

The demo sample program is as follows:

1

2

3

4

5

6

7

Require "UserLang. class. php ";

$ Obj = new UserLang ('sitelang ', 3600 );

Echo $ obj-> get ().'
';

?>

Examples in this article describe how to obtain and set the language class of the user access page in php, and share it with you for your reference. The specific analysis is as follows :...

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.