what does php's header function do?

108 views Asked by At

I can't find any solutions to this problem. I'm having hard time with this code. Based on what I searched: HTTP/1.1 200 OK - means that the page is good or OK. I don't understand this header function parts only. It is actually part of code.

My questions are:

  1. Why this code is sending header('HTTP/1.1 200 OK');? I know this code means that the page is good, but why are we sending this code?

  2. What is cache-control part, and what will happen if the code send that?

  3. What is Expires:, and the date is 1970? (please simple explanation)

  4. What will happen if the code send header ('Content-type: application/json'); this part, and why are we sending this ?

Code is here:

function json_response( $data, $error=false ) {
  if( $error )
    header('HTTP/1.1 500 JSON Error');
else
    header('HTTP/1.1 200 OK');

header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 01 Jan 1970 00:00:00 GMT');
header('Content-type: application/json');

// Convert strings/integers into an array before outputting data...
if(!is_array($data))
    echo json_encode(array($data), true);
else
    echo json_encode($data, true);
exit;
}
3

There are 3 answers

1
RiggsFolly On BEST ANSWER

why this code is sending "header('HTTP/1.1 200 OK');"? i know this code means that the page is Good, but why are we sending this code??_

This tells your browser that the requested script was found. The broswer can then assume it will be getting some other data as well.

what is cache-control part?? and what will happen if the code send that?_

This tell the browser, and intermediate caches, DO NOT CACHE the data I am sending. This is so that when you make a second request for this data, it will have to go to your server and rerun the data gathering process rather tha get the data from the browser cache or an intermediary cache somewhere on the internet, between your broswer and your server.

what is "Expires:"? and the date is 1970? (please simple explanation)_

This is again for the cache control. Is says that the cache should expire in 1970, in other words if you have it cached you should remove it because 1970 was a long time ago.

what will happen if the code send header('Content-type: application/json'); this part? and why are we sending this ???_

This is to tell the browser that the data you are sending is in JSON format and therefore how to treat it, in your case it means convert the JSON String that is being sent to a javascript Object so the javascript code can process it as a native object and not have to convert the JSON String to a Javascript object manually.

0
Ionic On

You simply return to the browser that you have a content of type json (header('Content-type: application/json');) which will directly expire after retrieving (header('Expires: Mon, 01 Jan 1970 00:00:00 GMT');). In case that your browser haven't expired the already received code and request it again, you'll saying that those code shouldn't be used from cache. Instead it should be retrieved from the server again (header('Cache-Control: no-cache, must-revalidate');).

The header('HTTP/1.1 200 OK'); is just set in case there is another header set in front of your code (i think). Normally if there is no header set, this could be left out.

0
Siim Kallari On

The code returns a json format data from a script.

If there are no Errors the HTTP/1.1 200 OK is returned by header and that means, data is printed to page.

Cache-control means that data you are requesting cant be written to memory - that means, every time you load the page, you have to reload data you get from page.

Expires - I guess it is there for the same reasons are Cache-control, if expiration date is always in past, it means, every time you visit page, you have reload all the data it gives you.

Application/json specifies that data that is returned by script should be treated as JSON type, what is JSON, well google that.