PHP http_response_code (); against the header ();

I created a contact form based on this tutorial: http://blog.teamtreehouse.com/create-ajax-contact-form

I am using PHP Version 5.3.10-1ubuntu3.4 on my server and am having problems with http_response_code(); , which is used in the example tutorial in the link above. I read http_response_code(); only works with PHP 5.4. So instead, I returned to using header(); .

My form works very well for me, and it shows a success message when I submit, and not errors when I used http_response_code(); but my PHP is not so good and I want to know that what I did is acceptable or if I have to do it differently? If so, correct my code.

Here is the contents of my mailer.php file, where you can see that I commented on http_response_code(); and use header(); .

 if ($_SERVER["REQUEST_METHOD"] == "POST") { // Get the form fields and remove whitespace. $name = strip_tags(trim($_POST["name"])); $name = str_replace(array("\r","\n"),array(" "," "),$name); $email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL); $phone = trim($_POST["phone"]); $company = trim($_POST["company"]); $minbudget = trim($_POST["minbudget"]); $maxbudget = trim($_POST["maxbudget"]); $message = trim($_POST["message"]); $deadline = trim($_POST["deadline"]); $referred = trim($_POST["referred"]); // Check that data was sent to the mailer. if ( empty($name) OR empty($phone) OR empty($message) OR !filter_var($email, FILTER_VALIDATE_EMAIL)) { // Set a 400 (bad request) response code and exit. //http_response_code(400); header("HTTP/1.1 400 Bad Request"); echo "Error (400). That not good, refresh and try again otherwise please email me and let me know you are having trouble submitting this form."; exit; } // Set the recipient email address. // FIXME: Update this to your desired email address. $recipient = " myemail@domain.com "; // Set the email subject. $subject = "Website enquiry from $name"; // Build the email content. $email_content = "Name: $name\n"; $email_content .= "Email: $email\n\n"; $email_content .= "Phone: $phone\n"; $email_content .= "Company: $company\n\n"; $email_content .= "Budget: $minbudget $maxbudget\n"; $email_content .= "Deadline: $deadline\n"; //$email_content .= "Max Budget: $maxbudget\n"; $email_content .= "\n$message\n\n"; $email_content .= "Referred: $referred\n"; // Build the email headers. $email_headers = "From: $name <$email>"; // Send the email. if (mail($recipient, $subject, $email_content, $email_headers)) { // Set a 200 (okay) response code. //http_response_code(200); header("HTTP/1.1 200 OK"); echo "Thank You! I'll be in touch soon."; } else { // Set a 500 (internal server error) response code. //http_response_code(500); header("HTTP/1.0 500 Internal Server Error"); echo "Error (500). That not good, refresh and try again otherwise please email me and let me know you are having trouble submitting this form."; } } else { // Not a POST request, set a 403 (forbidden) response code. //http_response_code(403); header("HTTP/1.1 403 Forbidden"); echo "Error (403). That not good, refresh and try again otherwise please email me and let me know you are having trouble submitting this form."; } 
+7
php
source share
3 answers

I managed to answer this question my own similar question by looking at the PHP source code to determine exactly what was going on.

The two methods are essentially functionally equivalent. http_response_code is basically an abbreviated way of writing an http status header, with an added bonus, that PHP will develop a suitable phrase Reason to provide by matching your response code with one of the values ​​in the enum that it supports in php-src / main / http_status_codes.h .

Please note that this means that your response code must match the response code that PHP knows about. You cannot create your own response codes using this method, however you can use the header method. Also note that http_response_code is only available in PHP version 5.4.0 and higher.

In general, there are differences between http_response_code and header for setting response codes:

  • Using http_response_code will cause PHP to match and apply the mind phrase from the list of mind phrases that are hard-coded into the PHP source code.

  • Due to paragraph 1 above, if you use http_response_code , you must install the code that PHP knows about. You cannot set your own code, however you can set your own code (and the sentence of reason) if you use the header function.

  • http_response_code is only available in PHP 5.4.0 and later

+12
source

A simple solution:

 /** * Sets the response code and reason * * @param int $code * @param string $reason */ function setResponseCode($code, $reason = null) { $code = intval($code); if (version_compare(phpversion(), '5.4', '>') && is_null($reason)) http_response_code($code); else header(trim("HTTP/1.0 $code $reason")); } 

you can use it like:

 setResponseCode(404); 

or

 setResponseCode(401,'Get back to the shadow'); 
+2
source

To answer your main question, the biggest answer that I could see using vs vs http_response_code () headers is that http_response_code () is only supported in PHP 5.4 and higher, older versions will not be able to use this function.

Using headers like you in your example ensures that the code will work in older versions.

+1
source

All Articles