Can anyone please show me a php script that only allows 300 users on a page at once, but makes the 301st user and so on be redirected to another page?
Can anyone please show me a php script that only allows 300 users on a page at once, but makes the 301st user and so on be redirected to another page?
Since HTTP is stateless, it's impossible to do this. The best you can do is only allow the page to be requested 300 times every, say, five minutes.
Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!
To rephrase my first post, I kinda want a function like the one in FTP that only allows the first x users, then blocks the rest until one of the first x users leaves.
If I can't do what I meant in the first post, how would I be able to do what you suggested?
Store a list of last-seen times in a database. Each time somebody accesses the page:
- Check if there are more than 300 times in the database
- If so, redirect the user
- Otherwise:
- Add the current time to the database
- Remove any times more than five minutes ago from the database
- Serve the page
Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!
logical error in there twey.
once you have 300 people, noone can get in, and the database isnt checked for timeouts.
This:
Remove any times more than five minutes ago from the database
Should be first.
Ah! You're right.
- Remove any times more than five minutes ago from the database
- Check if there are more than 300 times in the database
- If so, redirect the user
- Otherwise:
- Add the current time to the database
- Serve the page
Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!
untested. flatfile DB. 300 requests every 5 min(300 sec.)PHP Code:<?php
$contents = @file_get_contents("includes/online.txt");
$array = explode("\r\n",$contents);
$curr = time();
foreach($array){
$dif = $curr-$array;
if($dif >= "300") {
unset($array);
}
};
$num = count($array);
if($num >= "299") {
$array[] = $curr;
?>
WEB PAGE GOES HERE
<?php } else { ?>
Alternate web page/meta redirects
<?php
};
$towrite = implode("\r\n",$array);
$handle = fopen("includes/online.txt","w+");
fwrite($handle,$towrite);
fclose($handle);
?>
Rather, $arr = file('includes/online.txt'); The former will break on UNIX-based servers, which use \n rather than \r\n to signify a linebreak.Code:$contents = @file_get_contents("includes/online.txt"); $array = explode("\r\n",$contents);
Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!
Bookmarks