Send a different image from my web host if it was already requested by an IP address?
I am hosting an image with my hosting provider host gator and people can request it with www.example.com/image.png
When an IP address requests the image www.example.com/image.png the first time it sends them one image.
If that IP address ever requests that same image again, I want to make it send a different image.
How can I accomplish something like this?
You request image.php (not image.png) and ensure the returned result includes the header Content-Type:image/png so it is processed as an image.
You can set the header in php with
header("Content-Type: image/png");
The IP address can be picked up using the apache/environment variable REMOTE_ADDR - in PHP you can get this with $_SERVER["REMOTE_ADDR"]
You can read the content of the appropriate image using file_get_contents - so you might have code like the following pseudocode php code (the bits in the asterisks need to be replaced and depend on your database and how you interact with it:
<?php
# We want the browser to render this as a PNG image
header("Content-Type: image/png");
# We don't want to cache the output.
header("cache-control: max-age=0, no-cache");
header("expires: Sat, 01 Jan 2000 01:01:01 GMT");
if *$_SERVER["REMOTE_ADDR"] not in database*
echo file_get_contents("/path/to/initial.png");
else
echo file_get_contents("/path/to/repeatfile.png");
*insert $_SERVER["REMOTE_ADDR"] into database*
?>
You would call this with something html code like
<img src="imagemod.php" alt="changing image">
Comments
Post a Comment