...

View Full Version : Is there a way to log image usage with php ?



jeddi
08-23-2009, 12:49 PM
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:


<!-- //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

mlseim
08-23-2009, 03:36 PM
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.

jeddi
08-24-2009, 05:53 PM
Thanks for your reply,

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

like this:


<!-- //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
/*
* 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 == 0 ) { // 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 ?


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 :confused:

mlseim
08-24-2009, 06:25 PM
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
/*
* 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 == 0 ) { // 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' );

?>

jeddi
08-24-2009, 06:59 PM
Hi,

Ok , thanks for the advice.

I tried putting this in the code but the image does not
show.


// 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:


// 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.

mlseim
08-24-2009, 07:58 PM
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' );

jeddi
08-24-2009, 09:18 PM
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.

mlseim
08-25-2009, 01:35 AM
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.

jeddi
08-25-2009, 08:09 AM
Found the error.

It all works :)

I can now track views and clicks :thumbsup:

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 ?

mlseim
08-25-2009, 01:59 PM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum