Changing Images
Archive - Originally posted on "The Horse's Mouth" - 2009-12-17 06:55:09 - Graham Ellis
On yesterday's PHP course, I wrote an example that selects the picture that is displayed based on the number of minutes that have elapsed into the hour - you can run that page [here] and you can see the source code [here]. It's an easy step to change the "number of minutes" into a "number of hours" into the day, and produce a page that shows a different image depending on the time of day - a nighttime shot until 7 a.m., then a breakfast, followed by ... well, you'll be getting the idea.
A further request that your customers may make is for changed character sizes, backgrounds, etc - and if you provide a "service" you've a legal requirement in the UK to take all reasonable steps to ensure your service is accessible to the disabled, which means (on a web site) that you should provide this flexibility, not require your users to link via images which may not be accessible on browsers that read words for the blind, etc.
The same example code includes a simple example of using a form (or pseudo form) element to provide an input to the page, which sets a cookie and allows for a colour change to be requested and saved. In the case of the example, I have set a six hour timeout on the cookie so that it'll be reset for me for demonstration purposes by the following day ... you might choose to use a year or two.
Although both of the examples here are very short, simple, straightforward, I have enhanced them slightly since yesterday's demonstration by adding in a couple of checks to ensure that a malicious user can't inject a request for a colour that we didn't offer, or something rather more nasty. Bear in mind that even with these simples of demonstrations, you need to have one eye open for the security of your web site if you're running code on it - especially code that has user inputs!