drummer101 Posted November 24, 2007 Share Posted November 24, 2007 <?php function getpassword(){ formresults(); $sql = 'SELECT phpbb_users.user_password FROM phpbb_users WHERE phpbb_users.username = "$username"';?> formresults() contains <?php $username = $_POST['username']; ?> Currently getting Notice: Undefined variable: username in D:\Apache2\htdocs\includes\****\functions.php formresults() is declared before getpassword(). Is it possible to nest a function within another form declaration? or will I have to change the code to <?php function getpassword(){ $username = $_POST['username']; $sql = 'SELECT phpbb_users.user_password FROM phpbb_users WHERE phpbb_users.username = "$username"';?> Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted November 24, 2007 Share Posted November 24, 2007 You'll have to return $username from the formresults(); function. Functions have their own variable scope. Quote Link to comment Share on other sites More sharing options...
drummer101 Posted November 24, 2007 Author Share Posted November 24, 2007 You'll have to return $username from the formresults(); function. Functions have their own variable scope. Just to clarify, you're saying that I'll have to redeclare the variable inside of the function, instead of calling func2 (which contains the variable) inside of func2? Quote Link to comment Share on other sites More sharing options...
rlindauer Posted November 24, 2007 Share Posted November 24, 2007 You have to return the value of username. Functions do not automatically return the variables you create from within the function. Those variables are a part of the functions scope, and cannot be used outside it without explicitly returning it. <?php function getpassword(){ $username = formresults(); $sql = 'SELECT phpbb_users.user_password FROM phpbb_users WHERE phpbb_users.username = "$username"'; } function formresults(){ $username = $_POST['username']; // you have to return the variable if you wish to use it outside of this function return $username; } ?> Quote Link to comment Share on other sites More sharing options...
drummer101 Posted November 24, 2007 Author Share Posted November 24, 2007 okay, it's all slowly falling together now. Can you return two variables from within a function, i.e. a username and password? <?php function formresults(){ $username = $_POST['username']; $password = md5($_POST['password']); return $username; return $password; } function getpassword(){ $username = formresults(); $password = formresults(); $sql = 'SELECT phpbb_users.user_password FROM phpbb_users WHERE phpbb_users.username = "$username"'; } ?> thats my code as of right now, and $username is returned correctly, however, when I go to call $password, it returns as $username. Quote Link to comment Share on other sites More sharing options...
toplay Posted November 24, 2007 Share Posted November 24, 2007 Not directly as you think, however, you can return an array or an object that could contain more than one variable/value. You're making it more complicated than it needs to be. Since the $_POST superglobal array variable has global scope, the getpassword() function itself can access the $_POST values. But a function to get a password should just have an argument passed to it like the username and that's used in the query. Then the password if retrieved can be returned by the function. If you're building a function to not really get the password but to verify it, then pass the username and password to a function and call it something like verifyPassword. Think about what you're trying to do exactly. Example of getting a user's password when a username is known/passed: <?php function getPasswordForUser($strUsername) { // escape $strUsername before using in query // Use double quotes if you want $strUsername value to placed correctly in query $sql = "SELECT `user_password` FROM phpbb_users WHERE `username` = '$strUsername' "; // Open DB connection, select DB, do query, check results // When row of data found, return the password return $row['user_password']; // just an example } // Calling getPasswordForUser() example $postUsername = isset($_POST['username']) ? $_POST['username'] : ''; if (!empty($postUsername)) { $strUserPassword = getPasswordForUser($postUsername); } else { // no password passed $strUserPassword = ''; } ?> Example to verify a username and password are valid: <?php function verifyUserPassword($strUsername, $strPassword) { $strPassword = md5($strPassword); // escape $strUsername before using in query // Use double quotes if you want variable values to placed correctly in query $sql = "SELECT `user_password` FROM phpbb_users WHERE `username` = '$strUsername' AND `user_password` = '$strPassword' "; // Open DB connection, select DB, do query, check results // When row of user data found, return a true/false value return $blnFound; // just an example - return a boolean value, true if row found and false otherwise } // Calling verifyUserPassword() example $postUsername = isset($_POST['username']) ? $_POST['username'] : ''; $postPassword = isset($_POST['password']) ? $_POST['password'] : ''; if (!empty($postUsername) && !empty($postPassword)) { $blnValidUser = verifyUserPassword($postUsername, $postPassword); } else { // no username and/or password passed $blnValidUser = FALSE; } ?> Quote Link to comment Share on other sites More sharing options...
drummer101 Posted November 24, 2007 Author Share Posted November 24, 2007 Okay, that makes sense. Thanks so much. 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.