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 10 of 10
  1. #1
    Senior Coder
    Join Date
    May 2006
    Posts
    1,673
    Thanks
    28
    Thanked 4 Times in 4 Posts

    Is there a way to log image usage with php ?

    Hi,


    I am giving cleints an image to use as a kind of "trust seal" and I want
    to record the number of times it is diplayed, is that possible with
    php, of is this a javascript project ?

    This is what I mean:

    I will give a client this code to place on their website:

    PHP Code:
    <!-- //SF-Code---somewstuff here--// --> 
    <a href="http://www.my-website.com/check.php?key=$sup_id\" 
     onclick=\"NewWindow(this.href,'','800','600','yes','default'); return false\" 
     onfocus=\"this.blur()\" >
     <img src=\"http://www.my-website.com/images/logo2.gif\" 
     alt=\"Membership Click to Verify - Before you Buy\" 
     border=\"0\" >
    </a> 
    Now I will be recording the number of times the image is clicked
    on, but I also want to record the number of times that the
    image is downloaded from my website.

    How would I do this ?

    Thanks
    Last edited by jeddi; 08-23-2009 at 12:53 PM.

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,471
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    On this line:
    <img src=\"http://www.my-website.com/images/logo2.gif\"
    alt=\"Membership Click to Verify - Before you Buy\"
    border=\"0\" >


    You use a PHP script to display the image and at the same time, do some sort
    of logging. PHP will use GD for rendering the image, and you'll also give it some
    sort of code specific to each website, so you know what site is displaying the image.

    Also note that it will log each time the page is refreshed, so you will not get an
    accurate count ... it works the same way as a site counter ... you don't know if it's
    the same person refreshing the page or not.

    Your line would look like this:
    <img src=\"http://www.my-website.com/click.php?id=372348278326\"
    alt=\"Membership Click to Verify - Before you Buy\"
    border=\"0\" >


    Your PHP script called "click.php" would render the image and do something
    with the ID that is given. You probably have a MySQL database of all accounts
    that use your image and the ID is a client number of some kind?

    Use Google to find: PHP GD to see how to render that .gif image.

  • #3
    Senior Coder
    Join Date
    May 2006
    Posts
    1,673
    Thanks
    28
    Thanked 4 Times in 4 Posts
    Thanks for your reply,

    OK - I have changed the <img> tag so that it now runs a script.

    like this:

    PHP Code:
    <!-- //SF-Code---somewstuff here--// --> 
    <a href="http://www.my-website.com/check.php?key=$sup_id\" 
     onclick=\"NewWindow(this.href,'','800','600','yes','default'); return false\" 
     onfocus=\"this.blur()\" >
     <img src=\"http://www.my-website.com/image_load.php?id=$sup_id\" 
     alt=\"Membership Click to Verify - Before you Buy\" 
     border=\"0\" >
    </a> 

    and the scrip updates the product database:

    PHP Code:
    <?php 
    /*
    *  image_load.php
    *
    * Called by the sales page
    *
    */        
            
    $prod_id=$_GET["id"];
         
    $sql "SELECT image_count FROM products WHERE prod_id = '$prod_id' ";
    $result mysql_query($sql)    or die("could not execute FIND CLIENT query"). mysql_error();  

    $num mysql_num_rows($result);

    if (
    $num == ) { //  if the  client does not exist
       
    $message1 "That user id <br>
      is not registered on the system.<br>
      "


    require_once(
    "check_err.php");
      exit(); 
    }  
    // endif    
                        
    $sql "UPDATE products SET 
             image_count   = image_count+1
             WHERE prod_id = '$prod_id' "
    ;
        
    mysql_query($sql) or die("could not execute PROFILE update query"mysql_error());

    HERE DO THE 
    imagecreatefromgif
    (img/logo2.gif);

    AND 
    DISPLAY IT.

    how ???
    ?>
    My question is though, why do the
    imagecreatefromgif function ?

    Why can I not just echo the image like this ?
    PHP Code:
    echo "<img src=\"http://www.support-focus.com/img/logo2.gif>"

    Also, if I have to do the GD imagecreatefromgif() function ,

    how do I display the image aferwards ? is it with the
    <img > tag - brings me back to the first question

  • #4
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,471
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    The <img src> on your main page is what "loads" the image to the browser.
    It's expecting a .gif (or binary) file. So the PHP script that it calls generates
    that .gif file as binary data. The main page doesn't know or care where it came
    from, just that it's a .gif file. If you try to echo it in the PHP script, it will echo it
    on a blank page ... as it creates it's own header, etc.

    If you don't want to use GD, you can try out-putting the image like this:


    header('Content-Type: image/gif');
    @readfile( 'img/logo2.gif' );

    So this would be your script:
    PHP Code:
    <?php 
    /*
    *  image_load.php
    *
    * Called by the sales page
    *
    */        
            
    $prod_id=$_GET["id"];
         
    $sql "SELECT image_count FROM products WHERE prod_id = '$prod_id' ";
    $result mysql_query($sql)    or die("could not execute FIND CLIENT query"). mysql_error();  

    $num mysql_num_rows($result);

    if (
    $num == ) { //  if the  client does not exist
       
    $message1 "That user id <br>
      is not registered on the system.<br>
      "


    require_once(
    "check_err.php");
      exit(); 
    }  
    // endif    
                        
    $sql "UPDATE products SET 
             image_count   = image_count+1
             WHERE prod_id = '$prod_id' "
    ;
        
    mysql_query($sql) or die("could not execute PROFILE update query"mysql_error());

    // Define the correct path to "logo2.gif" ...
    header('Content-Type: image/gif');
    @
    readfile'img/logo2.gif' );

    ?>

  • #5
    Senior Coder
    Join Date
    May 2006
    Posts
    1,673
    Thanks
    28
    Thanked 4 Times in 4 Posts
    Hi,

    Ok , thanks for the advice.

    I tried putting this in the code but the image does not
    show.
    PHP Code:
    // Define the correct path to "logo2.gif" ...
    header('Content-Type: image/gif');
    @
    readfile'img/logo2.gif' ); 
    I suspect it is because it is looking on the sales pages
    server instead of on my-site server.

    Can I put a url in that readfie() function ?

    eg:
    @readfile( 'http://www.my-site.com/img/logo2.gif' );

    Update:

    Tried this:
    PHP Code:
    // Define the correct path to "logo2.gif" ...
    header('Content-Type: image/gif');
    @
    readfile'http://www.my-site.com/img/logo2.gif' ); 
    But image didn't show, only alt ext.
    Last edited by jeddi; 08-24-2009 at 07:04 PM.

  • #6
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,471
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    I tried it myself with a full URL to another website and it works fine.

    I wonder if maybe you aren't running PHP5?
    Or maybe something in the upper part of your script is causing a problem?

    Try running this part all by itself (without the MySQL stuff):
    // Define the correct path to "logo2.gif" ...
    header('Content-Type: image/gif');
    @readfile( 'http://www.my-site.com/img/logo2.gif' );

  • #7
    Senior Coder
    Join Date
    May 2006
    Posts
    1,673
    Thanks
    28
    Thanked 4 Times in 4 Posts
    Thanks for the idea.

    yer, it worked by itself,

    so I will go through the other stuff and see if I can find the error.

  • #8
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,471
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    You may have PHP error reporting turned off, so you are not seeing the error.
    Comment-out portions of your script until it fails, or works, depending on which
    parts you comment-out.

  • #9
    Senior Coder
    Join Date
    May 2006
    Posts
    1,673
    Thanks
    28
    Thanked 4 Times in 4 Posts
    Found the error.

    It all works

    I can now track views and clicks

    So I guess that this is how I could track page views
    if I wanted ? Just direct any image to a script that
    updates the tracking database ?

  • #10
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,471
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    Yes,
    That's how most site counters and ad-click stuff works.

    With page "views", you don't have to do any image stuff, as it
    can run a PHP logging script each time the page is loaded. The
    image part comes into play where you are determining which
    image they click on, not necessarily the page viewing or refreshing.

    So you could actually track both things separately.
    And do some IP checking to see if the user remains the same.


  •  

    Posting Permissions

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