Page 1 of 1

Handling PHP exceptions with Javascript

Posted: Mon May 17, 2010 2:57 pm
by sdeleon28
Hello everyone,
I'm developing this small system in AJAX (my first one using either PHP or Javascript). All redirections in my system are just refreshments of small containers within the same page. What I want to do is run a small authentication script on each js "redirection" (that uses the session vars to verify authentication). What I do then, is associate events (like the onclick on a link or button) to my js redirection script which substitutes the content of a div with the output of a php file (so my pages are php files that are just a piece of what is shown on the actual page). Right now, my pages look like this:

Code: Select all

try {
    require_once "authenticate.php";

    // Output code here.
} catch (Exception $e){
    echo "Authentication failed";
}
When the authenticate.php script fails authenticating, it throws an Exception (as you may have inferred). The thing is, I don't want to write the try/catch thing on each page. So here's the question: Is there a way to let the script throw the Exception straight up and handle it some way through Javascript? Or is there any other way to do this that doesn't involve this kind of boilerplate?

Thanks in advance!

Re: Handling PHP exceptions with Javascript

Posted: Wed May 19, 2010 5:12 am
by Benjamin
If you route all the requests through a single file, say a front controller, then you wouldn't need to perform user authentication on each and every page.

Re: Handling PHP exceptions with Javascript

Posted: Wed May 19, 2010 5:48 am
by Eran
If you route all the requests through a single file, say a front controller, then you wouldn't need to perform user authentication on each and every page.
Well, you do need to perform authentication on each page, you just don't need to call it explicitly.

Re: Handling PHP exceptions with Javascript

Posted: Wed May 19, 2010 5:50 am
by Benjamin
Benjamin wrote:then you wouldn't need to perform user authentication on each and every page.
I mean you would not need to have authentication code on each and every page, because it would be centralized.