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
share|improve this question
You need to return $this->check_em($username, $password); in your else block. – cbuckley May 28 at 22:46
I would say that a bigger problem is that your outer else has no return statement. – Oli Charlesworth May 28 at 22:46
But that just terminates the script... – Chris Jacobs May 28 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. – Harald Brinkhof May 28 at 23:12
feedback

2 Answers

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.

share|improve this answer
TYVM for the fresh eyes! I couldn't even see it... – Chris Jacobs May 28 at 22:49
feedback
...
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.

share|improve this answer
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.