nertskull Posted July 16, 2010 Share Posted July 16, 2010 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 Quote Link to comment Share on other sites More sharing options...
Wolphie Posted July 16, 2010 Share Posted July 16, 2010 You should learn about http://php.net/manual/en/language.variables.scope.php Quote Link to comment Share on other sites More sharing options...
trq Posted July 16, 2010 Share Posted July 16, 2010 $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); Quote Link to comment Share on other sites More sharing options...
nertskull Posted July 16, 2010 Author Share Posted July 16, 2010 Thanks!! That works much better. And thanks for the link to the other site, it helped clarify some things as well. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.