Take the 2-minute tour ×

I am trying to redirect a page to another page and that was working successfully. However I am trying to redirect the first page to another page with adverts. This page will then redirect to another page after five seconds.

I am trying to do that by doing this:

$url = $_GET['url'];
header("Location: ".$url."");

However it is showing the advert in ads.php perfectly, but it is not redirecting after five seconds. I am receiving this error in my web browser:

Warning: Cannot modify header information - headers already sent by 
(output started at /home/nucleusi/public_html/adverts/ads.php:1)
in /home/nucleusi/public_html/adverts/index.php on line 7

A typical link I would be redirecting to would be this:


share|improve this question
I would like to see the answers. It's going to be amazing –  Your Common Sense Jan 1 '11 at 16:43
@Col. Shrapnel - Thought Christmas was a good few days back. :-) –  middaparka Jan 1 '11 at 16:45

4 Answers 4

up vote 5 down vote accepted

on the first page(before the ads) before the doctype or html tags put:

<?php header("location: adlocationhere.php"); ?>

Then on the ad page put this:

<?php header("refresh:5;url=secondredirectafter5seconds.php"); ?>

This will redirect your first page immediately and it will redirect your second page after 5 seconds. Hope this helps (its should also get rid of the cannot modify header info if you put it before the doctype and html tags).

EDIT: Also having javascript do it can be a security risk b/c any user can change its location. By doing it this way you have full control over where the user is being directed.

share|improve this answer
I don't see how using JavaScript to perform such a redirect would constitute a security risk. In theory, any user agent could refuse to perform the redirect, or do so only after consulting the end user, even for the standard (3xx) HTTP status codes. You're right that a savvy user could modify your page source and have themselves redirected elsewhere, but they could do this just as easily with their browser's address bar! At no point should you ever assume you can "control the user", because you have no control over the user agent, beyond hoping it will comply with the HTTP RFC. –  Rob Jan 1 '11 at 19:04
The reason I added that is b/c they could change the source and they know exactly where it is going. With php they may not know the file names so without them know the file name they can not change the URL. Granted after going to it once, it will be stored in the history, but i just prefer with this sort of thing using something I have control over rather than the user. Most of the time for ads a client wants control not the user having control over it, and this easier to do with PHP. I agree with not controlling the user most of the time but this is an exception I believe. –  jefffan24 Jan 3 '11 at 2:27

Use refresh meta tag or javascript window.location

share|improve this answer

This might help you

setTimeout("javascript window.location",3000)

with regards


share|improve this answer
thanks, where would I put this? –  max_ Jan 1 '11 at 16:50

All pages have headers and content. In order to redirect a visitor to another page, you must do it in the header section of the page. Once you have printed the page content (your ads), you have effectively started the content, in which there is no going back to modify the headers.

The best way to do this is to have a javascript such as

function redirect() { window.location = '$url'; }
setTimeout("redirect();", 5000);
share|improve this answer
this would be done in the html.. probably at the bottom of the page, before </body> –  styler1972 Jan 1 '11 at 16:56
ok, I have this so far, but it is still not redirecting: <html> <script> $url = $_GET['url']; function redirect() { window.location = '$url'; } setTimeout("redirect();", 5000); </script> <?php include ('ads.php'); exit; ?> –  max_ Jan 1 '11 at 16:58
javascript doesn't have a $_GET feature. That php strictly. What you can do is... var url = <?php echo $_GET['url']; ?>; function redirect(){ window.location = url; }.... and that should work. –  jefffan24 Jan 3 '11 at 12:53

Your Answer


