Strangest thing is happening to me Sessions Variable

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
victor
Forum Commoner
Posts: 65
Joined: Fri Feb 13, 2004 1:36 am

Strangest thing is happening to me Sessions Variable

Post 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..
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

hmmm

Post 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'; ?>
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

put print_r($_SESSION);

and tell us what you get
victor
Forum Commoner
Posts: 65
Joined: Fri Feb 13, 2004 1:36 am

Post 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...
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I hope the semicolon after the function declare is just a typo.
victor
Forum Commoner
Posts: 65
Joined: Fri Feb 13, 2004 1:36 am

Post 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..?
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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.
victor
Forum Commoner
Posts: 65
Joined: Fri Feb 13, 2004 1:36 am

Post by victor »

OK get it. thks
Post Reply