3

我的 PHP 代码总是返回 HTTP/1.1 500 内部服务器错误。这是从一些 AJAX 代码访问的,并且是一个单独的文件。这很可能是显而易见的,任何帮助将不胜感激。PHP:

<?php  
if( isset($_POST) ){  

//form validation vars  
$formok = true;  
$errors = array();  

//submission data  
$ipaddress = $_SERVER['REMOTE_ADDR'];  
$date = date('d/m/Y');  
$time = date('H:i:s');  

//form data  
$fname = $_POST['fname'];  
$lname = $_POST['lname'];     
$aemail = $_POST['aemail'];  
$year = $_POST['year'];  
$position = $_POST['position'];  
$club = $_POST['club'];  

//validate email address is not empty  
if(empty($email)){  
    $formok = false;  
    $errors[] = "You have not entered an email address";  
//validate email address is valid  
}elseif(!filter_var($aemail, FILTER_VALIDATE_EMAIL)){  
    $formok = false;  
    $errors[] = "You have not entered a valid email address";  
}

//send email if all is ok  
if($formok){  
    $headers = "From: " $aemail . "\r\n";
    $headers = "Reply-To: " . $email_from . "\r\n"; 
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";  

    $emailbody = "<p>You have received a new message from the enquiries form on your website.</p> 
                  <p><strong>First Name: </strong> {$fname} </p>
                  <p><strong>Last Name: </strong> {$fname} </p>                      
                  <p><strong>Email Address: </strong> {$email} </p> 
                  <p><strong>School Year: </strong> {$year} </p> 
                  <p><strong>Club: </strong> {$club} </p> 
                  <p><strong>Position: </strong> {$position} </p> 
                  <p>This message was sent from the IP Address: {$ipaddress} on {$date} at {$time}</p>";

    ini_set("sendmail_from", $email_from); 

    $sent = mail("name@domaain.co.uk","New Enquiry",$emailbody,$headers, "-f" . $email_from);  

}  

//what we need to return back to our form  
$returndata = array(  
    'posted_form_data' => array(  
        'fname' => $fname, 
        'lname' => $fname, 
        'aemail' => $aemail,  
        'year' => $year,  
        'position' => $position,  
        'club' => $club  
    ),  
    'form_ok' => $formok,  
    'errors' => $errors  
);  


//if this is not an ajax request  
if(empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) !== 'xmlhttprequest'){  
    //set session variables  
    session_start();  
    $_SESSION['cf_returndata'] = $returndata;  

    //redirect back to form  
    header('location: ' . $_SERVER['HTTP_REFERER'].' 500 Internal Server Error', true, 500);  
 }  
}
?>
4

2 回答 2

3

可能有一个php错误。将其放在您的 php 文件的标题中以查看错误:

ini_set('display_errors', 1);
error_reporting(E_ALL);

感谢@bwoebi,我可以在解析错误的情况下添加它以显示错误文本而不是 500 错误,你应该把它放到你的 .htaccess 中:

php_flag display_errors 1
于 2013-06-16T17:31:27.580 回答
2

启用错误消息后,我看到:

PHP 解析错误:语法错误,第 33 行的 file.php 中出现意外的 '$aemail' (T_VARIABLE)

错字在第 33 行:

$headers = "From: " $aemail . "\r\n";
//                 ^------------------ note the missing point

写:

$headers = "From: " . $aemail . "\r\n";
于 2013-06-16T17:31:06.097 回答