just noticed that sleep() is probably more use to you and you just pass in the number of seconds
sleep(5); // 5 seconds
Main Topics
Browse All TopicsHello, I know that this question has been asked a million times, a million different ways here at experts-exchange, but unfortunately I was unable to find an existing answer to my question in my search through the existing resolutions. I would like to load my 404 error page completely (display the entire page of content), then after a few seconds, redirect to a specified url of my site.
I would like to be able to either redirect them to the previous page, or to a new url of my site, based on a variable or something, and I would like to set the refresh time via a variable.
E.g.
redirect (
time = 3 /* Refresh time in seconds */
new = 0
/* If $new = 0 we go to a new, if new = 1 we go to the previous page
if $new == 0; {
new_url = http://newpage/;
} else {
new_url = $referrer;
}
)
/* Now perform the redirect after a set amount of $time
redirect()
... Obviously I don't know the code, but maybe you will get my idea.
All of my hosting accounts support php, and I use it heavily, especially on my MT installations, so this script should work fine for me. Also, I do not want to rely on JavaScript or a meta tag to perform the refresh, because in some instance, both of these methods may fail, sometimes related to the browser, or JavaScript being disabled by the user. Where, if the server is performing the redirect, it "should" always work.
I appreciate an advice that would be provided. I am definately not a PHP guy, so I need your help :).
Regards,
-MattV
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
p.s. If anyone is wondering why I would wnat to do this w/ a 404 error document, maybe it would make more sense if we used a "thank you" page in our example. ...thank them for ordering, visiting, etc. then send them to the home page. This is why the page needs to load or be displayed before the redirect. Thanks.
Well, I do have a link there now, but I wanted it automated if possible.
What if ......... what if we just placed an include at the bottom of the page, and the page that we are including could have the pause / redirect code in its header? Would this work? Can you include a script like that and make it work? I have never tried it ...
For the record:
We have two different situations:
1. You want to redirect to the previous page.
2. You want to redirect to a specific page.
[1] You want to redirect to the previous page
There are a few problems with this one. Idealy you would be using javascript to do this, as the way to do it in that language is quite easy:
<script type="text/javascript">set
You mention however that people might not have javascript enabled. Besides this problem, also usability has always been a strong argument for the custom "go back/continue" hyperlink often seen on this type of pages. You DO want to notify your users on what is happening, so therefor you should always have some kind of line telling them what to do if all else fails.
Skipping the javascript, one other solution might be using $_SERVER['HTTP_REFERER'] to retrieve the previous page. However, increasingly, people have been disabling this feature from their webbrowsers because this would be saver in terms of privacy etc.
Thirdly you could try to use session to track your user. But then again, this might be total overkill to your situation. In this situation I would combine the first two methods:
<?php
if(!empty($_SERVER['HTTP_R
header('Refresh: 5,URL="'.$_SERVER['HTTP_RE
else
echo '<script type="text/javascript">set
?>
[2] Return to a specific page
Obviously, the best way to handle this is with a plain header('Refresh: ..'); like in the example above.
-r-
just for the record:
you can also put a meta redirect in the head section of the html, like
<html>
<head>
<meta http-equiv="REFRESH" content="15;url=http://www
...
this would redirect after 15 seconds to http://www.domain.com/inde
Yes, header("Refresh") is the same as meta refresh
http://www.desilva.biz/php
-r-
You can also solve this trough plain javascript
function redir($where,$time)
{
$ret ='
<body onload="redirect_delayed()
<script>
var isGO;
function redirect_delayed() {
if (isGO == true) {
location.href="'.$where.'"
} else {
isGO = true;
}
setTimeout("redirect_delay
}
</script>
';
return $ret;
}
I am sorry to be "the bearer of bad news here guys ...", and I certainly appreciate your help, but here are the issues that I raised in my initial question.
1.) One, many people may hav JS disabled.
2. ) The meta "refresh" is simply not supported in all browsers.
I am one of the poeple that is on the "standards" bandwagon! As you can notice with my sites, when I first started, I used to use HTML 3.2, then HTML 4.01 transitioanl (full of tables, some CSS, and JavaScript everywhere. Now, my "new" sites are XHTML 1.1 transitional & CSS strictly and they all validate! I make absolute sure of it! With this move in my business, I also wanted to investigate all options that would allow my websites to perform "uniformly" and look the same to each and every person that reads them ... even if it is a screen reader, which is why I embrace XHTML 1.1 so much. It is so easy for screen readers, humans and even SE "bots to read the pages. I have been having awesome results by using these standards in my coding.
Ok, I'll stop now ... :) :) Those are my reasons, maybe you can understand me.
There simply has to be a way to do this. I think. I have never come to experts-exchange, and left w/o an answer! I feel that if you throw enough programmers at an issue, surely someone will come up with a solution. Thanks again for all of the proposed solutions.
Regards,
-Matt V
Hello again!
Questions for "Roonaan" ...
Will your solution allow the page to load first? since it has that 5 second pause in it?
Another thing that I like about your solution is, if the refresh doesn't work, it will try JavaScript. Correct?
I guess that if I could just have a solution that would try a couple of different methods before it fails (E.g. header refresh or JavaScript, etc.) then that would be better than nothing.
Also with your proposed solution, would you be able to create a variable that would allow me to tell the script if it must go back one page of if it should go forward to a new page? If it must go forward, then we must also tell it the new address, else just use $referrer or if that fails try JavaScript.
This also refers back to my original post about this solution being "protable", allowing me to use the same script on different pages of my site, in different situations.
...thanks
if the header refresh is equivalent to meta refresh then its not a standard which i assume it what your after.
i too use xhtml now and in this case i wouls use javascript to redirect the page
but also have a noscript tag that says your browser does not support automatic redirection please click here etc.
Yes, you are right, I would like something that is a "standard".
... What if we wrote a php page that was actually a program itself. ???? What the h^%l do u say??? :) Let me explain further! :)
Ok, so we design a program that "includes" two files when it starts.
- File one contains the settings required to execute the script properly
E.g.
- time = 3 /* Refresh time in seconds */
- new = 0 /* =1 -> go to new page / =0 go back one page */
- new_url = http://domain.com/ /* If new=1 this is the new address */
- File two would contain a template for my site.
Next, our program would execute code "similar" to that provided by 'Roonaan':
<?php
if(!empty($_SERVER['HTTP_R
header('Refresh: 5,URL="'.$_SERVER['HTTP_RE
else
echo '<script type="text/javascript">set
?>
What I am trying to describe is a program that doesn't use the header call or JS, but is an actual program that will load the new page or the previous page via an actual request to the server for that particular page's URL, not just a simple redirect. This program would display our "thank you" page template or whatever via an include statement, then just print the page (make a dynamic page of sorts) that we included. I believe that this would allow us to move away from JS and header refresh tags, and into a program that is executed exclusively by the server, not by the client. The 'client' or browser in this case simply request our PHP page/program via link, then the server performes the rest of the functions. Our "programs" stdout would be our template.
Am I even on the right page? Let me know what you think. Thanks.
Hi again.
I do not actually understand your "php as a program" way.
What I would suggest is to have a combination of what I wrote originaly, and what you and others have suggested along the way.
Although the noscript part is the correct way to do it, I would leave it as is, and just have an ordinairy <p> instead of the <noscript>. Why? Because you want all the people to know what is going to happen, and not just those who have cancelled out javascript.
We even could call this a four stages missile :-)
1. Php header();
2. Meta tag
3. Settimeout
4. Manually
<?php
page_redirect(20, 'http://google.com/'); //goto google in 20 secs
//page_redirect(20); //goto previous page in 20 secs
function page_redirect($interval = 0, $redirect = false)
{
// use page_redirect(i) to goto history(-1) in i seconds,
// page_redirect(i, 'url') to forward
$use_meta_tag = false;
$use_settimeout = false;
$use_noscriptback = false;
$use_noscriptgoto = false;
if($redirect === false) //history.go(-1)
{
$refer = !empty($_SERVER['HTTP_REFE
? $_SERVER['HTTP_REFERER']
: '';
}
else
{
$refer = $redirect;
}
if(!empty($refer))
{
header('Refresh: '.$interval.';'.$refer);
$use_meta_tag =
'<meta'.
' http-equiv="Refresh"'.
' content="'.$interval.';'.
'URL='.htmlspecialchars($r
' />';
$use_settimeout =
'<script type="text/javascript">'.
'setTimeout("document.loca
addSlashes(htmlspecialchar
'\';",'.($interval+500).')
$use_noscriptgoto = $refer;
$use_noscriptback = false;
}
else
{
$use_meta_tag = false;
$use_noscript_back = true;
$use_noscript_goto = false;
$use_settimeout =
'<script type="text/javascript">'.
'setTimeout("history.go(-1
($interval+500).');</scrip
}
//Template
include "template.php";
}
?>
[[TEMPLATE]]
<?php echo '<'.'?xml version="1.0" encoding="iso-8859-1"?'.'>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtm
<html xmlns="http://www.w3.org/1
<head>
<?php if($use_meta_tag) echo $use_meta_tag; ?>
<title>You are being redirected</title>
</head>
<body>
<?php if($use_settimeout) echo $use_settimeout; ?>
<?php if($use_noscriptback) {?>
<p>Dear visitor, you are being automatically
redirected to the previous page. If this does not happen
within <?php echo $interval; ?> seconds, please manually
go to the previous page.</p>
<?php } ?>
<?php if($use_noscriptgoto) {?>
<p>Dear visitor, you are being automatically
redirected to the url below. If this does not happen
within <?php echo $interval; ?> seconds, please manually
go to this page:
</p>
<p style="text-align:center">
<a href="<?php echo htmlspecialchars($use_nosc
<?php echo htmlspecialchars($use_nosc
</p>
<?php } ?>
</body>
</html>
Hope this works out
Regards
-r-
Hello! I am sorry that it has taken me so long to get back with you. I finally got around to incorporating your suggestions into my thank you page today, and there only seems to be one small issue. :)
... there always is (just one small problem), isn't there ! :) :)
The page loads properly, but it does not seem to care how many seconds I specify as the interval. It just redirects immediately. I have tested the script both ways, and it works properly. Meaning if the url is left out, then it goes back one page, but if you supply a url, it goes to that url correctly. It just does it immediately.
I really appreciate your help with this Roonaan. It looks like it is going to work out great!
Regards,
-Matt V
Oh, damn it!! I always forget the important stuff!! ..........
I wanted to show you some things in case you wanted to look:
My thank you page: http://www.unique-wholesal
Your script (my implementation): http://www.unique-wholesal
My current Template: http://www.unique-wholesal
(see all of those tables ..., this is one of my previous designs before I started using Movable Type and stepped up to XHTML)
P.S. You will have to use a Gecko based browser such as FireFox to look at the template text file. IE loads it as an HTML page ...stupid, SPYWARE infested IE .... :(
Alright, haven't read EVERY comment, but I think I have a solution.
<?
$refresh_time = "5"; /* Seconds to refresh */
$refresh_new = "0"; /* 0 => New Page, 1 => REFERER. DEFAULT VALUE "0" */
$refresh_new_url = "http://www.newurl.com/new
$refresh_time = $refresh_time * 1000
if(!empty($_SERVER['HTTP_R
$refresh_new = "1";
}
if($refresh_new == "1") { /* REFERER */
?>
<script type="text/javascript">set
<noscript><a href="<?=$_SERVER['HTTP_RE
<?
} else if($refresh_new == "0") { /* New Page */
?>
<script type="text/javascript">set
<noscript><a href="<?=$refresh_new_url?
<?
}
?>
That should do it.
Hello, I am sorry, but I am extremely busy at this moment. I need to complete a Movable Type install / site design, then I can get back to this code which is for one of my sites.
If you don't hear from me in a couple of days, just post here again so I will get an email. Thanks for your help! I really appreciate it. :)
Regards,
-Matt V
Business Accounts
Answer for Membership
by: jkna_gunnPosted on 2004-12-15 at 23:07:33ID: 12837975
you can use usleep() to pause for a specified time
usleep(5000000); // 5 seconds
normally in php you would use Header('Location: $url') to redirect
but it sounds like you want to show the page contents first then redirect.
im not sure how to do that without javascript or meta. sorry.