scriptygoddess

19 Oct, 2004

gifs that execute a php script

Posted by: Jennifer In: Lessons learned|Script snippet

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

October 19th, 2004 at 2:30 pm

Avatar

ooooh! great tip!

2 | Mark J

October 20th, 2004 at 3:06 am

Avatar

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

October 20th, 2004 at 5:35 am

Avatar

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

October 20th, 2004 at 8:17 am

Avatar

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

October 24th, 2004 at 11:34 pm

Avatar

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.

Featured Sponsors

Genesis Framework for WordPress

Advertise Here


  • Scott: Just moved changed the site URL as WP's installed in a subfolder. Cookie clearance worked for me. Thanks!
  • Stephen Lareau: Hi great blog thanks. Just thought I would add that it helps to put target = like this:1-800-555-1212 and
  • Cord Blomquist: Jennifer, you may want to check out tp2wp.com, a new service my company just launched that converts TypePad and Movable Type export files into WordPre

About


Advertisements