PHP: try, catch and throw.

Soldato
Joined
4 Mar 2010
Posts
5,038
Hi chaps trying to implement some error checking in a couple of classes.

It's a login class with PDO instantiated inside.

Now the problem I am having is when a user logs in it will catch an error if they have left out a missing field ($user, $pass) this works; but when it catches the error it carrys on to the next private method being called in this case it's a PDO connection which checks to see if the user exists on the db then catchs another error returning "incorrect username and or password".

It's a PITA , because if a user were to leave an empty feild it will return two errors eg "Please enter your username Incorrect username and/or password". How do I keep the catching functionally without it passing both errors down the stack?
 
Last edited:
Would be helpful if you posted the code :)

And the level of coding you have :)
You got it ;)



Login.php
PHP:
<?php

require_once ($_SERVER['DOCUMENT_ROOT'].'/php/classes/display.class.php');
require_once ($_SERVER['DOCUMENT_ROOT'].'/php/classes/login.class.php');
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
$display = new display('Bookmark it Login');

$login = new login();
$login->check_valid_user($_POST['username'], $_POST['password']);

?>

login.class.php
PHP:
<?php

/**
 * Description of login
 *
 * @author Lukie
 */
class login {
    
    public function __construct() 
    {
        require_once ($_SERVER['DOCUMENT_ROOT'].'/php/classes/db_details.class.php');
    }

    
    private function connect($hostname,$dbname,$dbuser,$dbpass,$user,$pass)
    {
        
        /*
         * A quick PDO wrapper which gets connection details from db_details.class
        */            
        try
        { 
            
            $db = new PDO("mysql:host=$hostname;dbname=$dbname",$dbuser,$dbpass);
            
            $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
                        
            $sth = $db->prepare(
                    "SELECT * FROM user
                    WHERE username='$user'
                    and passwd = sha1('$pass')");
            $sth->execute();
            
                        
                if($sth->rowCount() == 0)
                {
                    throw new Exception('<p>Incorrect username and/or password</p>');
                }
                else
                {
                    echo 'user found';
                }
            
         }
        
        catch(Exception $epdo)
        {
            echo $epdo->getMessage();
        }

        
        
        
    }
    
    private function is_form_empty($user,$pass)
    {
        try
        {
            
            if(!isset($user) || empty($user))
            {
                throw new Exception('<p>Please enter your username</p>');
               
            }
        
            if(!isset($pass) || empty($pass))
            {
                throw new Exception('<p>Please enter your Password</p>');
                            }
        }
        catch(Exception $e)
        {
            echo $e->getMessage();
        }
        
    }

    public function check_valid_user($user,$pass)
    {
        $this->is_form_empty($user, $pass);
                      
        $this->connect(db_details::HOSTNAME,
            db_details::DB_NAME,
            db_details::GENERAL_USER,
            db_details::GENERAL_PASS,$user,$pass);
        
             
     
    }
}

?>
 
login.class.php
PHP:
<?php

/**
 * Description of login
 *
 * @author Lukie
 */
class login {
    
    public function __construct() 
    {
        require_once ($_SERVER['DOCUMENT_ROOT'].'/php/classes/db_details.class.php');
    }

    
    private function connect($hostname,$dbname,$dbuser,$dbpass,$user,$pass)
    {
        
        /*
         * A quick PDO wrapper which gets connection details from db_details.class
        */            

            $db = new PDO("mysql:host=$hostname;dbname=$dbname",$dbuser,$dbpass);
            
            $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
                        
            $sth = $db->prepare(
                    "SELECT * FROM user
                    WHERE username='$user'
                    and passwd = sha1('$pass')");
            $sth->execute();
            
                        
                if($sth->rowCount() == 0)
                {
                    throw new Exception('<p>Incorrect username and/or password</p>');
                }
                else
                {
                    echo 'user found';
                }
        
    }
    
    private function is_form_empty($user,$pass)
    {
 
            if(!isset($user) || empty($user))
            {
                throw new Exception('<p>Please enter your username</p>');
               
            }
        
            if(!isset($pass) || empty($pass))
            {
                throw new Exception('<p>Please enter your Password</p>');
            }

    }

    public function check_valid_user($user,$pass)
    {
		try
        {
			$this->is_form_empty($user, $pass);
						  
			$this->connect(db_details::HOSTNAME,
				db_details::DB_NAME,
				db_details::GENERAL_USER,
				db_details::GENERAL_PASS,$user,$pass);
			
        }
        catch(Exception $e)
        {
            echo $e->getMessage();
        }
     
    }
}

?>

Perhaps?
 
Last edited:
When something throws an Exception in a try block, the rest of the try block doesn't get run. So if the form is empty it won't even try to call connect() 'cause it'll just skip to the catch block.
As the try blocks were inside the methods before, each method would get the chance to run even if one threw an Exception!
 
When something throws an Exception in a try block, the rest of the try block doesn't get run. So if the form is empty it won't even try to call connect() 'cause it'll just skip to the catch block.
As the try blocks were inside the methods before, each method would get the chance to run even if one threw an Exception!

Ah okay thanks, so I should avoid using try blocks inside of methods and just use them to wrap the method around when being called?
 
Depends on the situation! In this case check_valid_user() should only be called if is_form_empty() doesn't throw an exception, so it makes sense for them to be in the same block.
 
Back
Top Bottom