Handling PHP exceptions with Javascript

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
sdeleon28
Forum Newbie
Posts: 4
Joined: Tue Mar 30, 2010 6:41 am

Handling PHP exceptions with Javascript

Post 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!
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Handling PHP exceptions with Javascript

Post 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.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Handling PHP exceptions with Javascript

Post 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.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Handling PHP exceptions with Javascript

Post 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.
Post Reply