Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Header refresh not working

echo "Message has been sent.<br>";
echo "Your will be redirected back to homepage in 5 seconds.<br>";
header("Refresh: 5;url=index.php");
echo "If you are not redirected yet <a href='../index.php'>click here</a>.";

The above code is working on my local machine but when i uploaded the code on live website its not redirecting automatically to my index.php page. I tried on all browsers but its not working. Any suggestions will be highly appreciated..

2 Answers

Do you get any errors? It can be because you have a different php doc root directory. Try adding a / before i dex.php.

Maybe it is because you are calling the header after you start sending info to the browser instead of before... Try this instead:

header("Refresh: 5;url=index.php");
echo "Message has been sent.<br />";
echo "You will be redirected back to the homepage in 5 seconds.<br />";
echo "If you are not redirected yet, <a href='../index.php'>click here</a>.";

Ok, Thanks it worked but i have one more question why the code below is not working same as above?

echo "Message has been sent.<br>";
echo "Your will be redirected back to homepage in 5 seconds.<br>";
echo "If you are not redirected yet <a href='../index.php'>click here</a>.";
header("Refresh: 5;url=../index.php");

Hi Chandan,

header() has to be called before any html output to the page.

http://www.php.net/manual/en/function.header.php