Question

Is there anyway for me to make it work so anyone who presses F5 or any refresh button will be moved to a different page, instead of it refreshing the page the user wants?

Something like :

If (refresh){

  goto "link to hopme page"

}

If not is there anyway for me to not allow refreshing on a certain page?

I have some people that are are just refreshing non stop and it is killing my bandwidth. It is a game site so I don't want to ban the ip's.

Was it helpful?

Solution

session_start();
if($_SESSION['hasbeenhere'] == 1)
{
 // Page refreshed
}
else
{
   $_SESSION['hasbeenhere'] = 1;
}

If the person doesn't have cookies enabled, this will fail. If someone goes to another page and comes back, it will shown as refreshed.

Overall, you can't do this in a way that is surefire, but this is 1 way to prevent someone from seeing the same page twice.

Because of your comment, if you want to stop people from pressing F5 200 times, try this.

$page = $_SERVER['REQUEST_URI'];
// Defaults
if(!isset($_SESSION[$page]['count']))
{
    $_SESSION[$page]['count'] = 1;
    $_SESSION[$page]['first_hit'] = time();
    $_SESSION[$page]['banned'] = false;
}
else
{
    $_SESSION[$page]['count']++; // Increase the counter
}

// If person is banned, end script
if($_SESSION[$page]['banned'] == true)
{
    die();
}

if($_SESSION[$page]['first_hit'] < time() - 30)
{
    $_SESSION[$page]['count'] = 1; // Reset every 30 seconds
}

if($_SESSION[$page]['count'] > 100)
{
    $_SESSION[$page]['banned'] = true; 
    // Ban if they hit over 100 times in 30 seconds.
}

OTHER TIPS

Perhaps you should be focusing your effort instead on reducing the bandwidth your page is using. Explore the areas of image compression, page optimization and caching.

Why would you want to?

But no, there's no way that'll work consistently that can stop this.

In your PHP code, do the following:

 if(isset($_SESSION["pagename-LAST_VIEWED"])) {
    v = $_SESSION["pagename-LAST_VIEWED"])
    if(time() - v < 15) {
       // user is refreshing more than once per 15 seconds
       // send them something else and die
       }
 }
 $_SESSION["pagename-LAST_VIEWED"] = time();

Please ignore my crummy pseudo-PHP, it's not my daily language.

This will prevent both a page refresh (F5) and the user just clicking the bookmark again or pressing Enter in the address bar again.

You could also enable some aggressive caching meta tags and HTTP headers, which will prevent some refreshes from ever hitting your server, but the above code should be pretty scalable.

Another thing to consider: the root problem is your other code, not your users. Consider rewriting your page so it auto-updates the part they want to see via AJAX on a timed delay. This will give them incentive not to use Refresh, and will help your server cope by only having to refresh the small bit of data they want to see updated.

I have no idea if this would work, but could you listen for keystrokes with javascript, and on F5 keypress, do what you want.

I guess you could do this:

session_start();

if (isset($_SESSION['visited'])) {
    header("Location: http://the.page/you/want");
} else {
    $_SESSION['visited'] = true;
}

In order to stop F5 from refreshing, you'd have to not allow refreshing of any type. As a hack, you could try putting a timestamp in the querystring. On load, you can check the value of the timestamp and if it is in the past, redirect to another page. This would work if cookies are disabled.

The site itself does this with Javascript, so when you're editing something, you don't loose it by refreshing, going back, etc. There's probably some event defined by JQuery that lets you do this, so look into that, and on that event, redirect to the page you want (probably has to be within the same domain).

It seems to me that there is no good technical solution to this problem (the other answers covered that pretty well). If the users are constantly hitting refresh on this page, I'm guessing it's because they expect the page to change and they want up-to-the-second information. Perhaps you should take away their reason for wanting to refresh the page. The easiest way to do this would probably be making the page update itself (likely via AJAX). Thus the user can sit there and updates will just roll in -- no more hammering F5, which will cut down on your bandwidth, not to mention be a better user experience.

I just find out a very simple solution.

For me after i process all the $_POST information and save the ones i need into a $_SESSION var i do this:

$_SESSION['admin']['PID']=session_id();
$_SESSION['admin']['user_user']=$_POST['user'];
$_SESSION['admin']['user_pass']=$_POST['pass'];
$_SESSION['admin']['last_access_time']=time();
if($_SERVER['REQUEST_METHOD']==="POST")
{
    header("LOCATION:".$_SERVER['PHP_SELF']."?p=redirected");
}

In my case i was trying to validate the user, and i have the problem that when someone logout and and go back and refresh the site load again, and with this simple if i finally solved it.

PD: ."?p=redirected" is only for testing porpouse

It's not answering your question directly but you can probably solve your issue with caching can't you?

If you send a header Cache-Control: public,max-age=120 That should instruct the browser that it doesn't need to request the page again for 2 minutes and can just use the copy from it's cache. If course they can use control-f5 to force a refresh but it ought to help a lot?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top