Handling PHP exceptions with Javascript
Posted: Mon May 17, 2010 2:57 pm
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:
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!
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";
}Thanks in advance!