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
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);

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.