Main Menu




browser lang:en

Users Area

Client Login

today cache size is:0

 

 


Comments

fg
16/04/2011 by fdg
Hi all
13/02/2011 by
John
24/07/2010 by John
better
16/04/2010 by bob
attached files not working !
05/11/2009 by Adnan
Tuvok
14/02/2009 by
Nice script
07/02/2009 by desaj
Other solutions
07/02/2009 by Mike

Read More »


RAPIDQ

Rapid-Q:The lost files
My old library where i have collected all the rapidq scripts and italian help


Today my pagerank is: Free Page Rank Tool
Upbooking - free booking engine

Redirecting visitors by browser language - Part 1

Learn php/walkthrough

One of the common feature of many websites is the "choose language" option, usually indicated with a list of flags.. so, with php is possible to give a professional touch to your web site letting php choosing which is your visitors' language as the choice is based on the default browser language used at that moment.

In this case is useful to create a function to speed up your site building or mantainance, simply calling the function as often as needed. But first of all let's examine wich parameter we have to deal with.

Open up your favourite php editor program and type


echo $_SERVER['HTTP_ACCEPT_LANGUAGE'];


remember any time to close the code between the tags
I've tested this echo in IE 6 and Firefox. If you still don't know how affortable is a browser like Firefox, get Firefox and try yourself.

IE gives this string as result of the echo

en,it;q=0.5

The same php code is Firefox gives this:

en,it;q=0.8,it-it;q=0.5,en-us;q=0.3

Pay attention at the first parts of this two strings, they start with en, the rest as ,it show the accept language at the second place, but wait a moment to see how to work with this. Now all we need is a php function that can get the first and the second letter of the string, we're going to use substr().
The first parameter of substr is the $_SERVER['HTTP_ACCEPT_LANGUAGE']; the one that returns the string already seen.

Now try

$lang = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
echo $result = substr($lang, 0, 2);
// $lang is the variable, 0 is the point where start to count as the beginnig
// and 2 is where to stop trimming the string, so only two letters.


So we obtain:

This code gives en as result, all that we wanted.
Well, to speed up our code let's create a function. If you're familiar with C, there's nothing new in php in creating a function. Every function must be declared typing function and must have a name, followed by (), that may contain one or more variables. In this case there are no variables. The fuction code must be closed with { and }

function yourfunctionname($optionalparameter)
// NOT use ; or it won't work to end this line code
{
//your code
}


Now let's see our function..


function comefrom()
{
$lang = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
if (substr($lang, 0, 2) == 'en')
{
// if the user's browser language is set to english
// do something
} else {
// if the user's browser language is NOT set to english
// do something else
}
}


The //do something comment may be replaced with everything you want, one of the feature may be the automatic redirection to the user's page language

function comefrom()
{
$lang = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
if (substr($lang, 0, 2) == 'en')
{
header("Location: english.php");
} else {
header("Location: french.php");
}
}


english.php and french.php are two pre-made pages with content thought for different users. header() is a php function that makes the redirect to the "Location: .... " indicated. Now, let's seen how to verify the correct working of our function with different languages Go to the second page


Comments

Insert your comment

Titolo
Messaggio
Nome Utente
e-mail (se vuoi ricevere le risposte a questo post anche via mail)

Users

Videos


Scripts & Tutorials

50 necessaries php tools

25/02/2011 

PHP is one of the most widely used open-source server-side scripting languages that exist today. With over…

in:Scripts and tutorials (0 comments)

FBJS Quick Jump Menu for a FBML Facebook Platform App

15/07/2010 

In a FBML Facebook App, your quick jump menu will require a little tweak to work in FBJS…

in:Scripts and tutorials (0 comments)

How to manage your online reputation, free tools forcommunity managers

02/07/2010 

Every single day, someone, somewhere is discussing something important to your business; your brand, your executives, your…

in:Scripts and tutorials (0 comments)

PHP - The Singleton Pattern

26/11/2009 

The Singleton Pattern is one of the GoF (Gang of Four) Patterns. This particular pattern provides a…

in:Scripts and tutorials (0 comments)

PHP - calculating distance between two points

20/10/2009 

Because of the near-spherical shape of the Earth, calculating an accurate distance between two points requires the use…

in:Scripts and tutorials (1 comments)

PHP - Download file with speed limit

20/10/2009 

With this script we can limit the download speed   // local file that should be send to the client $local_file…

in:Scripts and tutorials (0 comments)

PHP - Save remote images on our server using CURL

12/10/2009 

Some hosts disabled the ini setting allow_url_fopen. This also means that the ability to easily grab images…

in:Scripts and tutorials (0 comments)

PHP - verify file existence in a local server

11/06/2009 

The act to verify if a file exists, is one of more important tasks related to files operations,…

in:Scripts and tutorials (0 comments)

Read more »


Tag Clouds


necessaries toolsFBJS Quick Jump Menu FBML Facebook Platform AppHow manage your online reputation free tools forcommunity managersPHP Singleton PatternPHP calculating distance between points Download file speed limitPHP Save remote images


Add to Technorati Favorites