Forum Moderators: coopster

Message Too Old, No Replies

returning codes via header

         

smallcompany

6:03 pm on Apr 28, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



For my custom 404 page, I use this: header('status: 404 Not Found', null, 404);

plus little of additional coding so I get an email whenever 404 gets hit.

I see that 404 can be done in PHP in this way too: header ("HTTP/1.0 404 Not Found");

I wonder what "null" and "404" in my version mean?

Obviously I picked that from the web since I am not good at PHP.

The reason I ask is that now I am setting up a 403 page for bad bots and want to start with having a page in PHP that will issue 403 and send me an email.

I got stuck in if I should do

header('status: 403 Forbidden, null, 403);

or

header ("HTTP/1.1 403 Forbidden");

cameraman

7:27 pm on Apr 28, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



void header [us.php.net] ( string $string [, bool $replace [, int $http_response_code ]] )

The null is telling it to use the default of true for the second parameter.

penders

8:47 pm on Apr 28, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The null is telling it to use the default of true for the second parameter.

Is it safe to assume that passing null in an optional parameter will always invoke it's default value?

IMHO I would have thought it best to always pass the actual value if known, incase the default value should ever change in later versions?

cameraman

1:34 am on Apr 29, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Is it safe to assume...always invoke...
I rarely use the word always, so no, not for every function.

...pass the actual value if known, incase the default value should ever change...
I generally supply the actual parameters up to the one I want to change but leave it beyond that, e.g. I would do
imagejpeg($im,'myfile.jpg');
leaving the quality at its default.

So do you actually always do:
header('http://example.com/otherpage.php',true,302)
(just poking at you <grin>)

smallcompany

4:26 am on Apr 29, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



header('status: 403 Forbidden, null, 403);

or

header ("HTTP/1.1 403 Forbidden");

Is there any difference between the two? Is there any situation where first will act differently compared to the second?

penders

7:52 am on Apr 29, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I generally supply the actual parameters up to the one I want to change but leave it beyond that...

Same same. I would not try to 'skip over' optional parameters (ie. by specifying null ?) in order to change a later parameter; I would specify the actual params up to that point. So yes, I would specify true as opposed to null in the 2nd argument if I was going to specify a value for the 3rd. :)

penders

10:13 am on Apr 29, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Is there any difference between the two?

IMHO both should set the status code. The first example does not set the HTTP version number, but the server should default this. Other differences... anyone?

It might be worth bearing in mind that the 2nd and 3rd params were added later:

4.3.0The http_response_code parameter was added.
4.0.4The replace parameter was added.

Also:

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....

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 some 3xx status code has already been set.

As I understand it, specifying the 3rd param (http_response_code) enables you to set the status code when the header you are sending does not explicitly set one, or you wish to override the default (eg. setting a status code of 307 (moved temporarily) for a 'Location' header).