从函数中返回值

时间:2016-08-30 20:15:30

标签: php function

我已经定义了一个检查用户凭据的函数,并希望在auth通过时返回true,如果失败则返回false。我的功能定义如下:

function _userLogin($username, $password){
    include 'mysqli.php';
    $logged_in;
    $mysqli->select_db('Directories');
    // query the login table for the username
    $query = $mysqli->query("SELECT * FROM LOGININFO WHERE USERNAME='$username'");
    $num_rows = mysqli_num_rows($query);

    // check to see if the user exists
    if ($num_rows > 0) {

        $query = "SELECT * FROM LOGININFO WHERE USERNAME='$username'";
        if ($result = $mysqli->query($query)){
            while ($result_ar = mysqli_fetch_assoc($result)){
                $dbuser = $result_ar['USERNAME'];
                $dbpass = $result_ar['PASSHASH'];
                $salt = $result_ar['SALT'];
            }

        } else {
            echo "Could not connect to table: <br />".mysqli_error()."<br />";


            // create the hash for password validation
            $hash = hash('sha256', $salt.$password);

            // validate the password
            if ($hash == $dbpass){
                $logged_in = True;
                // retrieve info from the userinfo table
                $query = ("SELECT * FROM USERINFO WHERE USERNAME='$username'");
                if($result = $mysqli->query($query)){
                    while ($result_ar = mysqli_fetch_assoc($result)){
                        $name = $result_ar['name'];

                    }
                }
            } else {
                $logged_in = False;
                //$message = "Invalid USERNAME or PASSWORD";
                //echo $message;
            }
        }   
    } else {
        $logged_in = False;
        //$message = "Invalid USERNAME or PASSWORD";
        //echo $message;
    }
    return $logged_in;
}

我遇到的问题是这样,当我调用函数并尝试使用应该返回的值时,我得到一个错误,即未定义变量。

_userLogin($username, $password);
    if ($logged_in == True){
    'do something';
    } else {
    'do something else'
}

我做错了什么?

2 个答案:

答案 0 :(得分:1)

您正尝试使用块外部函数$logged_in中定义的变量_userLogin。指定函数返回的返回值,如

$logged_in = _userLogin($username, $password)
if ($logged_in == True){
    'do something';
    } else {
    'do something else'
}

此外,您将始终收到TRUE因为您正在检索它们的if块之外访问变量$salt$password,因此字段未正确分配。

function _userLogin($username, $password){
    include 'mysqli.php';
    $logged_in = false;
    $mysqli->select_db('Directories');
    // query the login table for the username
    $query = $mysqli->query("SELECT * FROM LOGININFO WHERE USERNAME='$username'");
    $num_rows = mysqli_num_rows($query);

    // check to see if the user exists
    if ($num_rows > 0) {
        $query = "SELECT * FROM LOGININFO WHERE USERNAME='$username'";
        if ($result = $mysqli->query($query)){
            $dbpass = '';
            $salt = '';

            while ($result_ar = mysqli_fetch_assoc($result)){
                $dbuser = $result_ar['USERNAME'];
                $dbpass = $result_ar['PASSHASH'];
                $salt = $result_ar['SALT'];
            }

            // create the hash for password validation
            $hash = hash('sha256', $salt.$password);

            // validate the password
            if ($hash == $dbpass){
                $logged_in = True;
                // retrieve info from the userinfo table
                $query = ("SELECT * FROM USERINFO WHERE USERNAME='$username'");
                if($result = $mysqli->query($query)){
                    while ($result_ar = mysqli_fetch_assoc($result)){
                        $name = $result_ar['name'];

                    }
                }
            }

        } else {
            echo "Could not connect to table: <br />".mysqli_error()."<br />";
        }
    }

    return $logged_in;
}

请注意:除了修复语法之外,我没有执行任何逻辑检查

答案 1 :(得分:0)

用更简单的方法替换你的分支(使用函数的地方):

if( _userLogin($username, $password) ){
  //success
}else{
  //failure
}