Limit To X Views Then Execute Code Every View After

Discussion in 'PHP' started by gahoo, Jan 18, 2008.

  1. #1
    I want to limit how many times a visitor can view a PHP file, and if they exceed the limit then it executes a block (not sure what you call it in PHP?).

    How would I do this? Thanks!
     
    gahoo, Jan 18, 2008 IP
  2. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #2
    If they have to log in, then you can record it in the database. Otherwise you have to track it in a cookie and they can alter the count (or at least reset it) if they want to.
     
    SmallPotatoes, Jan 18, 2008 IP
  3. gahoo

    gahoo Peon

    Messages:
    837
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Not a very technical crowd, cookies or IP tracking will work.
     
    gahoo, Jan 18, 2008 IP
  4. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #4
    It's easy with a cookie then. Roughly...

    
    $view_limit = 5; // set to whatever you want
    $times_viewed_already = intval($_COOKIE['view_count']);
    if ($times_viewed_already) > $view_limit
    {
       $allowed_to_view = false;
    }
    else
    {
       $times_viewed_already++;
       setcookie('view_count', $times_viewed_already, strtotime('Jan 1, 2020 12:00:00'));
       $allowed_to_view = true;
    }
    
    PHP:
    Note that you have to do this near the beginning of your script, before generating any other output, because PHP won't let you set a cookie after you've sent any HTML to the browser.
     
    SmallPotatoes, Jan 18, 2008 IP
  5. HuggyStudios

    HuggyStudios Well-Known Member

    Messages:
    724
    Likes Received:
    20
    Best Answers:
    26
    Trophy Points:
    165
    #5
    Use sessions, and store that information in a database alot more secure.
     
    HuggyStudios, Jan 18, 2008 IP
  6. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #6
    No more secure than cookies. Anyone can drop their session and start from scratch. The only more secure approach is to ensure that each user has a unique login id.
     
    SmallPotatoes, Jan 19, 2008 IP
  7. jayshah

    jayshah Peon

    Messages:
    1,126
    Likes Received:
    68
    Best Answers:
    1
    Trophy Points:
    0
    #7
    Tie their IP to their session server side, then.
     
    jayshah, Jan 19, 2008 IP
  8. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Takes me about 10 seconds to change my IP (hit the bookmark for my router's web interface, click "disconnect PPPoE session", done).

    Also, every mobile device user who uses my telephone company shares the same single IP, as we are all channeled through a proxy server. Should only one person among all their million customers be allowed in?

    I suppose this whole debate depends on how important it really is that people only be allowed to view the page a limited number of times.
     
    SmallPotatoes, Jan 19, 2008 IP
  9. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #9
    Make it members only, then do a increment in the database for that member.
     
    Kaizoku, Jan 20, 2008 IP
  10. jayshah

    jayshah Peon

    Messages:
    1,126
    Likes Received:
    68
    Best Answers:
    1
    Trophy Points:
    0
    #10
    Every point has its own vulnerability. Proxies allow everything to be defeated, so no single solution will achieve 100% success. Even if you mixed Cookie (Session), IP and Users Logging In/Out of a system, nothing is stopping them loading a proxy and then re-registering. Also, you can't detect the proper (HTTP) proxies so I wouldn't waste time with a detection system.

    Jay
     
    jayshah, Jan 20, 2008 IP
  11. pubdomainshost.com

    pubdomainshost.com Peon

    Messages:
    1,277
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Though nothing can be 100% secure, it does work as a deterrent and keeps most of the folks away, some persisting one's would not give up. Having said that - design site for majority and not the minority. Start with one of the many options provided and scale it up to include other options. If I were in your place, I would start with IP address in session and after a while block the IP address. Since I don't visit only one site, I would rather hate to keep toggling my IP after every 5 mins (and break sessions with other sites ... not a very wise choice, unless I have only one aim to keep coming to only ONE site through out the day) ...
     
    pubdomainshost.com, Jan 20, 2008 IP