0

For some reason the return doesn't work when the check_em() succeeds. I'm new to php, so I'm at a loss here.

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

?>

Results:

5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8
5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8
val1:


5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8
7c6a61c68ef8b9b6b061b28c348bc1ed7921cb53
val2:no good


test
1
4
  • You need to return $this->check_em($username, $password); in your else block. Commented May 28, 2012 at 22:46
  • I would say that a bigger problem is that your outer else has no return statement. Commented May 28, 2012 at 22:46
  • But that just terminates the script... Commented May 28, 2012 at 22:47
  • I'd say the worst issue is that you search for a username, data presumably given to you by userinput, without cleansing it. Please use pdo and bind your params, using userinput directly in your querystring this way will just open you up to SQL injection. Commented May 28, 2012 at 23:12

2 Answers 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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.