如何从PHP脚本发送500内部服务器错误错误 [英] How to send 500 Internal Server Error error from a PHP script

查看:227
本文介绍了如何从PHP脚本发送500内部服务器错误错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在某些情况下,我需要从PHP脚本发送500 Internal Server Error。脚本应该由第三方应用程序调用。该脚本包含几个 die(this happend)语句,我需要发送 500内部服务器错误响应代码,而不是通常的 200 OK 。第三方脚本将在某些情况下重新发送请求,包括未收到 200 OK 响应代码。



问题的第二部分:我需要像这样设置脚本:

 <?php 
custom_header( 500内部服务器错误 );
$ b $ if(that_happened){
die(that happened)
}

if(something_else_happened){
die(something其他发生)
}

update_database();

//脚本也可以在上面的行中失败
//例如发生mysql错误

remove_header(500);
?>

我只需要在> 200



编辑



一个侧面问题:我可以发送奇怪的500个标题,例如这些:
$ b $ pre $ HTTP / 1.1 500未找到记录
HTTP / 1.1 500脚本生成错误(E_RECORD_NOT_FOUND)
第23行中的HTTP / 1.1 500条件失败

Web服务器是否会记录此类错误? >

解决方案

  header($ _ SERVER ['SERVER_PROTOCOL']。'500 Internal Server Error',true ,500); 


I need to send "500 Internal Server Error" from an PHP script under certain conditions. The script is supposed to be called by a third party app. The script contains a couple of die("this happend") statements for which I need to send the 500 Internal Server Error response code instead of the usual 200 OK. The third party script will re-send the request under certain conditions which include not receiving the 200 OK response code.

Second part of the question: I need to setup my script like this:

<?php
    custom_header( "500 Internal Server Error" );

    if ( that_happened ) {
        die( "that happened" )
    }

    if ( something_else_happened ) {
        die( "something else happened" )
    }

    update_database( );

    // the script can also fail on the above line
    // e.g. a mysql error occurred

    remove_header( "500" );
?>

I need to send 200 header only after the last line has been executed.

Edit

A side question: can I send strange 500 headers such as these:

HTTP/1.1 500 No Record Found
HTTP/1.1 500 Script Generated Error (E_RECORD_NOT_FOUND)
HTTP/1.1 500 Conditions Failed on Line 23

Will such errors get logged by the webserver?

解决方案

header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500);

这篇关于如何从PHP脚本发送500内部服务器错误错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆