Jacques Koekemoer
Jacques Koekemoer

Reputation: 1414

How to see if a function called is originated from PHP die()

I have a php function create_error($error_string, $priority = false, $display_error = "")

$error_string = Error Message.

$priority = If the function should only display this message above all other messages. create_error uses a global variable to store all the previous error messages in an array. If $priority === true then it will just have the new message in the array else it will have the new message added using array_push.

$display_error = Control to highlight along side the error message.

function create_error($error_string, $priority = false, $display_error = "")
{
    global $json_responder;

    if ($priority === true) {
        $json_responder = array(array("typeof" => "message", "message" => translate($error_string)));
    } else {
        if (count($json_responder) >= 1) {
            array_push($json_responder, array("typeof" => "message", "message" => translate($error_string)));
        } else {
            $json_responder = array(array("typeof" => "message", "message" => translate($error_string)));
        }
    }

 // my ideal if died statement would be here.
 // like 
 // if(is_from_die() === true){
 // echo json_encode($json_responder);}
 }

I have the following piece of code:

$sql_code = "select username, password, login_allowed from user where current = 1 and username = '$username' and password = '$password' ";
$qrs = mysqli_query($sql,$sql_code) or die(create_error('E500.2 - internal server error.'));

if(mysqli_num_rows($qrs) >= 2 || mysqli_num_rows($qrs) === 0) die(create_error('Incorrect username / password combination.'));

So when this code runs die(create_error('Incorrect username / password combination.')); it doesn't ever display the message because it never gets to the end of the function.

How can I determine in my create_error function if it was called from a die construct in PHP? I've tried debug_print_backtrace(); which returns

Array
    (
        [0] => Array
            (
                [file] => C:\Web\dev\core\request_initializer.php
                [line] => 83
                [function] => create_error
                [args] => Array
                    (
                        [0] => Incorrect username / password combination.
                    )

            )

        [1] => Array
            (
                [file] => C:\Web\dev\core\request_initializer.php
                [line] => 29
                [function] => request_login
                [args] => Array
                    (
                    )

            )

    )

Is it possible to detect weather it came from a die() construct? I have a lot of work around options here because the application is still in the core development stages, but my ideal option would be for PHP to "automatically" detect the die() or exit()

Upvotes: 2

Views: 118

Answers (1)

Halayem Anis
Halayem Anis

Reputation: 7785

I really advice you to use Exception, and that you implement you Custom Exceptions instead of calling exit() or die() built in functions... Otherwise, now, the only way to achieve your work, is by changing your code like this :

$qrs = mysqli_query($sql,$sql_code) or create_error('E500.2 - internal server error.', FALSE, "", TRUE);


function create_error($error_string, $priority = false, $display_error = "", $withExit = FALSE) {
    // do your stuf...
    // ...
    if($withExit === TRUE) {
        exit (0);
    }
}

Upvotes: 1

Related Questions