Jump to content


Photo

Session vars disappear when loop is used to assign them


  • Please log in to reply
2 replies to this topic

#1 Kingskin

Kingskin
  • Members
  • PipPip
  • Member
  • 18 posts

Posted 19 July 2006 - 05:28 PM

Ok hopefully somone can help me with this as its driving me crazy!

I'm trying toi register some session variables based on db fields within a loop when a user logs in. Here is the code im using:

<?
function sqlquery($sql){
	$result = mysql_query($sql);
	$row = mysql_fetch_array($result);
	if(!$result){
		echo '<p>SQL Error: '.$sql.'</p>';
	}
	return $row;
}

function reg_session_vars($userid){
	$row = sqlquery("SELECT * FROM users WHERE userid = $userid");
	foreach($row as $key => $value){
		$_SESSION[$key] = $value;
	}
	session_write_close();
}

This seems to register the variables fine until I navigate to another page, when they are emptied. If i print_r on the login page itself, I get my $_SESSION array nicely filled, but as soon as I navigate away it's gone.

If i register sessions like this:
$_SESSION['userid'] = $userid;
$_SESSION['username'] = $username;
$_SESSION['first_name'] = $first_name;
$_SESSION['last_name'] = $last_name;
etc...

It works fine. Why is it my loop isn't working properly, and can anyone help me populate $_SESSION from a db table with a loop please? I'd rather it was dynamic than be having to add & remove lines if the table structure changes.

#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 19 July 2006 - 05:53 PM

Code looks fine to me, and I cant see nout wrong with it. However i'd suggest you use mysql_fetch_assoc rather than mysq_fetch_array, otherwise you'll get duplicate sessions, one with a numerical key and another with a string key eg: for your userid session, you'll get userid sessions like so:
$_SESSION[0] and $_SESSION['userid']
With mysql_fetch_assoc this wont happen.

Another suggestion which may not help but chnage this:
$_SESSION[$key] = $value;
to:
$_SESSION[{$key}] = $value;

Also make sure you have session_start(); as the first line for everypage that use's sessions.

#3 Kingskin

Kingskin
  • Members
  • PipPip
  • Member
  • 18 posts

Posted 19 July 2006 - 05:59 PM

Code looks fine to me, and I cant see nout wrong with it. However i'd suggest you use mysql_fetch_assoc rather than mysq_fetch_array, otherwise you'll get duplicate sessions, one with a numerical key and another with a string key eg: for your userid session, you'll get userid sessions like so:
$_SESSION[0] and $_SESSION['userid']
With mysql_fetch_assoc this wont happen.

Another suggestion which may not help but chnage this:

$_SESSION[$key] = $value;
to:
$_SESSION[{$key}] = $value;

Also make sure you have session_start(); as the first line for everypage that use's sessions.


cheers dude I had notices that duplicate session data which you mentioned, was going to be my next question! :D

The suggestion regarding the curly braces actually resulted in an error, however it seems that changing to mysql_fetch_assoc seems to have cleared the problem anyway! I remember reading that session keys can't be numbers, so this must be the cause.

Thanks for the help, problem solved :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users