Forum Moderators: coopster

Message Too Old, No Replies

PHP Image with Cookie support

         

JAB Creations

8:53 am on Jun 12, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The concept is simple, in certain conditions (which I will be able to easily apply separate of this post so don't push me on them)...I'd like to create a PHP image that when set will create a cookie.

The important part is that I need to ensure PHP does actually create the image and the cookie only when the image creation is successful.

A real world example of PHP images can be seen as the avatars of vBulletin PHP based forum software.

I have absolutely no idea of how to approach this but it is a very high level goal of mine.

- John

tomda

9:26 am on Jun 12, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Jab_creations,

Please elaborate, do you have problems to:
1/ Upload pictures
2/ Make sure pic is uploaded and setting cookies
3/ Or both ot them.

And do you have any code already?

adb64

10:04 am on Jun 12, 2006 (gmt 0)

10+ Year Member



Hello John,

I use something similar on my site to track visitors when they visit static HTML pages, not PHP pages. On these HTML pages there is a PHP generated image and at the same time the script sets a cookie for visitor tracking.
So in short the following things are done:

- Create image internally
- Send cookie
- Output image data

Don't hesitate to ask me if you need more detailed information.

Regards,
Arjan

JAB Creations

11:57 am on Jun 12, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi tomda,

I found this...
[us3.php.net...]

I'll be trying that out a little later (I have to go out to a client of mine this morning firs though).

I have no materials, just trying to get some direction with this. My server and my local copy of Apache support GD.

I'm not working on any uploader nor will use any in conjunction with this.

The image will already be on the server, it's a sort of conditions test for a specific page. If the condition passes the cookie is set...that is if the image displays correctly.

I'll have more time to post stuff I'll be messing around with after I get back from my client.

Thanks to Arjan as well ... for both your responses.

- John

tomda

1:09 pm on Jun 12, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm not working on any uploader nor will use any in conjunction with this.

The image will already be on the server, it's a sort of conditions test for a specific page. If the condition passes the cookie is set...that is if the image displays correctly.

Then, you just need to check if the file exist in PHP


<?php
$filename = '/path/to/foo.img';

if (file_exists($filename)) {
echo "The file $filename exists";
// SET COOKIE
} else {
echo "The file $filename does not exist";
// DO NOT SET COOKIE
}
?>

or another way (not sure) would be to use the getimagesize function


<?php
$filename = '/path/to/foo.img';
if (getimagesize($filename)) {
@$file_width = $sizeb[0];
@$file_height = $sizeb[1];
echo "The file $filename exists";
// SET COOKIE
} else {
echo "The file $filename does not exist";
// DO NOT SET COOKIE
}
?>

JAB Creations

2:27 pm on Jun 12, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Tomda,

I'm not trying to detect the image on the server but rather if the user requested and successfully downloaded the image.

If the user requests the image and it successfully downloads then I wish to set a cookie.

Thats it, I don't need the cookie code, else code, etc. I just need the PHP script to reliably know if the user is capable of viewing images via PHP. Thanks for your continued help!

- John

tomda

5:56 am on Jun 13, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi,

I AM MAY BE WRONG, but since PHP is a server-side script, checking if the image has been correctly downloaded using PHP is difficult if not impossible...

Furthermore, as you may know, cookie should be sent before any content or HTML, so sending a cookie after the image is dowloaded is also difficult....

UNLESS you work with HTTP request (I am not good at header request, therefore I can't help you) but I guess there is a way to request a download, check if dowloading has been succesfull and sent further header request and cookie.

If you have a real example at vBulletin PHP, why don't you download the bulletin board and do some search in the script itself to locate how they are doing?

Arjan said that he is currently using something similar, may be you could get in touch with him. And lastly, if you find the solution, report back.

Tomda

adb64

6:53 am on Jun 13, 2006 (gmt 0)

10+ Year Member



Tomda,

John already stickied me and I mailed him the following example:

In each HTML file have an IMG tag somewhere in the body like:

<img src="testimage.php" alt="">

And a PHP file called testimage.php with the following content:

<?
set_cookie(cookie stuff);
$ImageOnServer = "ImageToServe.png";
$Fsize = filesize($ImageOnServer);
$fp = fopen($ImageOnServer,"r");
header('Content-type: image/png');
header('Content-Length: ' . $Fsize);
fpassthru($fp);
fclose($fp);
?>

When someone/something reads the HTML file and does not request the image (a PNG in my example), the cookie won't be set. If it does request the image, so a genuine user with a genuine browser, the cookie will be set. This cookie wiil then be sent when the next file is requested by the user.

tomda

7:42 am on Jun 13, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks a lot Arjan,

Very interesting script indeed!
It is the first time I encounter this method...
Keep learning new things everyday in WW, that's great.

JAB Creations

11:05 pm on Jun 13, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks for the script, I finally had a moment to test it out.

*Drumroll*

Technical!

I used a jpg image and modified the mime as so (image/jpeg). It set the cookie and displayed the image.

Next I had to ensure that the image would not load if I disabled images in the browser. Image still downloaded (but the images on my site including via the CSS did not download).

I need a different means of testing this though I'm not sure how.