doucong3048
doucong3048
2018-12-10 21:00

如何在php中创建一个错误处理程序,以便在发生特定错误时重定向用户?

  • php
  • error-handling
已采纳

I am running some generic php/mysql code that is consistently working fine, then I run the html dom parser (http://simplehtmldom.sourceforge.net/), then I WANT TO redirect to an error page IF AND ONLY IF a specific error occurs with the dom parser, but if not, continue with some additional php/mysql script that is also currently working fine. Here is what my code looks like:

//First do some php/mysql operations here - these are all working fine

$html = file_get_html($website);

foreach($html->find('a[href!=#]') as $element) {
    Do several things here
}

//Then finish up with some additional php/mysql operations here - these are all working fine

Most of the time it works great, but about 10% of the time, depending on the website that is assigned to the $website variable, I get a warning and then a fatal error. For example, when I put “https://www.liftedlandscape.com/” into the $website variable:

Warning: file_get_contents(https://www.liftedlandscape.com/): failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request in /home/test/test.test.com/simple_html_dom.php on line 75

Fatal error: Call to a member function find() on boolean in /home/test/test.com/login/create_application.php on line 148

I am OK with the error happening every once and while; I just want to create an error handler that responds to the error cases appropriately. I want to make sure that the php/mysql code before the dom parser stuff always runs. Then run the dom parser, then run the rest of the script run if the dom parser functions are working right, but if there are the errors above with the dom parser, redirect users to an error page.

I have tried numerous iterations with no success. Here is my latest attempt:

function errorHandler($errno, $errstr) {
  echo "Error: [$errno] $errstr";
  header('Location: 
https://test.com/login/display/error_message.php');

}

//First do some other php/mysql operations here 

$html = file_get_html($website);

foreach($html->find('a[href!=#]') as $element) {
    Do several things here
}

//Then finish up with some additional php/mysql operations here

I swear this actually worked once, but then after that failed to work. It is only returning the same errors listed above without redirecting the user. Can someone please help?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • doupin5667 doupin5667 3年前

    Don't send any output via "echo" or similar because you can't redirect AFTER you've already started sending out the page.

    file_get_contents will return false when it can't complete the request so make sure you check for that before attempting to use the returned variable and assuming you actually have some html to work with.

    Additionally you have to exit after your redirect to prevent the rest of the code being processed.

    $html = file_get_html($website);
    if($html === false) {
      header('Location: https://test.com/login/display/error_message.php');
      exit;
    }
    
    // You can now work with $html string from here onwards.
    
    点赞 评论 复制链接分享