Page 1 of 1

Peculiar Session Issue

Posted: Wed Sep 17, 2003 10:10 am
by ryanwpatterson
I'm a Java developer who has FINALLY made my way into the realm of PHP and I am having a seemingly strange issue regarding session variables. Basically, I have created a simple controller class that redirects a user without setting http params by outputting javascript to change the page location. The code goes something like this:

//------------------------------------------>
session_start();

if (!session_is_registered('user_id')) {
SessionController::Redirect('login.php'); // send to the login page
}
else {
$bar = 'bar';
session_register('foo');
$_SESSION['foo'] = $bar;
}
//------------------------------------------>

This seems simple enough to me, but it appears that the session variable (foo) is being created and set to $bar even if the user is redirected to the login page. I even put some output statements in the above "else" statement, and those DID NOT execute, but the "session_register()" call apparently did. Is this a bug in PHP or are all session variables registered on a page implicitly even if the code does not execute?

Much thanks in advance for any tips. I have been stumped on this one for longer than I'd like to admit.

Posted: Wed Sep 17, 2003 10:37 am
by SantaGhost
i would not rely on session_register and session_is_registered since they are deprecated (http://nl2.php.net/manual/en/function.s ... gister.php)

try the following:

Code: Select all

<?php
//------------------------------------------>
session_start();

if (!isset($_SESSION['user_id'])) {
	header("Location: login.php"); // send to the login page
}else {
	$bar = "bar";
	$_SESSION['foo'] = $bar;
}
//------------------------------------------> 
?>

Posted: Wed Sep 17, 2003 10:59 am
by ryanwpatterson
Thanks for the tip. So, session variables can be set on the fly without registration via $_SESSION['my_var'] = ...?

Posted: Wed Sep 17, 2003 11:43 am
by SantaGhost
sessions need to to started if $_SESSION is used. variables can be registered on the fly using $_SESSION["var"] = "myvar"
If session_start() was not called before this function is called, an implicit call to session_start() with no parameters will be made. $_SESSION does not mimic this behavior and requires session_start() before use.

Posted: Wed Sep 17, 2003 3:35 pm
by m3rajk
hehe. one thing i like on php over java: no need for prior initialization

although sometimes i fall back on it just out of habit and others out of being able to track code easier

Posted: Wed Sep 17, 2003 6:10 pm
by JAM
SantaGhost wrote:sessions need to to started if $_SESSION is used. variables can be registered on the fly using $_SESSION["var"] = "myvar"
He is using that.
m3rajk wrote:hehe. one thing i like on php over java: no need for prior initialization

although sometimes i fall back on it just out of habit and others out of being able to track code easier
Noone forces you to use php, per se. Code a e-commerce sites with Java instead.

Point is, there is allways pro's/con's with everything...

Posted: Thu Sep 18, 2003 2:08 pm
by m3rajk
i know no one is forcing it. but php is adapted to this purpose. java isn't meant forit. besides, like i said, there's little things i do that are because of learning java, but a lot of things i like about php. for this purpose php is the far superior tool