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.
Peculiar Session Issue
Moderator: General Moderators
-
ryanwpatterson
- Forum Newbie
- Posts: 6
- Joined: Wed Sep 17, 2003 10:10 am
- Location: Austin, TX
- SantaGhost
- Forum Commoner
- Posts: 41
- Joined: Mon Sep 15, 2003 11:54 am
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:
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
- SantaGhost
- Forum Commoner
- Posts: 41
- Joined: Mon Sep 15, 2003 11:54 am
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.
He is using that.SantaGhost wrote:sessions need to to started if $_SESSION is used. variables can be registered on the fly using $_SESSION["var"] = "myvar"
Noone forces you to use php, per se. Code a e-commerce sites with Java instead.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
Point is, there is allways pro's/con's with everything...