0

由于某种原因,当 check_em() 成功时返回不起作用。我是php新手,所以我在这里不知所措。

<?php

//Class to handle mysql
class db_handler {
    private $db_host = 'localhost';
    private $db_name = 'project';
    private $db_user = 'project';
    private $db_pass = 'dbpassword';
    private $db_con_mysql = '';
    private $db_con_db = '';

    public function check_em($username, $password) {
        $db_query = "SELECT password FROM user WHERE name='".$username."' LIMIT 1;";
        if($this->db_con_mysql!='') {
            $db_query_response = mysql_query($db_query) or die('Query failed: '.mysql_error());
            $db_query_return = mysql_fetch_row($db_query_response);
            $db_sha1_hash = $db_query_return[0];
            echo $db_sha1_hash."<br>";
            echo sha1($password)."<br>";
            if(sha1($password)==$db_sha1_hash) {
                return 'user valid'; //THIS DOESN'T WORK!?!?!?
            } else {
                return 'no good';
            }
        } else {
            $this->db_connect();
            $this->check_em($username, $password);
        }

    }

    //Connect to mysql, then database
    private function db_connect() {
        $this->db_con_mysql = mysql_connect($this->db_host, $this->db_user, $this->db_pass) || die('Connection failed: '.mysql_error());
        $this->db_con_db = mysql_select_db($this->db_name) || die('Could not use'.$this->db_name.'. '.mysql_error());
        return;
    }

    //Disconnect from database and reset vars used to track connection.
    private function db_disconnect() {
        if($this->db_con_mysql!='') {
            mysql_close();
            $this->db_con_mysql = '';
            $this->db_con_db = '';
            return;
        }
    }

    public function fake($some_val) {
        if($some_val<6) {
            return TRUE;
        } else {
            return FALSE;
        }
    }
}

$db_obj = new db_handler();
$val1 = $db_obj->check_em('someuser','password'); //should return 'user valid'
echo "val1:".$val1."<br>";
echo "<br><br>";

$val2 = $db_obj->check_em('someuser','passw0rd'); //should return 'no good'
echo "val2:".$val2."<br>";
echo "<br><br>";

echo "test<br>";
echo $db_obj->fake(4)."<br>";

?>

结果:

5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8
5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8
val1:


5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8
7c6a61c68ef8b9b6b061b28c348bc1ed7921cb53
val2:no good


test
1
4

2 回答 2

3

This line needs a return:

return $this->check_em($username, $password);

But a more sensible solution would be to connect to the database inside the if when the connection is null. Really, the whole thing could be better written, but I'll leave it at that.

于 2012-05-28T22:46:38.527 回答
1
...
else {
            $this->db_connect();
            return $this->check_em($username, $password);
        }
...

You want to add the return, so that if it fails, then it goes one level deeper and finds another. If that level deeper succeeds, it passes the value up to the level above, which can pass it up and up until it reaches the original function call.

于 2012-05-28T22:46:56.430 回答