Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3

Thread: Hit Counter

  1. #1
    New Coder
    Join Date
    Mar 2003
    Location
    USA
    Posts
    99
    Thanks
    24
    Thanked 0 Times in 0 Posts

    Hit Counter

    I can make a hit counter, but I'm just looking for tips on how I can make it so it's not abused (refresh). How can I make it so only 1 click per IP? So if you visited a url it would say "total views: 5" and if you refreshed, it would stay at 5. Only increasing to 6 from the next unique visitor.

  • #2
    Regular Coder Zangeel's Avatar
    Join Date
    Oct 2007
    Location
    public_html/
    Posts
    638
    Thanks
    17
    Thanked 79 Times in 79 Posts
    Blocking via IP would suck because it would only count uniques infinity. A better option is to use sessions, that way they can't refresh refresh refresh, basically when the session expires (usually when they exit a browser) then it'll count if they revisit the page.

    Here's what I use:

    PHP Code:
    if (!isset($_SESSION['iVisited'])) {
        
    $_SESSION['iVisited'] = 1;
        
    $counterfile "counter.txt";
        
    $fp fopen($counterfile"r");
        
    $hits fgets($fp10000);
        
    fclose($fp);
        
    $hits++;
        
    $fp fopen($counterfile"w");
        
    fputs($fp$hits);
        
    fclose($fp);
        echo 
    '<code>This is you\'re first visit: ' $hits ' hits</code>';
    } else {
        
    $counterfile "counter.txt";
        
    $fp fopen($counterfile"r");
        
    $hits fgets($fp10000);
        
    fclose($fp);
        echo 
    '<code>You\'ve been here before: ' $hits ' hits</code>';

    Then create counter.txt in the same directory, make sure it's writeable.
    PHP Code:
    $aString is_string((string)array()) ? true false// true :D 
    [/CENTER]

  • Users who have thanked Zangeel for this post:

    Blatch (09-01-2009)

  • #3
    New Coder
    Join Date
    Mar 2003
    Location
    USA
    Posts
    99
    Thanks
    24
    Thanked 0 Times in 0 Posts
    Thanks


  •  

    Posting Permissions

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