php 错误 500:脚本头过早结束

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17583341/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:08:04  来源:igfitidea点击:

Error 500: Premature end of script headers

phpcgi

提问by Keven

I get a "Premature end of script headers: contactform.cgi" error message when running the below script. What frustrates me is that I ran this as a .php on another server and it worked. However, I had to change servers and they only support CGI PHP. However, it doesn't work. I don't think the code is wrong, but take a look just in case.

运行以下脚本时,我收到“脚本标题过早结束:contactform.cgi”错误消息。让我感到沮丧的是,我在另一台服务器上将它作为 .php 运行,并且它工作正常。但是,我不得不更换服务器,它们只支持 CGI PHP。但是,它不起作用。我不认为代码是错误的,但请看一下以防万一。

I've read around and some have said it's a permissions issue. Could this be the case for me?

我已经阅读了,有些人说这是一个权限问题。这可能是我的情况吗?

I know that the "display_errors" and "error_reporting" statements will display errors in the error log, but if I don't have access to the server, how can I check the logs?

我知道“display_errors”和“error_reporting”语句会在错误日志中显示错误,但是如果我无法访问服务器,我该如何检查日志?

#!/usr/local/bin/php

<?php

print "Content-type: text/html\n\n";
use CGI::Carp qw(fatalsToBrowser);
ini_set('display_errors',1);
error_reporting(E_ALL);

if(isset($_POST['email'])) {

//Email this form to me
$email_to = "[email protected]";

function died($error) {
    // your error code can go here
    echo "Oops... something's wrong. ";
    echo "Fix the error(s) below:<br /><br />";
    echo $error."<br /><br />";
    die();
}


// validation expected data exists
if(!isset($_POST['first_name']) ||
    !isset($_POST['last_name']) ||
    !isset($_POST['email']) ||
    !isset($_POST['subject']) ||
    !isset($_POST['comments'])) {
    died('There appears to be a problem with the form you submitted.');       
}


$first_name = $_POST['first_name']; // required
$last_name = $_POST['last_name']; // required
$email_from = $_POST['email']; // required
$subject = $_POST['subject']; // not required
$comments = $_POST['comments']; // required

$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$first_name)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$last_name)) {
$error_message .= 'The Last Name you entered does not appear to be valid.<br />';
}
if(strlen($comments) < 2) {
$error_message .= 'The Comments you entered do not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}


$email_message = "Form details below.\n\n";

function clean_string($string) {
  $bad = array("content-type","bcc:","to:","cc:","href");
  return str_replace($bad,"",$string);
}

$email_message .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Last Name: ".clean_string($last_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Subject: ".clean_string($subject)."\n";
$email_message .= "Comments: ".clean_string($comments)."\n";

//Email Subject (put here to include subject from form)
$email_subject = "SUBJECT | ".clean_string($subject)."";


// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
?>

<!-- include your own success html here -->

<?php
header("Location: thankyou.html");
}
?>

回答by Keven

I thought I would answer my own question for kicks and giggles (and for all those who might one day find themselves with the same error and no answers).

我想我会回答我自己的问题,因为踢球和咯咯笑(以及所有可能有一天会发现自己犯同样错误而没有答案的人)。

It was a file permission issue.

这是一个文件权限问题。

All files on my website were set to a permission of '644.' Once I changed the permission level to 705 (chmod 705[chmod 755will also work]) everything was fine and dandy. Not that this matters, but I also changed the folder it was in to 701 (to hide it, but still be executable by the server).

我网站上的所有文件的权限都设置为“644”。一旦我将权限级别更改为 705(chmod 705[chmod 755也可以使用]),一切都很好。这并不重要,但我还将它所在的文件夹更改为 701(以隐藏它,但仍可由服务器执行)。

I still don't understand why my .PHP file worked on the other server when it was probably set to 644?? How could Apache execute the script without world permission?? Does Apache not need world permission?? Just a few questions I still have...

我仍然不明白为什么我的 .PHP 文件可能设置为 644 时在另一台服务器上工作?Apache 如何在没有世界许可的情况下执行脚本?Apache 不需要世界许可吗??我还有几个问题……

回答by edwardmp

The "Premature end of script headers" error message is probably the most loathed and common error message you'll find. What the error actually means, is that the script stopped for whatever reason before it returned any output to the web server. A common cause of this for script writers is to fail to set a content type before printing output code. In Perl for example, before printing any HTML it is necessary to tell the Perl script to set the content type to text/html, this is done by sending a header, like so:

“脚本头过早结束”错误消息可能是您会发现的最讨厌和最常见的错误消息。错误的实际含义是,脚本在将任何输出返回到 Web 服务器之前因任何原因停止。脚本编写者的一个常见原因是在打印输出代码之前无法设置内容类型。例如,在 Perl 中,在打印任何 HTML 之前,有必要告诉 Perl 脚本将内容类型设置为 text/html,这是通过发送标头来完成的,如下所示:

print "Content-type: text/html\n\n";

(source; http://htmlfixit.com/cgi-tutes/tutorial_Common_Web_dev_error_messages_and_what_they_mean.php#premature

(来源;http://htmlfixit.com/cgi-tutes/tutorial_Common_Web_dev_error_messages_and_what_they_mean.php#premature

回答by BuvinJ

Check your line endings! If you see an error about the file not being found, followed by this "premature of end headers" error in your Apache log - it may be that you have Windows line endings in your script in instead of Unix style. I ran into that problem / solution.

检查你的行尾!如果您看到有关未找到文件的错误,然后在您的 Apache 日志中看到此“结束标头过早”错误 - 可能是您的脚本中有 Windows 行结尾而不是 Unix 样式。我遇到了那个问题/解决方案。

回答by Henry van Megen

I experienced this problem today, but unfortunately none of the suggestions here helped. The only problem was that I didn't see ANY errors.. I literally had to do an strace -p <process_id>on the Apache thread to spot the headers being written and Apache crashing on the next line; Somewhere in my PHP code I was setting a header with over 12KB of data.

我今天遇到了这个问题,但不幸的是,这里的建议都没有帮助。唯一的问题是我没有看到任何错误.. 我真的不得不strace -p <process_id>在 Apache 线程上做一个来发现正在写入的头文件和 Apache 在下一行崩溃;在我的 PHP 代码中,我设置了一个包含超过 12KB 数据的标头。

The lesson here is that in some cases, Apache crashing with a HTTP error 500 - Premature end of script-failure can be the result of having too long or overflowing HTTP headers.

这里的教训是,在某些情况下,Apache 因HTTP error 500 - Premature end of script-failure崩溃可能是 HTTP 标头过长或溢出的结果。

Debug your headers for length if you have this same problems because most (if not all) web servers have HTTP header limits.

如果您遇到同样的问题,请调试您的标头长度,因为大多数(如果不是全部)Web 服务器都有 HTTP 标头限制。

PS: This replyhas some info on header sizes.

PS:这个回复有一些关于标题大小的信息。

回答by George Donev

It is to do with file permissions and it happens on systems that have the suphp(usually cpanel/whost servers) installed.

这与文件权限有关,它发生在安装了 suphp(通常是 cpanel/whost 服务器)的系统上。

Removing the write permissionfrom anyone else but the owner(644|600) for the php files will fix the issue. That is how i got it fixed.

删除php 文件的所有者(644|600)以外的任何其他人的写权限将解决该问题。我就是这样修复的。

I hope this helps.

我希望这有帮助。

回答by webomg

In my case it was a cache directory that wasn't so big, only 17MB but duo to the file structure it took forever to be accessed by the website and was producing that error after max_execution_time was reached.

在我的情况下,它是一个缓存目录,它不是那么大,只有 17MB,但由于网站需要永远访问的文件结构,并且在达到 max_execution_time 后产生该错误。

I renamed the directory to cache-BK and created a new cache directory with the same permissions and that solved the problem.

我将目录重命名为 cache-BK 并创建了一个具有相同权限的新缓存目录,这解决了问题。

回答by mejem

In my case I had to change

就我而言,我不得不改变

#!/usr/bin/php

to

#!/usr/bin/php-cgi

回答by Adem

to fix this, I had to change permissions for whole directory to 755 (777 did not work for me), and changed file owners for whole directory

为了解决这个问题,我必须将整个目录的权限更改为 755(777 对我不起作用),并更改了整个目录的文件所有者

chmod -R 755 public_html
chown -R nobody:nobody public_html

nobody is user that runs php in my computer.

没有人是在我的电脑上运行 php 的用户。

回答by Nitigya Sharma

Error can be caused by various issues. for more info check suexec or fcgi logs. For example if suexec has wrong user and permssion it can cause the error to occur to solve try

错误可能由各种问题引起。有关更多信息,请查看 suexec 或 fcgi 日志。例如,如果 suexec 有错误的用户和权限,它可能会导致错误发生以解决尝试

chgrp WEBGROUP /usr/local/apache2/bin/suexec
chmod 4750 /usr/local/apache2/bin/suexec

回答by BryanT

In my case (referencing a PHP file in the top folder of a Wordpress plugin) I had to change the permissions on that folder. My test environment was fine, but when deployed the folder had 775. I changed it to 755 and it works fine.

就我而言(引用 Wordpress 插件顶部文件夹中的 PHP 文件)我必须更改该文件夹的权限。我的测试环境很好,但部署时文件夹有 775。我将其更改为 755,它工作正常。