doucong3048 2018-12-10 21:00
浏览 47
已采纳

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

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 2018-12-11 16:16
    关注

    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.
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?