Results 1 to 8 of 8

Thread: A script to allow only a certain amount of users on a webpage?

  1. #1
    Join Date
    Jan 2007
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default A script to allow only a certain amount of users on a webpage?

    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?

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    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!

  3. #3
    Join Date
    Jan 2007
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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?

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    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!

  5. #5
    Join Date
    Jun 2006
    Location
    Acton Ontario Canada.
    Posts
    677
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    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.
    - Ryan "Boxxertrumps" Trumpa
    Come back once it validates: HTML, CSS, JS.

  6. #6
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    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!

  7. #7
    Join Date
    Jun 2006
    Location
    Acton Ontario Canada.
    Posts
    677
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    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);
    ?>
    untested. flatfile DB. 300 requests every 5 min(300 sec.)
    - Ryan "Boxxertrumps" Trumpa
    Come back once it validates: HTML, CSS, JS.

  8. #8
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    $contents = @file_get_contents("includes/online.txt"); 
    $array = explode("\r\n",$contents);
    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.
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •