Thread: Counter

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    11
    Rep Power
    0

    Counter


    I have a php code that counts everytime a someone clicks on the link.. Where can I put this php page on my site so that it will count my visitors? Here is the code

    PHP Code:
    <?php
    $filename 
    'counter.txt';
    if (!
    $_COOKIE['counter']) {
     
    setcookie("counter"1time()+3600);
     
    $handle fopen($filename'r');
     
    $hits trim(fgets($handle)) + 1;
     
    $handle fopen($filename'w');
     
    fwrite($handle$hits);
    } else {
     
    $handle fopen($filename'r');
     
    $hits trim(fgets($handle));
    }
    fclose($handle);
    echo 
    $hits;
    ?>
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Location
    Saint-Petersburg, Russia
    Posts
    236
    Rep Power
    28
    If you want it to count the clicks on the link, you obviously could not put it into page itself (since you'll be counting page loads then).

    The trick is that you need to add "onclick" action to your link and write JavaScript function which performs ajax request to this code (which should be put into some separate file). After ajax request is completed, javascript function should end and return "true", so that browser will proceed by the link "href" to its target.

    Other idea could be to make your link pointing to page with counter and providing real target url in the parameter, so that user is firstly switched to counter page and the counter then redirects him to his target. This variant requires no javascript, but link hrefs would not be pointing to outer sites.
    CodeAbbey - programming problems for novice coders
  4. #3
  5. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,692
    Rep Power
    6351
    Or just...install google analytics. It's ok if google is smarter than you, they're smarter than everyone.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.

IMN logo majestic logo threadwatch logo seochat tools logo