0

I'm using PHP 7.2.0

I've installed the latest version of XAMPP on my machine that is running on Windows 10 Home Single Language 64-bit Operating System

I'm mainly using following web browsers :

  • Google Chrome Version 63.0.3239.132 (Official Build) (64-bit)
  • Firefox Quantum 57.0.4 (64-bit)

I'm trying to learn the header() function in PHP

Consider below text and sample code from the PHP Manual:

string

The header string.

There are two special-case header calls. The first is a header that starts with the string "HTTP/" (case is not significant), which will be used to figure out the HTTP status code to send. For example, if you have configured Apache to use a PHP script to handle requests for missing files (using the ErrorDocument directive), you may want to make sure that your script generates the proper status code.

<?php
header("HTTP/1.0 404 Not Found");
?>

First of all let me tell you frankly and honestly that I did not understand much of the part from the above text relating to the code below it. I want to understand the above text completely.

Following are the doubts I have and I want appropriate answers for each of my question in an easy to understand, simple and lucid language.

  1. The HTTP status code is not figured out anywhere rather it is hard-coded into a string passed as an argument to the header() function.

    Then what does the sub-clause mean from the above text saying that

" 'HTTP/' (case is not significant), which will be used to figure out the HTTP status code to send"

?

I'm not understanding where the HTTP status code is figured out here as it's been simply hard-coded into the string passed as an argument to the header() function.

  1. How to configure Apache to use a PHP script to handle requests for missing files(using the ErrorDocument directive)?

    In case if it is already configured how to check it?

  2. How can I make sure that that my PHP script generates the proper HTTP status code?

Now, consider below text and sample code from the PHP Manual:

The second special case is the "Location:" header. Not only does it send this header back to the browser, but it also returns a REDIRECT (302) status code to the browser unless the 201 or a 3xx status code has already been set.

<?php
header("Location: http://www.example.com/"); /* Redirect browser */

/* Make sure that code below does not get executed when we redirect. */
exit;
?>

Does the sub-clause

"it also returns a REDIRECT (302) status code to the browser"

  1. From the above text mean that the REDIRECT (302) status code is sent implicitly as the third argument of header() function? If yes, how should I check it? If no, what does this sub-clause actually mean?
  2. What do the status codes 201 and 3xx mean and how do they affect the header() function?

Thank You.

PHPLover
  • 1
  • 51
  • 158
  • 311
  • I think for status code you should take a look on http://php.net/manual/en/function.http-response-code.php – h.s.o.b.s Jan 10 '18 at 08:39
  • 2. By setting up your script as the error document, using the directive? 3. By executing the line of code you yourself quoted …? – CBroe Jan 10 '18 at 09:13
  • @CBroe : Answer my first question please. It's more important to understand me than later two questions. – PHPLover Jan 10 '18 at 09:59
  • Well replace “figure out” with “specify” if that helps ... basically all this is telling you is that you can set the HTTP status code of the response this way. – CBroe Jan 10 '18 at 10:02
  • @CBroe : If so, then why there has been reserved a third argument to the header() function for specifying HTTP Response Code? It would be really helpful if you answer all my questions in well manner and put it as an answer to my main question. – PHPLover Jan 10 '18 at 10:11
  • Because you might want to specify the status code in other cases as well, for example with a `Location` header ... – CBroe Jan 10 '18 at 10:12
  • @CBroe : Please invite me to chat with you. I want to ask few other doubts. I don't know how to invite you otherwise I'd have myself sent invite to you. Please...I'm waiting for your chat invite. – PHPLover Jan 10 '18 at 10:20
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/162879/discussion-between-user2839497-and-cbroe). – PHPLover Jan 10 '18 at 11:02

0 Answers0