stergosz
stergosz

Reputation: 5860

Saving user language in website

I am having multiple languages in my website and i would like to know whcih is the best approach to save the user selected language when browsing my website.

I thought of adding cookies and checking if the language exists and if so, show it, otherwise show the default one.

My other though was using sessions, but this wont stay long since sessions expire too soon.

Do you have any better approach? Thanks.

Upvotes: 1

Views: 171

Answers (4)

longstaff
longstaff

Reputation: 2051

i would suggest putting a suffix or prefix on your page strings that the php can pull out and set the language as neccessary (such as www.somewhere.com/en/pagename) so that you can allow bookmarking/posting etc. it should also help seo.

-Edit to add code:

here is an example code for the .htaccess:

# rewrite all requests for language-specific files
RewriteEngine on
Options +FollowSymlinks
RewriteRule ^en/(.*)$ $1?lang=en [NC,L]
RewriteRule ^de/(.*)$ $1?lang=de [NC,L]
RewriteRule ^fr/(.*)$ $1?lang=fr [NC,L]

Upvotes: 1

Canser Yanbakan
Canser Yanbakan

Reputation: 3870

If you want to register your site to the search engines, you should make your links attainable. When the robots visited your page, they are not able to save cookies or session variables so they can't reach your other different language pages.

But if you dont, you can use cookies to save their choises.

http://tr.php.net/manual/en/function.setcookie.php

Upvotes: 0

Andreas Eriksson
Andreas Eriksson

Reputation: 9027

I'd definitely go with cookies - sessions expire just as soon as you close the browser, and are thus unsuitable.

Using the IP-address can yield very bad results because of dynamic addresses and whatnot.

The best thing would be to tie the language to a user account in a database, but this, of course, won't work if your website doesn't have any sort of accounts for users.

Upvotes: 0

Cydonia7
Cydonia7

Reputation: 3826

Cookies are a good way in you don't have any authentication on your application.

Upvotes: 1

Related Questions