Click to See Complete Forum and Search --> : How to do Redirect
Duke Will
02-04-2004, 01:02 AM
I asked in HTML and someone said Redirect is best done with server side. Can anyone tell me SPECIFICALLY how? I am no expert programmer. Right now, I am using meta tag to do it. (Send viewer to a new page after x seconds.)
fredmv
02-04-2004, 01:07 AM
<?php
header('Refresh: 3; url=foo.html');
?>
Duke Will
02-04-2004, 01:20 AM
Originally posted by fredmv
<?php
header('Refresh: 3; url=foo.html');
?>
Uh, so, I don't know what to do. I mean, if I want HTML content on the page they land on and then redirect x-seconds after that... how?
fredmv
02-04-2004, 01:24 AM
The code I provided is generally all you need. As long as your server supports PHP and the file has a .php, .phtml or other associated extension with PHP, it should work perfectly fine. Simply alter the number of seconds and the URI to whatever you'd like.
Duke Will
02-04-2004, 01:31 AM
Well, I mean, do I just put this at the top of the HTML code on the page or where does it go? And then name it .php? I know my server supports php. Obviously, I don't know much. I rarely do any php stuff.
fredmv
02-04-2004, 01:34 AM
No problem; I don't really work with PHP much myself, I just happen to know how to do this. As you've already assumed, it must go at the top of the page even more so because it is sending an HTTP header — absolutely no content whatsoever can come before it (even a space might cause a problem) — so, at the very top of the page. You could, however, use output buffering to overcome this, but that would be slightly more complex.
Duke Will
02-04-2004, 07:29 PM
Could someone look at this test page (http://www.tigersnumberone.com). The php redirect is not working. Do I have to "do" anything other that ftp the file? (My server is set up for php.)
I assume the "3" means 3 seconds.
daed17
02-04-2004, 08:04 PM
make sure the page's extension is php... so index.htm with php code will be index.php
Duke Will
02-04-2004, 08:15 PM
Ok, thanks, renamed to .php and it works.
Duke Will
02-04-2004, 08:23 PM
Any idea... after the first time, in IE, it will NOT redirect. In Mozilla, it continues to redirect each time.
fredmv
02-04-2004, 08:34 PM
Perhaps try changing Refresh to refresh? I know it sounds stupid, but IE is rather weird on how it handles HTTP headers: even if they are valid HTTP headers and how you wrote it doesn't have certain characters uppercase or lowercase, it will simply not work.
daed17
02-04-2004, 08:50 PM
I just tested it in IE 6 and it did work. you might want to clear the cache on you pc. That could be your problem also.
fredmv
02-04-2004, 09:11 PM
Interesting. Maybe that HTTP header just isn't recognized by IE? I suppose you could emulate it using sleep, like this:<?php
sleep(3);
header('Location: http://www.foo.com/');
?>
Duke Will
02-04-2004, 09:20 PM
Well, it DID work but only once. (On this computer's IE.) When someone says clear cache, you mean clear cookies?
slyfox
02-06-2004, 07:14 AM
Do it in JavaScript:
Paste this between the <head></head> tags:
<script language="Javascript">
<!--
function redirectMe()
{
window.location = "somepage.htm";
}
-->
</script>
...then this inside your <body> tag:
onLoad="redirectMe();"
..and presto! no need for server side;)
fredmv
02-06-2004, 09:50 AM
Originally posted by slyfox
Do it in JavaScript No. That way it would not work for people in which use browsers that don't support JavaScript or have disabled it. By doing it server-side, the user's setup is completely irrelavant and thus it is the preferred method.
slyfox
02-07-2004, 03:53 PM
..that makes sense!
Ok.. i see the poster had some problems setting it up with the header...
why not let him use a normal redirect function...
in asp he will achieve this with the following:
Response.Redirect("newpage.htm")
hope it helps;)