Count image/link clicks/hits

Discussion in 'PHP' started by bondigor69, Jun 18, 2012.

  1. #1
    Hello guys.
    Im not that good in php, just learning and I try to make a simple script.

    I want to count how many times an image has been clicked on each URL and echo the count beside the image.

    I have the same Image on each url(share image)

    Please help

    It has been 2 days that Im looking around and writing scripts. :shrug: :'(
     
    bondigor69, Jun 18, 2012 IP
  2. NetStar

    NetStar Notable Member

    Messages:
    2,471
    Likes Received:
    541
    Best Answers:
    21
    Trophy Points:
    245
    #2
    Maybe you should take some more time to read tutorials and experiment with code before asking. Take no offense, but this should come across as easy to do and if you have no idea how to do it you most likely won't know how to implement the code if someone told you in plain English.

    To keep track of Hits of an image you have a couple different methods.

    Method 1
    - Requires Database. You should have a Table named "Pictures" or something similar storing at least a PictureID (char), PictureURL (char), and Hits (int).
    - Write a PHP script that accepts a PictureID as a param (blah.php?id=whatever) then fire off an SQL to fetch the PictureURL and another to update the row by adding another hit.
    Example of update: UPDATE Pictures SET hits = hits + 1 WHERE pictureid = ?;
    Example of fetch: SELECT pictureurl FROM Pictures WHERE pictureid = ?;
    Then display the picture via the IMG Html tag.

    Method 2 - Counting actual hits requested directly to the file.
    - Requires SQL with the same configuration in Method 1 + another column called "PicturePath".
    - Use .htaccess to rewrite JPG urls to PHP script with an appended param which contains the picture file name.
    - In your PHP script use the param (picture file name) as an ID to look up the picture path in the database.
    - Add a hit with the above SQL.
    - In your php script open the picture and print it to the output with a JPG MIME.
    - This method will keep track of requests to the picture.

    Method #1 should technically be enough.
     
    NetStar, Jun 18, 2012 IP
  3. NetStar

    NetStar Notable Member

    Messages:
    2,471
    Likes Received:
    541
    Best Answers:
    21
    Trophy Points:
    245
    #3
    Method 1 Example (untested...but it's so generic it probably works)

    
    <?php
    
    //
    // Your database information
    //
    $dbname = "MySite";
    $dbuser = "blah";
    $dbpass = "pass123";
    
    
    //
    // Fetch id (pictureid) from param (ie. blah.php?id=54 - 54 would be the pictureid)
    //
    $pictureid = "";
    if (isset($_GET['id'])) $pictureid = $_GET['id'];
    
    if (!$pictureid)
    {
       print "Missing id!\n";
    } else
    {
       //
       // Connect to database
       //
       // Note: Should check to see if connected...but this is merely an example
       $db = new PDO("mysql:dbname=".$dbname.";host=localhost", $dbuser, $dbpass);
    
       //
       // Fetch PictureURL
       //
       $sql = "SELECT pictureurl FROM Pictures WHERE pictureid = :pictureid;";
       $sth = $db->prepare($sql);
       $sth->bindParam(":pictureid", $pictureid);
       $sth->execute();
    
       $pictureurl = "";
       $sth->bindColumn('pictureurl', $pictureurl);
    
            
       if (!$pictureurl)
       { // No picture!
          print "Invalid id!\n";
       } else
       { // We found a picture
           //
           // Update Counter
           //
           $sql = "UPDATE Pictures SET hits = hits + 1 WHERE pictureid = :pictureid;";
           $sth = $db->prepare($sql);
           $sth->bindParam(":pictureid", $pictureid);
           $sth->execute();
    
           // Print image html
           print '<img src="' . $pictureurl . '" border="0" alt="" />\n';
       }
    }
    
    ?>
    
    PHP:
     
    NetStar, Jun 18, 2012 IP
  4. Custom IDX/MLS

    Custom IDX/MLS Peon

    Messages:
    23
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Rewrite .htaccess to point image files to PHP would give a hit every time the image's loaded, not clicked. If you want to catch the actual clicks, pass one or more parameters indicating that the link is a linked image e.g. <a href="/asd.htm?l=img"><img src="/image.png" alt="img" /></a> and count these parameters in the catching page. Assumed you've got a template-based interface, counting clicks would not pose a problem. Might have some SEO ramifications though.
     
    Custom IDX/MLS, Jun 19, 2012 IP