错误500:脚本头过早结束 [英] Error 500: Premature end of script headers

查看:106
本文介绍了错误500:脚本头过早结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行以下脚本时,出现脚本头的结尾过早:contactform.cgi"错误消息.让我感到沮丧的是,我在另一台服务器上以.php的身份运行了它,并且效果很好.但是,我不得不更改服务器,并且它们仅支持CGI PHP.但是,它不起作用.我认为代码不是错误的,但以防万一.

我已经阅读了一遍,有些人说这是权限问题.对我来说是这样吗?

我知道"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 = "myemail@site.com";

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");
}
?>

解决方案

我想我会回答自己关于踢和咯咯笑声的问题(对于所有有一天可能会发现相同错误且没有答案的人). /p>

这是文件权限问题.

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

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

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.

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?

#!/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 = "myemail@site.com";

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");
}
?>

解决方案

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 755 will 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).

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...

这篇关于错误500:脚本头过早结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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