Header Image Slideshow

Header Image Slideshow

I hacked in some code that allows you to click the main header image to see the next one in the sequence. This is more convenient, I think. I set a “cookie” that holds the current image number, and your browser will remember it for 30 days. Sordid technical details follow!

So if you want to do something similar, here’s what I did:

I modified my index.php file to check for the presence of an existing cookie and a variable to set the image:

<?php
/* cookie setting routines */
 global $dscom_header;
 // read the cookie to get the img
 $dscom_header = $HTTP_COOKIE_VARS['headerimg'];
 if ($dscom_header == NULL) $dscom_header = 1;

 // read the next header value, if it's set
 $nextheader = $_GET["headerimg"];
 if ($nextheader != null) {
  setcookie("headerimg",$nextheader,time()+60*60*24*30,"/");
  $dscom_header = $nextheader;
 }
?>

This code needs to appear before any HTML, including the tag. That’s because you have to set cookies before any output is written from the web server (where your PHP code is running) to the target browser (which expects cookies before it sees any HTML markup).

There’s code later on that checks for the value of $dscom_header and modifies it appropriately. The URL for the header image is set to the next image value. The way you pass variables via HTTP GET is by tacking them on the end of the URL. So there it is…my quick and dirty front-page gallery. It would be cooler to actually use Javascript to do the image replacement through DHTML techniques…but suddenly I feel tired :-)

If it doesn’t seem to work, your .htaccess file might need some attention. Mine was a bit screwed up until I modified the first rewrite rule to correctly skip all all rules when a “regular” url was entered.

0 Comments