Forum Moderators: coopster
------------
home.php
------------
<?php
session_start();
$loggedIn = $_SESSION['userID'];
if ($loggedIn == null)
include("language.php");
else
$language = $_SESSION['lang'];
?>
<html>
<body>
<p>Session: <?php echo $session; ?></p>
<p>_SESSION: <?php echo $_SESSION['lang']; ?></p>
<p>Cookie: <?php echo $cookie; ?></p>
<p>_COOKIE: <?php echo $_COOKIE['lang']; ?></p>
</body>
</html>
------------
language.php
------------
<?php
$session = $_SESSION['lang'];
$cookie = $_COOKIE['lang'];
$server = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
if ($session != null)
{
$language = $session;
}
elseif ($cookie != null)
{
$language = $cookie;
}
elseif ($server != null)
{
$languageArray = explode(",", $_SERVER["HTTP_ACCEPT_LANGUAGE"]);
$lang == "none";
foreach ($languageArray as $value)
{
if ($language != "none")
{
$langArray = substr($value, 0, 2);
switch ($langArray)
{
case "de":
$language = "en"; // should be "de"
break;
case "en":
$language = "en";
break;
case "es":
$language = "en"; // should be "es"
break;
case "fr":
$language = "en"; // should be "fr"
break;
case "is":
$language = "en"; // should be "is"
break;
}
}
if ($language == "none")
$language = "en";
}
}
else
{
$language = "en";
}
$_SESSION['lang'] = $language;
$_COOKIE['lang'] = $language;
?>
------------
expected results (with my language settings)
------------
Session: is
_SESSION: is
Cookie: is
_COOKIE: is
------------
actual results (with my language settings)
------------
Session:
_SESSION: is
Cookie: is
_COOKIE: is
------------
conclusion
------------
The 'lang' session never seems to be set because the session variable is always null. I asked a mate and had him read my code and he told me he'd had the same problem when he once tried to make a language selection script for his website and he was never able to resolve it. I hope I'm not doomed.
1) Check login
2) Login fails - Open language.php
3) Define $session as $_SESSION['lang']
For number 3 to check out, $_SESSION['lang'] would have to already be set or your script will fail when $session is used. Since it seems that language.php is called after a failed login check at home.php; it would then be logical to set $_SESSION['lang'] to a usable value at home.php so there is no risk of losing the session when language.php is included.
To be 100% certain of this; I just tried this on my development server with full error reporting. I created a session as follows:
[fixed]
$_SESSION['var'] = $var;
[/fixed] Where $var was not previously defined and $_SESSION['var'] was not previously set. I received the following error:
[fixed]
Notice: Undefined variable: var in ...
[/fixed] I'm not sure why you're losing your sessions though. As far as I know, sessions should not disappear when a page is refreshed; only when the browser is closed and opened again.
you test continuously for $language != "none" but you never set it to none, what is that comarison supposed to be?
I see this higher up
$lang == "none";
my guess is you mean
$language = "none";
you also test
$somevar != null
my guess is all those null comparisons will all evaluate to true, you should test what is actually returned if the var you are trying to read into $session,$cookie and $server is unset
why not use an explicit !is_null?
why not try empty? or !isset directly on the superglobal var.
If I could also offer a small piece of advice. A better way to ensure that your hosting company enjoys dealing with you is to not give them ultimatums, especially when I am not thinking this is anything they did. Even if they do screw up the odds you'll get good service in a continued manner is rare if you threaten. just a thought :)