Jump to content

Session Variables with a variable


nertskull

Recommended Posts

How can I set session variables using a function and variable names?

 

For example, this doesn't work for me, and I don't know why.

function validate_User()
{
session_regenerate_id ();
$_SESSION['open'] = 1;
$_SESSION['userid'] = $userid;
$_SESSION['username'] = $username;
}


$row = mysql_fetch_array($result, MYSQL_ASSOC);
$userid = $row['userid'];
$username = $row['username'];

validateUser();

 

the $result is a query that returns just one row based on the unique username

 

Its my first time trying to use a function, so I'm sure I'm misunderstanding.  But I thought that since I call the "validateUser()" after I defined username and userid that it would work.  And assign those to the sesssion variable.  But its not working for me.

 

Nothing gets set to the $_SESSION[] variables (well, other than $_SESSION['open'], that gets set to 1 like it should)

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/207939-session-variables-with-a-variable/
Share on other sites

$userid and $username are not available within your function. You'll need to pass them in as arguments.

 

function validate_User($userid, $username) {
  session_regenerate_id ();
  $_SESSION['open'] = 1;
  $_SESSION['userid'] = $userid;
  $_SESSION['username'] = $username;
}


$row = mysql_fetch_array($result, MYSQL_ASSOC);
$userid = $row['userid'];
$username = $row['username'];

validateUser($userid, $username);

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.