gifs that execute a php script

Problem: (somewhat in line with my last post) I needed to run a script in the “background”, but I only had the ability to present the script as a .gif.

Solution: actually, I came up with two ways of doing this and both use .htaccess to pull it off.

1) I’m a gif but really I’m a php script
This one is fairly straightfoward. I put my script in a seperate directory, (that doesn’t actually include any images!) and renamed it from filename.php to filename.gif (or filename.jpg) (Yes, I know it’s not really an image file. Hang in there)

Then, in the htaccess file for JUST THAT FOLDER! (similar to this trick) I added this:
AddType application/x-httpd-php .php .jpg .gif

FYI - in your php file (that now looks like a .gif or .jpg) make sure you don’t have any headers/text echoed, and at the end of the file add this to your php code:
header('Content-Type: image/gif');
@readfile( '/SERVERPATH/TO/A/REAL/IMAGE/spacer.gif' );

You can then include the “script” as if it were an image file, it will run the script, but only display a gif to the user.

You can even pass it variables like filename.gif?somevariable=somevalue&anotervariable=anothervalue, but there’s another way to do that too without the ugly URL.

2) I’m a gif that’s actually a php script super-powered with rewrite
Ok, starting from the beginning. You have your script (filename.php) in a folder, add the following to the .htaccess file in that folder:
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)/(.*)/spacer.gif /YOURFOLDER/FILENAME.php?somevar=$1&anothervar=$2 [QSA]

Before we continue, I must tell you that I dont’ completely understand rewrite rules. There’s a LOT of holes in my knowledge. So if you have a better way, PLEASE post it in the comments. I would LOVE to be able to understand what the hell I’m doing. LOL.

OK, moving along. Now you can call your script by actually calling that “phantom” spacer.gif, with the variables in the URL as if they’re directories like this:

http://www.yourdomain.com/YOURFOLDER/somevalue/anothervalue/spacer.gif

Again, make sure you have this at the end of you php code in your filename.php:
header('Content-Type: image/gif');
@readfile( '/SERVERPATH/TO/A/REAL/IMAGE/spacer.gif' );

5 Responses to “gifs that execute a php script”

  1. Jenna Says:

    ooooh! great tip!

  2. Mark J Says:

    If you do not need the gif to have a “.gif” extension, you can skip the mod-rewrite stuff and just point to the php script, which will behave like a gif to anyone who views it.

    You can do this with all types of files. Try giving your CSS files a “.php” extension and

    header(’Content-Type: text/css’);

    at the top and get creative. :-)

  3. Jeremy Says:

    if you do need a .gif extension for some purpose, like an avatar for a bbs that checks filenames, it is possible to add /image.gif to the end of the script.

    script.php/image.gif

    no rewrites needed then.

  4. Jennifer Says:

    My problem was that any filename with an extension other .gif or .jpg was getting stripped out. AS WELL, I needed to be able to pass the script some variables.

    So, I was fine with the first solution. But the minute I started pulling the something.gif?var=value etc. etc. .. it got yanked. So I had to find another (albeit) sneaky way to pass variables into the url.

    Not sure about the script.php/spacer.gif thing. I suspect the .php would have tipped it off. As well - not sure how I would pass variables using that method without being obvious. (ie. without doing file.ext?var=value)

  5. Andrew Says:

    Instead of AddType I often use ForceType wrapped inside a Files block in my .htaccess. This way I can restrict it to a particular file or set of files. I’ve done this for both images and CSS files.