Forum Moderators: coopster

Message Too Old, No Replies

Sessions, again

Full source code included

         

Infant89

5:07 am on Jun 10, 2009 (gmt 0)

10+ Year Member



Okay, I had a problem with variables and sessions and nobody seemed to know what was wrong so I'm including the entire source code so you can run it and see for yourself:

------------
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.

Infant89

4:48 pm on Jun 10, 2009 (gmt 0)

10+ Year Member



I'm beginning to think that this might be some PHP bug or it might have something to do with my host's servers because sometimes it works and then 5 minutes later it doesn't. This is making me so frustrated I wanna sue somebody!

max4

5:21 pm on Jun 10, 2009 (gmt 0)

10+ Year Member



Hi Infant89,

It would appear that you have an undefined variable at the top of your home.php script:

[fixed]
$language = $_SESSION['lang'];
[/fixed]

The variable '$language' is being defined in the language.php script. You'll have to define it in home.php if you wish to use it in that page.

Infant89

6:51 pm on Jun 10, 2009 (gmt 0)

10+ Year Member



I don't see how that matters. I'm having problems with the $session variable. The if ($loggedIn == null) is always run because I haven't made any login so the variable you mentioned isn't even used in the home.php. Why do you say it's undefined in home.php? I use it the same way in both scripts and as far as I know you don't have to explicitly define variables in PHP, unlike conventional programming languages. When I used it in language.php I didn't define it specifically.

max4

7:01 pm on Jun 10, 2009 (gmt 0)

10+ Year Member



The way your script is set up, the only way you will not have errors upon visiting home.php is if $_SESSION['lang'] is set. If a user visits home.php after freshly opening his/her browser, they will receive errors because you are trying to define $language from the data carried within a session that is not set. So you are correct, the problem is indeed with the way sessions are handled between your two scripts.

jatar_k

7:02 pm on Jun 10, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



why not dump the whole session and see what's in there?

echo '<pre>';
print_r($_SESSION);
echo '</pre>';

that way you can watch what happens as you are going along

I would dump it top and bottom to see what changes through out the script

max4

7:31 pm on Jun 10, 2009 (gmt 0)

10+ Year Member



Forgive me, I was mistaken. I will look at your script in greater detail and see what I can come up with.

Infant89

7:40 pm on Jun 10, 2009 (gmt 0)

10+ Year Member



I've done a lot of testing jatar_k. I've been trying to get this to work for over a week. I've tried putting strings literally everywhere in my code to monitor the control flow. The session doesn't seem to remain in memory when the page is refreshed. puff... gone...

max4

8:12 pm on Jun 10, 2009 (gmt 0)

10+ Year Member



Okay, by what you've said and from your script the order of events is thus:

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.

Infant89

9:04 pm on Jun 10, 2009 (gmt 0)

10+ Year Member



Why would the session be lost in general? Isn't $_SESSION['lang'] equals whatever enough to set the session? I've been looking at a lot of session tutorials and they all seem to indicate that all you have to do is, say, $_SESSION['userID'] = "infant89" and then it'll remain. And so you should be able to do something like $myVar = $_SESSION['userID'] and get the value into $myVar. But in my case $_SESSION['lang'] is not set when the page is refreshed so $session is empty.

Infant89

9:05 pm on Jun 10, 2009 (gmt 0)

10+ Year Member



That is to say, I can manually set it, but it will have "forgot" by the time I've refreshed.

Infant89

10:54 pm on Jun 10, 2009 (gmt 0)

10+ Year Member



I've contacted my host telling them to fix it or I'll stop doing business with them. That ought to do!

max4

12:06 am on Jun 11, 2009 (gmt 0)

10+ Year Member



$_SESSION['userID'] = "infant89"; is valid and would work. But setting $_SESSION['userID'] = $var would not work unless the variable was defined. If $var is undefined then $_SESSION['userID'] = $var is the same as $_SESSION['userID'] = undefined.

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.

jatar_k

2:20 pm on Jun 11, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



some issues with the code

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 :)