Header Location – PHP

  php

Q(Question):

Its not a php Question. Its a HTTP question

how web server send client Location header request.

The thing is, after sending "\r\n\r\n" web server start to send data to web client but even though in that situation if we create a Location header request it works
look at the below example.

<?php
echo "johny";
header("Location: request.php");
?>

How it works?

A(Answer):

I don’t understand your question; but I am pretty sure your piece of code will fail with a ‘headers already sent’ error

A(Answer):

That is the point. This code dont return error. More over it redirected by browser perfectly.

this is the out put i get from a small http client i have made

HTTP/1.1 302 Found
Date: Mon, 16 Aug 2010 08:49:57 GMT
Server: Apache/2.2.11 (Win32) PHP/5.2.8
X-Powered-By: PHP/5.2.8
Location: request.php
Content-Length: 37
Connection: close
Content-Type: text/html

johny

A(Answer):

I have just made a test

<?php
echo "I am johny\n";
setcookie("Log_time", time()); //according to my previous knowledge it was not suppose to work
?>

the output is

HTTP/1.1 200 OK
Date: Mon, 16 Aug 2010 09:06:50 GMT
Server: Apache/2.2.11 (Win32) PHP/5.2.8
X-Powered-By: PHP/5.2.8
Set-Cookie: Log_time=1281949610
Content-Length: 11
Connection: close
Content-Type: text/html

I am johny

now i can guess its a version issue. Now PHP do not send data directly to client. First it create output then send it to client. that is why it does not create any warning. But I can guess older version make this warning.

A(Answer):

I thought this was only possible with output buffering turned on.
Is it turned on by default in your version?
Take a look at the php.ini file

A(Answer):

for the client it doesn’t matter if there is content or not. the Location header directive just states that the the client has to load the specified URI.

A(Answer):

@code_green
in that case this is the fact. In my case output buffering is on.
I didnt change the configuration. it was default

Directive Local Value Master Value
output_buffering 4096 4096

I got my answer, I think.

@Dormilich:
The entire issue started while i was answering one persons question about header warning. When I tested it in my page with Location directive it was not making any warning. So I made a c code to check what server give to client. Then i figured out that the header part is in header part even though i already have sent the out data. So I was confused how it work. 🙂 now i got my answer

LEAVE A COMMENT