language selection cookie

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

fapps
New php-forum User
New php-forum User
Posts: 3
Joined: Fri Apr 26, 2013 5:26 am

language selection cookie

Postby fapps » Fri Apr 26, 2013 5:57 am

perhaps my question is silly and very easy to solve but i´m very new in php and cand find the solution. I apologize in advance

I have a 3 languages (catalan, spanish, and english) web in subfolders (default one is catalan) and want to set a cookie to remember the visitors language and redirect them to the proper one the next visit. I have this code:

index.php (in catalan, default language):

<?php $lang=$_COOKIE["idioma"];
switch ($lang) {
case 'es':
header('Location:es/index-esp.php');
break;
case 'en':
header('Location:en/index-eng.php');
break;
}
?>
<html> ......


all the other pages : ("ca" for catalan ones, "es" for spanish ones and "en" for english)

<?php
setcookie("idioma", "es", time()+94608000);
?>
<html> ......


It doesn´t work. don´t generate any cookie when I change the language and every time I load the page is always in catalan (index.php)

thanks a lot

seandisanti
php-forum Fan User
php-forum Fan User
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: language selection cookie

Postby seandisanti » Fri Apr 26, 2013 8:14 am

Do you have cookies enabled? is your code generating any errors?

fapps
New php-forum User
New php-forum User
Posts: 3
Joined: Fri Apr 26, 2013 5:26 am

Re: language selection cookie

Postby fapps » Fri Apr 26, 2013 8:59 am

I have enabled "cookies from everywhere". In the browser cookies list there are lots of cookies from many pages but not mine.

I´ve just made a test with index.php, idex-es.php, and only one sub-page in each language and don´t give me any error, but always start in catalan
(I´m doing it all in my localhost. I have the web in the net in just one language yet, www.ktrens.com
I would like to solve the language issue before uploading all.)

seandisanti
php-forum Fan User
php-forum Fan User
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: language selection cookie

Postby seandisanti » Fri Apr 26, 2013 11:51 am

just for laughs, echo out $_COOKIE['idioma'] and make sure that it's returning the right value

fapps
New php-forum User
New php-forum User
Posts: 3
Joined: Fri Apr 26, 2013 5:26 am

Re: language selection cookie

Postby fapps » Fri Apr 26, 2013 1:16 pm

thank you seandisanti,

I´ve solved it..
there was a problem in the localhost direction.
(I´ve also added a "/" parameter in the cookie function)
it seems it works fine now.

seandisanti
php-forum Fan User
php-forum Fan User
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: language selection cookie

Postby seandisanti » Fri Apr 26, 2013 2:33 pm

good job!


Return to “PHP coding => General”

Who is online

Users browsing this forum: Google [Bot] and 3 guests

cron