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

[REQ] Click tracking script to track clicks

Discussion in 'PHP & Perl' started by matt1972, Mar 30, 2010.

  1. matt1972

    matt1972 Regular Member

    Joined:
    Nov 19, 2009
    Messages:
    499
    Likes Received:
    206
    Occupation:
    To make money
    Location:
    192.168.0.1
    I'm in need of a good click tracking script to track my click for Clickbank. Anyone have anything they could share? Thanks.
     
  2. PostWhore

    PostWhore Registered Member

    Joined:
    Jul 22, 2008
    Messages:
    51
    Likes Received:
    2
    Here is the simplest solution that will count a click and then redirect to a URL.
    Create a "clicks.txt" in the same place as the PHP script.


    PHP:

    <?php

    $fname 
    "clicks.txt";
    $fsize filesize($fname);

    $fp fopen($fname'r');
    $clicks fread($fp,$fsize);
    fclose($fp);

    $clicks += 1;

    $fp fopen($fname'w+');
    fwrite($fp$clicks);
    fclose($fp);

    header('Location: http://www.YOUR URL.com');


    ?>



     
  3. nezZario

    nezZario Newbie

    Joined:
    Mar 30, 2010
    Messages:
    5
    Likes Received:
    2
    Occupation:
    Web Developer, Business Owner
    Location:
    North-West Indiana
    This is a shorter version but requires PHP5 or greater. I would suggest doing a database solution instead.

    PHP:
    <?php
        $file 
    'clicks.txt';
        
    $clicks file_get_contents($file);
        
    file_put_contents($file, ($clicks+1));
        
        
    // The rest of your script would go here
        // OR to redirect them to the URL:
        
    header("Location: http://www.google.com");
    ?>

    Or you can use the SQL version I whipped up. Create this mySQL table:

    Code:
    CREATE TABLE clicks` (
    `click_id` INT( 11 ) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,
    `click_url` VARCHAR( 1000 ) NOT NULL ,
    `click_time` INT( 11 ) UNSIGNED NOT NULL ,
    `click_ip` VARCHAR( 16 ) NOT NULL
    ) ENGINE = MYISAM ;
    And use this code whereever you need it:

    PHP:
    <?php
            $c 
    mysql_connect('localhost''your sql user''your mysql pass') or die("Error connecting to mySQL: " mysql_error());
            
    mysql_select_db('your mysql db') or die("Error selecting database: " mysql_error());

            function 
    smart_quote($q) {
                    return 
    "'" mysql_real_escape_string($q) . "'";
            }

            
    mysql_query("INSERT INTO `clicks` SET `click_url` = " smart_quote($_SERVER['REQUEST_URI']) . ",
                    `click_time` = " 
    smart_quote(time()) . ",
                    `click_ip` = " 
    smart_quote($_SERVER['REMOTE_ADDR'])) or die("Couldn't execute query: " mysql_error());
    ?>
    Tested and working. If you want to count the clicks, you'll need to either look in your database manually, or make a short script to SELECT.

    Thanks, Ben
    http://www.ntertech.com/
     
    • Thanks Thanks x 1
  4. Subject

    Subject Power Member

    Joined:
    Dec 26, 2007
    Messages:
    656
    Likes Received:
    303
    Location:
    Living With Articles!!
    nezZario, thanks for the script. It is working good.

    1. It also tracks .csss or .js files, need to avoid that... How we can add dont track some specified folders or files?
    2. Importantly, it is not tracking outgoing urls. It only tracks site urls.. Any idea we can add outgoing url tracking?
    3. Any idea how to get a report of most clicked urls?

    Thanks
     
  5. nambooooo

    nambooooo Regular Member

    Joined:
    May 21, 2009
    Messages:
    219
    Likes Received:
    71
    Location:
    GreenMTN
    Wouldn't prosper202 do the same and more?
    Posted via Mobile Device
     
  6. Velvet

    Velvet Junior Member

    Joined:
    Feb 18, 2009
    Messages:
    199
    Likes Received:
    146
    prosper202 is very good for this and free