Peculiar Session Issue

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ryanwpatterson
Forum Newbie
Posts: 6
Joined: Wed Sep 17, 2003 10:10 am
Location: Austin, TX

Peculiar Session Issue

Post 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.
User avatar
SantaGhost
Forum Commoner
Posts: 41
Joined: Mon Sep 15, 2003 11:54 am

Post 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;
}
//------------------------------------------> 
?>
ryanwpatterson
Forum Newbie
Posts: 6
Joined: Wed Sep 17, 2003 10:10 am
Location: Austin, TX

Post by ryanwpatterson »

Thanks for the tip. So, session variables can be set on the fly without registration via $_SESSION['my_var'] = ...?
User avatar
SantaGhost
Forum Commoner
Posts: 41
Joined: Mon Sep 15, 2003 11:54 am

Post 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.
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post 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
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post 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...
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post 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
Post Reply