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

报告相同问题?

悬赏问题

  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?