Friday, August 31, 2007

Send Header without error

One of the common errors a web developer often gets is "Headers already sent" error.This happens when we render some text/characters /space in the browser and then try to redirect the page using header function of php.
For long I always thought that header function will only work when there is nothing rendered before the header call.
But recently I discovered(ofcorse with help of google) that we can render anything before the header call and still redirect the page.

Normal Usage with error is


echo "This will give error"
header("location:http://www.yahoo.com");


echo "This will NOT give error";
header("refresh:0; url:http://www.yahoo.com");


Here refresh:0 means the page will load after 0 seconds(which is instantly).
Basically this can be used when we want to have delay the page loading say we want to load yahoo after 2 seconds.
In this case the code will be


header("refresh:2; url:http://www.yahoo.com");




So start using this header call and redirect the page anytime during page execution.

No comments: