Jump to content

[SOLVED] Nested function within declaration


drummer101

Recommended Posts

<?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"';?>

Link to comment
Share on other sites

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;
}
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

?>

 

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.