Jump to content

A better session data storage method??


AbydosGater

Recommended Posts

Hi,...
In my login script, if the user logs in correctly with the right information.. I set the session variables with the code below.. And send them to the members.php...

[quote]
<?php
session_register("user");
$_SESSION['username'] = $username;
$_SESSION['password'] = $password;
//-- Rest Of Info On The User
$_SESSION['member_id'] =  $user['member_id'];
$_SESSION['email'] =  $user['email'];
$_SESSION['CL'] =  $user['CL'];
$_SESSION['banned'] =  $user['banned'];
//-- Session Variables End
echo "<META HTTP-EQUIV=\"refresh\" CONTENT=\"0; URL=members.php\">";
//Leave out the opening and closing tags, i only put them in here so the code is in colour...
?>
[/quote]

I think its silly that i have so many session vars...
Is there any way to set it in an associtave array? so like the script has the $user.. I just have to $user['db_field_name']; for the info..
How would i set it so the session saves an array so i would have to call say.. for example the username..

$_SESSION['user']['username'];

Is this possible?

Thanks Abydos
Link to comment
https://forums.phpfreaks.com/topic/28099-a-better-session-data-storage-method/
Share on other sites

Try it out! Make a test page and try if you can assign and get the variables...

[code]
<?php
session_start();

$_SESSION['user'] = array(
  'name' => 'myName',
  'email' => 'emailAddress'
)

echo '<pre>';
print_r($_SESSION['user']);
echo '</pre>';

?>
[/code]
aah... the wonder of arrayed arrays...

[code]$result = mysql_query("SELECT * FROM users WHERE `username`='$username' AND `password`='$password' limit 1");
$_SESSION[user]= mysql_fetch_array($result);[/code]

then you can access vars like $_SESSION[user][username]

personally... i dont use anything but arrayed arrays for holding user info.
As cheesier said, you're better off just storing minimal information (username or user_id) in the session and querying for it on every page.  If you don't do that, you have to remember to update variables in $_SESSION every time you allow the user to update their personal information which can become a PITA.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.