Page 1 of 1
Strangest thing is happening to me Sessions Variable
Posted: Tue Sep 14, 2004 9:42 pm
by victor
I have declared a session variable i.e.
Code: Select all
<?php
$_SESSION['string'] = 'baby';
?>
on one page, however, the session variable doen't seem to register or stay persistent on other pages, after much trial and frustration, i decided to declare it in the previous page, guesse what, the variable is registered....!(By the way, session_start() has been added on all pages. This is not my first experience in this incident, anyboby ever came across the same thing? pls help..
hmmm
Posted: Tue Sep 14, 2004 9:58 pm
by neophyte
I've never had this problem before I can only guess.
Are you redefining the variable? Over writing like:
Code: Select all
<?php $_SESSION['string'] = 'momma''s baby'; ?>
Posted: Tue Sep 14, 2004 10:24 pm
by John Cartwright
put print_r($_SESSION);
and tell us what you get
Posted: Tue Sep 14, 2004 10:52 pm
by victor
the culprit has been found, once the variable is declared after the function check_valid_user() the problem starts..
Code: Select all
<?php
check_valid_user();
$_SESSION['string'] = 'baby';
//the function
function check_valid_user();
// see if somebody is logged in under the same session, notify them if not
{
global $_SESSION;
if (isset($_SESSION['user']))
{
echo 'Logged in as '.$_SESSION['user'].'.';
echo '<br><br>';
}
else
{
// they are not logged in
do_html_heading('Problem:');
echo 'You are not logged in.<br><br>';
do_html_footer();
exit;
}
}
?>
still don't know why...
Posted: Tue Sep 14, 2004 11:12 pm
by John Cartwright
What's the problem, I see you setting the string but you are not outtputting $_SESSION['string'] anywhere on the page
and global $_SESSION; is useless because it is already a global...
I also do not see session_start() anywhere on the page, and you might have a problem because you are calling the functions before you are declaring it
Posted: Tue Sep 14, 2004 11:15 pm
by feyd
I hope the semicolon after the function declare is just a typo.
Posted: Tue Sep 14, 2004 11:33 pm
by victor
the function has been included.
Code: Select all
<?php
require_once('../user_sc_fns.php');
session_start();
check_valid_user();
$_SESSION['string'] = 'baby';
echo $_SESSION['string']; //nothing..printed
?>
when i removed global from the function , everything works fine, why is this so..?
Posted: Wed Sep 15, 2004 12:21 am
by Weirdan
de_php at amano7 dot org wrote:
if you use global with $_SESSION, it won't work.
The vars you put in $_SESSION won't be stored in the area where you use global with it.
So it's "it crashes if you do so" instead of "you don't have to" on that topic.
Posted: Wed Sep 15, 2004 12:46 am
by victor
OK get it. thks