Page 1 of 1

Set cookie for user language

Posted: Mon Feb 13, 2006 10:31 am
by ofanged1
Jcart | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


Im trying to use cookies to remember the users language.
base site is in http://www.domain.org
spanish site in http://www.domain.org/es
in my header.php page I have an include:

Code: Select all

<?php include("inc/lang.php"); ?>
which consists of

Code: Select all

<?php
// Check For Cookie
if (isset($_COOKIE['domain']) && $_COOKIE['domain']!='/') {
	$lang = $_COOKIE['domain'];
	$host = $_SERVER['HTTP_HOST'];
	$uri  = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
	header("Location: http://$host$uri$lang");
} else if (!isset($_COOKIE['domain'])) {//If No Cookie Set One
	setcookie("domain", "/", time() + 30*24*60*60, "/", '.domain.org', 1);
        //if no lang preference yet send them to root and set cookie with root as lang
}
?>
I think this would check for a cookie and append the value of the cookie to domain.org ie. domain.org/ or domain.org/es using header only if redirect is needed.

in my header.php I allow the user to choose a language once the page loads with:

Code: Select all

<form name="lang" action="setLang.php" method="post">
			<select name="lang">
				<option value="/es">Espanol</option>
				<option value="/">English</option>
			</select>
			<input name="submitLang" type="submit" value="Go">
			</form>
and setLang.php consists of:

Code: Select all

<?php
$lang=$_POST['lang'];
$host  = $_SERVER['HTTP_HOST'];

if (isset($_COOKIE['domain'])) {
	$oldLang=$_COOKIE['domain'];
	setcookie ("domain", $oldLang, time() - 3600);
	setcookie ("domain", $oldLang, time() - 3600, "/", ".domain.org", 1);
}

setcookie("domain", $lang, time() + 30*24*60*60, "/", '.domain.org', 1);
header("Location: http://$host/$lang");
?>
this i imagine would first check for an existing cookie, remove it. then set a new cookie with the variable lang that the header form passed. finally, redirect using header and the var that the cookie was set with.

1. Is a cleaner way to do this?
2. Upon initial visit I get the english page in root and a cookie is set with "/". If i click "Go" with espanol selected I get to the index in "/es". If i type in the root url i do not get redirected to the "/es" site i end up in "/". Help!
3. Once at the index in "/es" i use the same form code except it posts to "../setLang.php". the cookie is reset to "/" although I stay at the "/es" index page.

I really hope this is clear to whomever reads it, and i apologise for its length.
in advance thanks to all help i may recieve.


Jcart | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Posted: Mon Feb 13, 2006 10:44 am
by feyd
your cookies will not set. Reason: you've set them to secure mode, but do not run under secure (https) mode for the website.

The old cookie removal has no effect as you're overwriting the cookie output with the new cookie.

For future reference, setting the cookie back only an hour may not delete the cookie on the user's machine. It's advisable to set in back much farther in time, like a month.

Posted: Mon Feb 13, 2006 11:51 am
by ofanged1
i dont know what you mean by the cookie wont set.
i see it in the temp int files folder. with a 1 or a 0, and yes the site does have a ssl cert.
I want the site to beable to be spansih or english in ssl and plain text