Jump to content

Session vars disappear when loop is used to assign them


Kingskin

Recommended Posts

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:

[code]
<?
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();
}
[/code]

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:
[code]
$_SESSION['userid'] = $userid;
$_SESSION['username'] = $username;
$_SESSION['first_name'] = $first_name;
$_SESSION['last_name'] = $last_name;
etc...
[/code]

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.
Link to comment
Share on other sites

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:
[code]$_SESSION[$key] = $value;[/code]
to:
[code]$_SESSION[{$key}] = $value;[/code]

Also make sure you have session_start(); as the first line for everypage that use's sessions.
Link to comment
Share on other sites

[quote author=wildteen88 link=topic=101127.msg399930#msg399930 date=1153331611]
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:
[code]$_SESSION[$key] = $value;[/code]
to:
[code]$_SESSION[{$key}] = $value;[/code]

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

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 :)
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.