Automatically redirect to a "maintenance" page if php error is detected - possible?

Discussion in 'PHP' started by bluebetta, Feb 2, 2011.

  1. #1
    Hello,

    Right now I have display errors turned off, so if I make a coding mistake a blank page is shown. Is it possible to place something in my .htaccess file or somewhere that would auto-redirect to a "maintenance is being performed, check back later" type page if a php error is detected.

    I was just thinking that would be nicer than a blank page. But, from all the research I've done, it seems like this either is not possible, or it's not worth implementing. I'm fine with a blank page, it's better than visitors seeing the actual error message, but again, a maintenance page would be cooler.

    Thanks for your time!
     
    bluebetta, Feb 2, 2011 IP
  2. W3Theory

    W3Theory Peon

    Messages:
    40
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    What is the way you currently handle errors?

    All my errors are handled in a class, with a custom error handler. I have a DevMode that displays errors in an accurate manor for my use when the site is closed, if the site is open then I use the error handler to display a nicer error message.

    You can use set_error_handler() to achieve this.
     
    W3Theory, Feb 2, 2011 IP
  3. bluebetta

    bluebetta Guest

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Well, I'm your typical inexperienced web site owner, so I'm not sure I can accurately explain how I'm handling errors. All I know is I used my php.ini file to turn display errors off because I don't want visitors seeing any "error in config.php on line 563" type errors.

    That works just fine. I'm happy with that, I don't need any type of fancy error handling. I was just hoping for that easy redirect to maintenance page solution that probably doesn't exist. :D

    I think I may just be wasting time on this. I don't think this is something people usually worry about, which is why I'm not finding any solutions online.

    It would be nice if I could just copy and paste something in .htaccess or copy and paste something in my site's config or functions file.


    You can tell .htaccess to go to servererror.html if there is a 500 server error, so you would think they would make an option for you to say, hey .htaccess, if any php errors are detected, show thispage.html.

    If this is not possible, I think it's a feature that should be added in the future :)
     
    Last edited: Feb 2, 2011
    bluebetta, Feb 2, 2011 IP
  4. W3Theory

    W3Theory Peon

    Messages:
    40
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    In my experience, this isn't possible. Syntax errors aren't catchable, I don't think. Your best bet is the error handler
     
    W3Theory, Feb 2, 2011 IP
  5. bluebetta

    bluebetta Guest

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Okay, so basically I can set up error handler so that if there is a scripting error, it will display a custom message?
     
    bluebetta, Feb 2, 2011 IP
  6. W3Theory

    W3Theory Peon

    Messages:
    40
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Yup, you can make the page redirect as well, or update your site and turn on "error mode" which will lead to your maintenance page
     
    W3Theory, Feb 2, 2011 IP
  7. bluebetta

    bluebetta Guest

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Well, now I have to figure out how to do that. I'm using Wordpress as my CMS. I wonder what file I'd need to add the error handler to... Maybe I need to post this at the Wordpress forums.

    What does a basic custom error handler look like? For example, if I want it to redirect to maintenance.html if there is a php error, how do I even begin to set this up?
     
    bluebetta, Feb 2, 2011 IP
  8. W3Theory

    W3Theory Peon

    Messages:
    40
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Wordpress is probably better suited place to ask this, yes.

    And, here will help: http://php.net/manual/en/function.set-error-handler.php
     
    W3Theory, Feb 2, 2011 IP
  9. bluebetta

    bluebetta Guest

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    But wait, this may be my solution! I can't include a link to the page, because I'm a new user here and this forum won't let me post a link, but I found this:

    Basically you add the code below to a file...

    
    register_shutdown_function('errorHandler');
    function errorHandler() {
       $err = error_get_last();
       if($err)
         include "error_page.php"; // your custom error page
    }
    
    PHP:
    Then in php.ini I can use auto_prepend_file which will include this error handling page on every request. So if an error is detected, I'm redirected to this error_page.php.

    I'm may try this later...

    UPDATE: I just read a comment on the page where I found this script, the guy says "This code would not work if you encounter a syntax parse error. The include would bring your script to silently fail, leaving not much of any traces." So, maybe this won't work... I think I'm on the right track though. It seems like if I can create a php page to handle the errors, then in php.ini include this script on every request, then I don't see why that wouldn't work. I just have to find the right combinations of code. :)
     
    Last edited: Feb 2, 2011
    bluebetta, Feb 2, 2011 IP
  10. W3Theory

    W3Theory Peon

    Messages:
    40
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    That looks like it could work, actually
     
    W3Theory, Feb 2, 2011 IP
  11. bluebetta

    bluebetta Guest

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    I guess I could try it.

    But, I just read another comment, which may mean that what I want to do just isn't possible after all. Someone in another forum stated:
    "Syntax errors are not of catchable by an user error handler - I'm sorry but can't do that, this is a limitation of PHP."

    So do you understand what this person is saying? Are they right, it's just not possible?
     
    bluebetta, Feb 2, 2011 IP
  12. W3Theory

    W3Theory Peon

    Messages:
    40
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Like I said earlier, I don't think syntax errors can be caught by handlers....e.g

    Parse Error: syntax error, unexpected $end in .....
     
    W3Theory, Feb 2, 2011 IP
  13. bluebetta

    bluebetta Guest

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Okay, so for example, if I accidentally type an extra single quote where there shouldn't be one in a php file, then a handler couldn't catch that? That's a syntax error right, because it would tell me that there is an unexpected single quote in whatever file, for example.

    If that's the case, then the bottom line is, what I'm wanting definitely is not possible. What I was wanting is, if I'm editing a php page, and I put an extra ; or ) in the code, which triggers an error, could that in any way be redirected to a custom error page. But, what I'm gathering now is, no, it's not possible because syntax errors can't be caught by handlers.

    I'm fine with my site the way it is, so if this just isn't possible, I just want to know so I stop wasting time on it. :)
     
    bluebetta, Feb 2, 2011 IP
  14. W3Theory

    W3Theory Peon

    Messages:
    40
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #14
    Nope, I don't think it's possible to catch syntax errors.
    I've personally never been asked to deal with something like this, so I never looked into it. You can catch fatal errors and warnings
     
    W3Theory, Feb 2, 2011 IP
  15. bluebetta

    bluebetta Guest

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #15
    Thanks for your help W3Theory! With your advice along with the research that I've done for the past two days... I'm going to agree and move on. It's not worth the time to continue researching. As you stated and this other person in another forum stated: Syntax errors are not catchable by an user error handler. Therefore, what I was wanting to do isn't possible.
     
    Last edited: Feb 2, 2011
    bluebetta, Feb 2, 2011 IP
  16. senth

    senth Peon

    Messages:
    53
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #16
    edit the .htaccess file and redirect whatever you want, if the url not found.
     
    senth, Feb 4, 2011 IP
  17. srisen2

    srisen2 Peon

    Messages:
    359
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #17
    srisen2, Feb 4, 2011 IP