Is this possible with PHP, a random signature?

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

User avatar
Posts: 142
Joined: Wed Sep 12, 2007 3:32 pm UTC
Location: springfield, il

Is this possible with PHP, a random signature?

Postby gnomes » Mon Sep 17, 2007 9:46 pm UTC

I just thought about it, would it be possible to have a php file grab a random image from a directory each time "random.jpg" was requested?

I know I could do something like that on the server, but I want to use it for message boards. I'm not trying to execute codes on the boards, merely instead of the server spitting out a 404 for random.jpg the php file handles it and supplies sig1.jpg or sign.jpg.

Also, I wouldn't mind use feed back on if people would be willing to put up with this (I've seen things where once a person does something, everyone picks up on it)
Powered by Necroite

Posts: 1460
Joined: Wed Mar 21, 2007 4:07 am UTC

Postby Rysto » Mon Sep 17, 2007 10:12 pm UTC

Yes, but it wouldn't work the way you wanted it to because of browser caches.

Posts: 4331
Joined: Mon Aug 07, 2006 6:28 am UTC
Location: Madison, WI

Postby EvanED » Mon Sep 17, 2007 11:10 pm UTC

Rysto wrote:Yes, but it wouldn't work the way you wanted it to because of browser caches.

I haven't done anything in PHP for years, but I do know that you can send HTTP headers, which include things like cache-control to suppress caching on anyone who pays attention to the RFC.

My understanding is that, and IIRC, while you may need to use a couple headers to do the trick, you can accomplish that task most of the time.

User avatar
Posts: 560
Joined: Wed Sep 05, 2007 10:05 pm UTC
Location: San Francisco, Ca

Postby biolution » Tue Sep 18, 2007 3:09 am UTC

This is quite easy, really:

1) setup apache (or whatever webserver) to execute php when random.jpg is requested
2) randomly pick a file from your pile (however you do it is up to you)
3) (as others have mentioned) set http headers so clients don't cache the image. You can also append query params (?blalba) and fragments(#blabla) to help reduce this.

Additionally, you can simply keep the file name as "random.php" (<img src> doesn't care) so browsers don't try to guess the file type based on the name. Most likely you'll have to play around with it in IE to make sure it isn't caching it, since ie has a tendency to not give a shit what headers say.

This is probably gonna be useful. ... tp+caching

Love, Sex & Relationships: I'D RATHER BE HAVING SEX WITH BEES.

Posts: 92
Joined: Fri Jun 22, 2007 12:14 am UTC
Location: Melbourne, AU

Postby Karrion » Tue Sep 18, 2007 3:29 am UTC

You'll also need to make sure your php code sets the Content-Type header to the correct MIME type for the image file.

Basically you want to:
  1. Determine the available files and select one at random
  2. Set the Content-Type and Content-Length headers based on the selected file
  3. Set cache control headers
  4. Copy the image file from disk to output

User avatar
Restorer of Worlds
Posts: 7572
Joined: Sat Sep 23, 2006 3:56 am UTC
Location: Australia

Postby phlip » Tue Sep 18, 2007 3:59 am UTC

Another trick: if you set up Apache's content negotiation right (possibly also for other servers), you can call the script random.png.php and then access it as just random.png in the URL... or set up aliases to do a similar thing. That way it'll work for things like phpBB which expect an image-type filename extension.

If you don't have the ability to mess with the server settings, another way to do it is to make it random.png/index.php - then just access random.png... when you open it in the browser, the server will redirect you to random.png/ but that doesn't matter for when you put the URL into the "signature" box.

Code: Select all

enum ಠ_ಠ {°□°╰=1, °Д°╰, ಠ益ಠ╰};
void ┻━┻︵​╰(ಠ_ಠ ⚠) {exit((int)⚠);}

Posts: 20
Joined: Tue May 01, 2007 11:52 pm UTC

Postby MrSomeone » Tue Sep 18, 2007 10:09 pm UTC

What I did when I made a dynamic signature was had a directory dedicated to the signature, fir example /s/, and set the web server to process png files within that directory as PHP script (Apache: AddType application/x-httpd-php .png) then sig.png would actually be a PHP script outputting raw png data.

mod_rewrite can also be used, something like

Code: Select all

RewriteRule ^sig.png$ sig.php

User avatar
Posts: 167
Joined: Sat May 05, 2007 8:38 pm UTC

Postby Jach » Wed Sep 19, 2007 1:48 am UTC

I did this several months ago. I just set up an Apache .htaccess to forward all 404 errors to the PHP file that grabs a random image and displays it.
I love reading quotes.

Posts: 39
Joined: Wed Apr 25, 2007 1:35 am UTC
Location: Illinois

Postby Elevator_Hazard » Thu Sep 20, 2007 2:47 am UTC

I didn't really read up on this thread so much, but I made a random signature system already (currently doesn't work because the firewall on my host is trying to put ads in the image which produces errors) but I do have an image rotator. They both you .jpg files and you can see the imgRotator here. The .htaccess tells apache that .jpg files should be treated as .php files. The .htaccess code is here if you want to see it:

Code: Select all

<Files "img.jpg">

AddType application/x-httpd-php .jpg


User avatar
Posts: 799
Joined: Mon Dec 11, 2006 9:46 pm UTC

Postby Durinthal » Thu Sep 20, 2007 3:21 am UTC

If people can do something like this:

I'm sure just grabbing a random image isn't that hard.

User avatar
Posts: 2
Joined: Fri Sep 28, 2007 5:23 pm UTC
Location: Grants Pass, Oregon

Re: Is this possible with PHP, a random signature?

Postby orygunbofh » Tue Oct 02, 2007 7:45 am UTC

I know your thread's getting a little old, but I've had some PHP + .htaccess goodness doing exactly this for a while now. Decided that it was time to dust it off, fix a bug or two, and publish it.

You can snag a tarball here, if you so desire. :)

Return to “Coding”

Who is online

Users browsing this forum: No registered users and 7 guests