Skip on down to the menu.
Style Switcher Perl Script
Here's a style switcher script that does two things.
- The first thing it can do is insert external style sheet link elements, or any other html for that matter, based on the value of a cookie.
- The second thing it can do is set the value of this cookie based on input you give it on the url you use to call the script.
You can see where this is going... Custom html based on a user cookie, and the ability for the user to alter this cookie by clicking links!
The page you're reading now is using the very code shown below.
You may be tempted to alter this program to include an arbitrary style sheet specified by the user, rather than one of a few that are defined in the program. Don't do it. You want to be completely in control of which style sheets the user can use, as well as the content of those style sheets. Remember, Internet Explorer can execute ActiveX code that is buried in css!
How to include
Using Apache SSI
Use the following line in your .shtml files wherever you want the menu to appear.
Since I use only Apache I don't have any idea how this would work on any other web server. Sorry...
Invoking the script to include a style sheet selection
<!--#exec cgi="/cgi-bin/styleswitcher.pl"-->
You may need to adjust the path as required by your web server setup.
Invoking the script to switch the style sheet selection
<!--#exec cgi="/cgi-bin/styleswitcher.pl?style=Cookie+Value"-->
In the example above the value of the cookie that I'm setting is "Cookie Value". This is case sensitive. The "+" is required to indicate that there is a space. Remember, officially there can never be a space in a url, so this is the cgi way to indicate a space.
After the cookie is set to the new value, the current page will reload. This will refresh the page with the new style sheet links.
The Code
In the example below, the cookie name I use is "style". The two valid values for this cookie are "Lefthand Menu" and "Righthand Menu".
You'll have to modify this to meet your own requirements. As I make refinements to the script, I'll try to put all the custom config stuff at the top of the file.
#!/usr/bin/perl
# styleswitcher.pl
# revision A, 2004-04-13, TAS
#
#
# Insert style style sheets into a HTML document based on a browser cookie.
# Change the browser cookie based on an input parameter to this script.
# Project started by Tom Sneddon on April 12, 2004
#
# Revision History:
# Rev A: April 14, 2004
# Tom Sneddon
# Initial Release
#
use strict; # Require good programming practice
use CGI; # Use the CGI module
use CGI::Carp qw(fatalsToBrowser); # View fatal errors in the output text stream
# Initialize variables
my $setstyle_param = 'style';
my $output = ''; # This is our html output
my $cookie_name = 'style'; # This is the cookie that determines the
# style sheets that are fed to the browser
# Next is the list of valid values for this
# cookie, along with the CSS to be
# included based on each of these
# cookie values.
my $lefthand = <<'LEFTHAND';
<link rel="stylesheet"
type="text/css"
media="screen, print"
href="/style/hide-default.css"
>
<link rel="stylesheet"
type="text/css"
media="print"
href="/style/print-only.css"
>
<link rel="stylesheet"
type="text/css"
media="screen, print"
href="/style/hide-left.css"
title="Lefthand Menu"
>
LEFTHAND
# Absolutely no spaces (or tabs, or comments, or anything else)
# allowed before or after LEFTHAND in the previous line
my $righthand = <<'RIGHTHAND';
<link rel="stylesheet"
type="text/css"
media="screen, print"
href="/style/hide-default.css"
>
<link rel="stylesheet"
type="text/css"
media="print"
href="/style/print-only.css"
>
<link rel="stylesheet"
type="text/css"
media="screen, print"
href="/style/hide-right.css"
title="Righthand Menu"
>
RIGHTHAND
# Absolutely no spaces (or tabs, or comments, or anything else)
# allowed before or after RIGHTHAND in the previous line
my %styles = ('Lefthand Menu' => $lefthand,
'Righthand Menu' => $righthand
);
my $default_style = $styles{'Lefthand Menu'}; # Stylesheets to use by default
my $stylecookie; # This is the style sheet cookie info
my $query; # This is what we'll call the query
$query = new CGI; # Create a new query object
# Is there a parameter on the command line to set the cookie?
if ($query->param($setstyle_param)) {
# Get the command line parameter
my $parameter = $query->param($setstyle_param);
# **** Should really match on an array of valid cookie values ****
if ($parameter =~ m/^Lefthand Menu$|^Righthand Menu$/) {
# What url called this script?
my $ref = $query->referer();
# Load $newcookie with the proper values to be used in the redirect command
my $newcookie = $query->cookie(-name => $cookie_name,
-value => $parameter,
-expires => '+1y',
-path => '/',
-domain => '.excusemeplease.org',
-secure => 0
);
# Reload (via a redirect) the current document with the new style settings
print $query->redirect(-cookie => $newcookie,
-type => 'text/html',
-charset => 'ISO-8859-1',
-expires => 'now',
-location => $ref,
-method => 'get'
);
# Nothing more to do at this point...
exit;
}
}
else {
# Print the standard http header
print $query->header(-type=>'text/html',
-charset=>'ISO-8859-1');
# Get the current value of the style cooki
$stylecookie = $query->cookie($cookie_name);
# Print the style html for each match to the cookie (I could say this better...)
foreach my $style (keys %styles) {
if ($stylecookie eq $style) {
$output .= $styles{$style};
}
}
# Just in case a cookie is not available...
if ($output eq '') {
# Use the default style
$output = $default_style;
}
# Print our output to the web page
print $output;
}