1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Simple PageViews Counter

Discussion in 'PHP' started by Izaan, Apr 17, 2009.

  1. Izaan

    Izaan New Member

    Joined:
    Oct 16, 2007
    Messages:
    215
    Likes Received:
    2
    Trophy Points:
    0
    I just created this for one of my client

    Create A File Called counter.txt With CHMOD 777 In The Root Directory

    $Visits = file_get_contents("counter.txt");
    file_put_contents("counter.txt", $Visits++);

    It works great like statcounter and other programs which also does not do much like unique ip tracking and other stuff.
     
  2. varaprasadreddy

    varaprasadreddy New Member

    Joined:
    Mar 19, 2009
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    dear izaan we can create counter.text easily but how to create "counter.txt With CHMOD 777"
    and where can we use that script "i understood but i mean that please show me in a html page in a real scenario to download that script & then i can use in my page "
    please help me boss.
    waiting for your reply.
     
  3. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83
    counter.txt with CHMOD 777 means it should be read and writable by the PHP script.

    In windows you need to set the needed permission using IIS and for Linux its simply changing the file permission.

    I have not tested but it should work with chmod 666 as well as you do not need to execute anything.
     
  4. PradeepKr

    PradeepKr New Member

    Joined:
    Aug 24, 2010
    Messages:
    24
    Likes Received:
    0
    Trophy Points:
    0
    Home Page:
    I really doubt it worked.

    PHP:
    $Visits++
    increments after it has already written the old count in the file. Hence no increment. Number would remain same.

    change it to
    PHP:
    ++$Visits
     
  5. pein87

    pein87 New Member

    Joined:
    Aug 6, 2010
    Messages:
    173
    Likes Received:
    47
    Trophy Points:
    0
    Occupation:
    Web Dev
    Location:
    Limbo
    If you want it to keep track of IP's why not have it hold it as an array in a separate file.

    PHP:
    <?php

    define
    ('COUNTER_FILE','counter.txt'); // define counter file
    define('IP_FILE''ip_log.txt'); // define ip log

    $ips file_get_contents(IP_FILE); // get file content as a variable

    $visitCount file_get_contents(COUNTER_FILE); // get page views

    $seperator " "// defaults to a space but change as you see fit

    $ipRanges explode($seperator$ips); // transform into an array

    $currentUsersIp $_SERVER['REMOTE_ADDR']; // get current IP


    if (!in_array($currentUsersIp$ipRanges// check if the value is not in an array
    {
     
    // add it to array, save array it to a file and update file counter

    $visitCount $visitCount 1// add one to the current visitor file
    file_put_contents(COUNTER_FILE$visitCount); // write it to file
    file_put_contents(IP_FILE$seperator $currentUsersIpFILE_APPEND); // add ip to ip log 
     
    echo file_get_contents(COUNTER_FILE); // get visits including this new visit

    }
    else 
    // is in array dont count it as a visit
    {
    // show current page views

    echo $visitCount;
    }

    ?>
    Its not a complete solution but should work just fine if you want views to be unique based on ips.
     
    Last edited: Sep 16, 2010

Share This Page