Tuesday, July 21, 2009

Php page/URL redirect with header() function

If you are a php developer then you must know how to redirect a page from current page using php header() function. And example script should look like as follows :
<?php
header("Location: http://www.myurl.abc");
echo "I don know what to do next";
?>
The only thing that you should take care of is that NOTHING should be printed before the header() function call.
Now if you need to call this header function in the middle of any of your html design- what to do ? Simple call ob_start() at the very first line of your page then call have all your html tags,call your header() function and and call ob_end_flush() immidiately after header() function call. The code should look like as follows :
<?php
ob_start();
//Your html and or other code to print html
echo "A line before header() call";
header("Location: http://www.url");
ob_end_flush();
?>
Reference:

2 comments: