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 Link to comment https://forums.phpfreaks.com/topic/207939-session-variables-with-a-variable/ 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 Link to comment https://forums.phpfreaks.com/topic/207939-session-variables-with-a-variable/#findComment-1087000 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); Link to comment https://forums.phpfreaks.com/topic/207939-session-variables-with-a-variable/#findComment-1087002 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. Link to comment https://forums.phpfreaks.com/topic/207939-session-variables-with-a-variable/#findComment-1087133 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.