Home
 Articles
 Book Store
 DIT
 Login
 Register
 News
 Online Training Courses
 Programming
 Research Papers
 Software Development
 Students Notes
  Web Hosting


 
 

 

Learning PHP from admin of this Websie

 PHP About  PHP $_GET  PHP Array Refrence  PHP MYSQL Database
 PHP  Intro  PHP $_POST  PHP Date/Time Ref  PHP Sql Connection
 PHP Download  PHP Date()  PHP File System Ref  PHP Create
 PHP Syntax  PHP include  PHP FTP  PHP Insert
 PHP Variables   PHP File system  PHP HTTP  PHP Select
 PHP Operators  PHP Upload  PHP Mail Refrence  PHP Where
 PHP if..else  PHP Cookies  PHP Math  PHP Order By
 PHP switch  PHP Sessions  PHP MYSQL  PHP Update
 PHP array  PHP Mail  PHP String  PHP Delete
 PHP loops  PHP E-mail injunction  PHP Miscellaneous  PHP ODBC
 PHP Functions  PHP XML  PHP XML Parser  
 PHP Forms  PHP SimpleXML  PHP Zip  

 

Knowing the languages selected in the browser of our visitors may be a very important data we may used to redirect the user to a specific page in the corresponding language, or to display specific information directed to the user (as for example the most suitable ad).

The basic command we will use to know the languages selected in the browser is this one:

Request.ServerVariables("HTTP_ACCEPT_LANGUAGE")


In the table bellow we have use an script to identify the languages selected in your browser.  You may get the complete code here. The list of languages may be obtained here.
 
 

The languages selected in your browser:
English/United Satates
 

In the table bellow are displayed the different values for Request.ServerVariables("HTTP_ACCEPT_LANGUAGE") obtained from visitors to my home page.
 

Values for Request.ServerVariables("HTTP_ACCEPT_LANGUAGE")
obtained from 164 visitors to codedcode.com main page:

84 en-us 
9 en-gb
9 de
7 nl
5 en-us,zh;q=0.5
4 es
4 en
3 nl-be
3 it
3 eu,en,es
3 en-za
3 en-us,el;q=0.5
3 en-ie
2 pt-br
2 fr-ca
2 es-mx
2 en-us,hi;q=0.5
1 zu;q=0.3
1 th
1 sv
1 sl
1 ro
1 pl
1 in
1 he
1 fr-ch
1 fr
1 en-ca
1 en-au
1 en;q=0.7,
1 en,fr-CA
1 de-ch
1 bg

Redirecting visitors to lenguage specific pages

Let suppose we have our information in English and Spanish, and we will also suppose spanish is not the more used language by our visitors (our default language will be English)

By using Request.ServerVariables("HTTP_ACCEPT_LANGUAGE") we may know whether Spanish is one of the languages selected in the browser of our visitor. If it is so, we may consider to situations:

  • Spanish is the first language (p.e: Request.ServerVariables("HTTP_ACCEPT_LANGUAGE") value is "es,en")
  • Spanish is not the first langauge (p.e: Request.ServerVariables("HTTP_ACCEPT_LANGUAGE") value is "en,es")
We may consider also two other situations:
  • If Spanish is in the list, the user knows Spanish
  • The user knows Spanish only when Spanish is  the first langauge in the list
Option 1 (If Spanish is in the list, the user knows Spanish, so we will redirect the user to the page in Spanish)
 
defauldpage.asp
<% if Instr(1,Request.ServerVariables("HTTP_ACCEPT_LANGUAGE"), "es",1)>0 then
response.redirect("spanishpage.asp")
end if %>

<html> 
<title>Page into English, my defauld language</title> 
<body> 

 Page into English, my defauld language

 </body> 
 </html>

In line 1 we will check whether substring "es" is included in value for Request.ServerVariables("HTTP_ACCEPT_LANGUAGE"). Our comparation will start in character number 1 of the string, and it will not be case sensitive (see Compare strings page for explanation).

If comparation is true the user will be redirected to the page in Spanish (line 2). When redirection is performed, the rest of information in the page is not transferred to the clients browser. If the comparation is false, the remaining information in the page is send to the client.
 
 

Option 2 (Spanish is in the list, but the user only knows Spanish when this language is the first one in the list. If it is so, we will redirect the user to the page in Spanish)
 

defauldpage.asp
<% if Mid(Request.ServerVariables("HTTP_ACCEPT_LANGUAGE"), 1,2)="es" then
response.redirect("spanishpage.asp")
end if %>

<html> 
<title>Page into English, my defauld language</title> 
<body> 

 Page into English, my defauld language

 </body> 
 </html>

In line 1 we will get the two first positions for Request.ServerVariables("HTTP_ACCEPT_LANGUAGE") and we will check whether its value is "es" (see Manipulate Strings page for explanation).

If comparation is true the user will be redirected to the page in Spanish (line 2). When redirection is performed, the rest of information in the page is not transferred to the clients browser. If the comparation is false, the remaining information in the page is send to the client.
 

Showing lenguage specific ads or text

This example will work basically in the same way shown for previous example.

In this case we will assume that  if Spanish is in the list of lenguages selected in the browser, the user knows Spanish, so we will display an ad or text in Spanish. In the script bellow we will show ads:
 

languagespecificad.asp
<html> 
<title>My page</title> 
<body> 

<% if Instr(1,Request.ServerVariables("HTTP_ACCEPT_LANGUAGE"), "es",1)>0 then %>

<a href=http://www.link1.com><img scr=spanishad.gif></a>

<% else %>

<a href=http://www.link2.com><img scr=englishad.gif></a>

<% end if %>
 

 My info

 </body> 
 </html>

Code for ad in Spanish will be the red one (if comparation is true) and code for ad in English will be in magenta (if comparation is false).
In order to display text instead of ads or figures, we just need to substitute the corresponding code.

 



  

 

 
 

Copyright © http://www.itbaba.com. 2007 All Rights