エラー 500: スクリプト ヘッダーの終了が早すぎます 質問する

エラー 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 = "[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");
}
?>

ベストアンサー1

それはファイルの権限の問題でした。

私のウェブサイト上のすべてのファイルは、権限レベル「644」に設定されていました。権限レベルを705に変更すると(chmod 705) すべてが機能しました。705 に変更しましたが、755 でも機能することに注意してください。また、フォルダーを 701 に変更しました (非表示にするためですが、サーバーから実行可能です)。

余談:おそらく 644 に設定されていたのに、なぜ .PHP ファイルが他のサーバーで動作したのか、いまだに理解できません。Apache はどのようにしてワールド パーミッションなしでスクリプトを実行できたのでしょうか。Apache にはワールド パーミッションは必要ないのでしょうか。まだいくつか疑問が残っています...

おすすめ記事