views:

1123

answers:

11

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.

+4  A: 

Why would you want to?

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

mattl
I have some people that are are just pressing the F5 button non stop and it is killing my bandwidth
yaar zeigerman
Then ban those IP addresses
Andrejs Cainikovs
There are to many and I want most of them this is for a game site.
yaar zeigerman
Try my answer, it bans people who refresh 100 times in 30 seconds.
Chacha102
Use the Apache module iplimit, maybe?
mattl
module iplimit? care to explain?
yaar zeigerman
http://dominia.org/djao/limitipconn2.htmlLimits the number of times resources can be accessed by a single IP. People refreshing constantly would get an error telling them they're doing it wrong (or words to that effect)
mattl
+1  A: 

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

Magic Hat
+7  A: 
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.
}
Chacha102
Well if they go to a different page and then come back to this page will the count go up? or is this only for refreshing? since I don't mind them going to a different page and then coming back
yaar zeigerman
This only works for the current page, let me modify it
Chacha102
There, try that. It might need a little modification, but all the basics are there.
Chacha102
They can only visit a specific page 100 times in 30 seconds. So it shouldn't matter if they go and come back, they should visit it 100 times in 30 seconds.
Chacha102
Will this work even ff the person doesn't have cookies enabled?
yaar zeigerman
No. You would need to keep track of the IP for that. Which you might want to install Apache IPlimit for.
Chacha102
A: 

I guess you could do this:

session_start();

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

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.

sideproject
**@sideproject:** Since the time the link will be built will always be in the past relative to the time the link is clicked, this will not work.
Andrew Moore
Agreed. Best to go with another solution.
sideproject
A: 

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).

Dana the Sane
+11  A: 

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.

Gav
I am working on that as well but I need a solution for now.
yaar zeigerman
Good call. If your host is using Apache servers, it is highly likely you will have `mod_deflate` and `mod_gzip` at your disposal. Configured correctly, these will compress textual content (like HTML, Javascript, CSS). If you are using PHP, perhaps have a look at `ob_start("ob_gzhandler");`.
Gav
+3  A: 

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.

richardtallent
A: 

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.

rmeador
A: 

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 alt-f5 to force a refresh but it ought to help a lot?

John Burton
A: 

Hi,

I tried apllying your code example for ASP.Net page to track the refresh of page using C#. But, the code sample what you have given does not work.

Let me know, if you have any working sample code which can be used to test the refresh scenario.

Thanks in advance. Hirak

Hirak