[Web-Development] REFRESH, REDIRECT, ... which?

Grant G. Root [Address Concealed]
Wed Jun 6 00:20:37 EDT 2007


On 4 Jun 2007 at 10:11, chuck nc8q wrote:

> I am trying to gather some information on who is visiting my web site.
> 
> I have a .php program that does this whenever someone loads my default page.
> 
> I would like to redirect/refresh their browser so that it will only
> load the [main] default page once per [hour,day] and then all other
> default page loads will be from another [default] HTML file.  I want
> the .php to run once per visit and not to run again during the current
> session(s). 

Chuck, if you'll help me understand more specifically what you want to do 
and why, I might have some suggestions.

The "once per session" part can be done easily in a single PHP page. 
Something like this:

<?php
session_start();
if (!isset($_SESSION['beenthere']) {
    $_SESSION['beenthere'] = true;
    // The following will be done once per session.
    ...
}
?>
<html>
...

>  I am using the <meta http-equiv="REFRESH"
> content="0;url=http://www.the-domain-you-want-to-redirect-to.com">
> now. However I see a warning about this breaking the 'back' button. I
> think I know why some web sites won't let me leave by suing the 'back'
> button. :-| 

You can also look at using the Location response header to do the 
redirect -- it's a little cleaner than the meta refresh, as you won't see the 
first page "flash" before the redirect. Just be sure to use an absolute 
URL, to meet the HTTP spec:

<?php
header("Location: http://www.example.com/mypage.html");
exit;
?>

or

<?php
/* Redirect to a different page in the current directory */
$host  = $_SERVER['HTTP_HOST'];
$uri   = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
$extra = 'mypage.html';
header("Location: http://$host$uri/$extra");
exit;
?> 

-- 
Grant Root
http://www.rootcentral.org/grant/

~~~~~ Randomly-Selected Thought for the Day ~~~~~
"Our position is that whatever grievances a nation may have, however
objectionable it finds the status quo, aggressive warfare is an
illegal means for settling those grievances or for altering those
conditions." -- Robert Jackson, Supreme Court justice and U.S.
representative to the Nuremberg Tribunal after WWII





More information about the Web-Development mailing list