我的PHP代码始终返回HTTP / 1.1 500内部服务器错误。这是从一些AJAX代码访问的,并且是一个serperate文件。这很可能是显而易见的,任何帮助都会受到赞赏。 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);
}
}
?>
答案 0 :(得分:3)
可能存在php错误。把它放到php文件的标题中以查看错误:
ini_set('display_errors', 1);
error_reporting(E_ALL);
感谢@bwoebi我可以在解析错误的情况下完全添加,以显示错误文本而不是500错误,你应该把它放到.htaccess:
php_flag display_errors 1
答案 1 :(得分:2)
启用错误消息后,我看到:
PHP解析错误:语法错误,第33行的file.php中出现意外的'$ aemail'(T_VARIABLE)
第33行的错字:
$headers = "From: " $aemail . "\r\n";
// ^------------------ note the missing point
写:
$headers = "From: " . $aemail . "\r\n";