Code: Select all
andCode: 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"); ?>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
}
?>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>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");
?>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
andCode: 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]