Contents of file 'styleswitch.php':
1 <?php
2 //---------------------------------------------------------------------------
3 // File: index.php [PHP code]
4 // Created: 17/10/2006
5 // Last change: 1/02/2009
6 // Author: Fredrik Jonsson <www.jonsson.eu>
7 //
8 // This PHP script is used for dynamically switching between CSS stylesheets
9 // determining the layout and typography of web pages. In this case, four
10 // different avaible layouts are assumed. The style to use is controlled by
11 // cookies set by the script, and the choice of which cookie to set is in
12 // turn controlled by parameters supplied to the script. The script is to be
13 // called using the following syntax, here supposing that the script has been
14 // placed in a catalogue /switch/ in the root directory on the server:
15 //
16 // www.myserver.com/switch/[?layout=jonssona|jonssonb|jonssonc|jonssond]
17 //
18 // In plain words, this syntax provides the following actions:
19 //
20 // 1. If no parameters are passed at all, then the script will check if
21 // a layout cookie already has been set. If a cookie has been set,
22 // then the next available layout will be set instead (i.e. next cookie
23 // in the list contained by the array 'cssdesign' in the code below),
24 // in a cyclic fashion (i.e. after the last available layout, the first
25 // one will be set).
26 //
27 // 2. If no parameter has been supplied AND no previously set cookie has
28 // been found, then the first cookie in the line will be set. This
29 // does not necessarily have to be the default one, as this is set
30 // independently of the order of the available layouts.
31 //
32 // 3. If a layout parameter (i.e. 'jonssona', 'jonssonb', 'jonssonc', or
33 // 'jonssond') has been supplied to the script, then the corresponding
34 // cookie will be set. In the case of an illegal supplied parameter,
35 // the default cookie will instead be set.
36 //
37 // 4. After the cookie has been set (or, rather attempt to set a cookie),
38 // the client browser will automatically be redirected to the previous
39 // page, hence providing a 'transparent' way of changing the layout.
40 //
41 // 5. If the script is run on an Microsoft IIS server, then the client
42 // browser will be redirected to an ASP script (index.asp) instead,
43 // prior to any actions or attempts by the PHP script to set a cookie.
44 // The reason is that PHP run on IIS causes problems in redirects via
45 // headers, a common problem which is well documented on the internet
46 // but with cumbersome solutions. The ASP script is similar to the PHP
47 // script, but runs in a somewhat more native fashion on Microsoft
48 // servers. In any case, the client browser will not notice this
49 // redirect, as the referring page is supplied to the ASP script as
50 // well, providing the way back to the page where the style switching
51 // originated.
52 //
53 // Notice that if cookies are not possible to set, for example if the client
54 // browser has diabled cookies, then the corresponding code in the page header
55 // will apply the default design. Notice that this case requires action from
56 // a script external of the one as implemented in this file.
57 //
58 // Copyright (C) 2006-2007 under Gnu General Public License, Fredrik Jonsson
59 //---------------------------------------------------------------------------
60
61 //
62 // Define the values of the cookies corresponding to the four different CSS
63 // designs. Here the values of the cookies are very simple, with 'jonssona'
64 // corresponding to the design contained in file 'jonssona.css' etc.
65 //
66 $cssdesign[1]="jonssona";
67 $cssdesign[2]="jonssonb";
68 $cssdesign[3]="jonssonc";
69 $cssdesign[4]="jonssond";
70 $cssdesign[5]="jonssone";
71 $cssdesign[6]="unstyled";
72 $defaultDesign=$cssdesign[4]; // Points out the default CSS design to use
73
74 //
75 // Get the referring page (if any) and any explicitly supplied layout. If
76 // no referring page exist, then use the root of the web server directory
77 // for the final redirection.
78 //
79 $ref=(isset($_SERVER['HTTP_REFERER']))?$_SERVER['HTTP_REFERER']
80 :"http://{$_SERVER['SERVER_NAME']}/";
81 $layout=$_GET["layout"];
82
83 //
84 // If we are running IIS at the server side, then immediately redirect and
85 // instead use the similar ASP script for switching of CSS stylesheets. In
86 // this case, any referring page will be supplied to the ASP script as a
87 // parameter 'ref'.
88 //
89 $platform=$_SERVER['SERVER_SOFTWARE'];
90 if (strpos($platform,"Microsoft")!==false) {
91 if (strpos($platform,"IIS")!==false) {
92 //
93 // Comment: "Referer" is a common misspelling of the word "referrer."
94 // It is so common, in fact, that it made it into the official
95 // specification of HTTP - the communication protocol of the World
96 // Wide Web - and has therefore become the standard industry spelling
97 // when discussing HTTP referers.
98 //
99 if (($layout)=="") { // If no CSS layout was specified as input parameter
100 header("Location: index.asp?ref=$ref"); // Redirect to ASP script
101 } else { // If a CSS layout actually WAS specified as input parameter
102 header("Location: index.asp?ref=$ref&layout=$layout");
103 }
104 exit();
105 }
106 }
107
108 //
109 // If we get to this point, the server platform is something else than
110 // Microsoft IIS (e.g. Apache), and the steps listed in the description
111 // of the script will be executed.
112 //
113 if (($layout)=="") { // If no layout was explicitly supplied ...
114 if (isset($_COOKIE['layout'])) { // .. then cycle the available layouts.
115 if ($_COOKIE['layout']==$cssdesign[1]) {
116 $layout=$cssdesign[2];
117 } elseif ($_COOKIE['layout']==$cssdesign[2]) {
118 $layout=$cssdesign[3];
119 } elseif ($_COOKIE['layout']==$cssdesign[3]) {
120 $layout=$cssdesign[4];
121 } elseif ($_COOKIE['layout']==$cssdesign[4]) {
122 $layout=$cssdesign[5];
123 } elseif ($_COOKIE['layout']==$cssdesign[5]) {
124 $layout=$cssdesign[1];
125 } else { // If corrupt layout specification, resort to default
126 $layout=$defaultDesign;
127 }
128 } else { // If no layout supplied and no cookie set, choose design in line...
129 $layout=$cssdesign[1];
130 }
131 } else { // If a layout was suppplied to the script
132 if (trim($layout)=="jonssona") {
133 $layout=$cssdesign[1];
134 } elseif (trim($layout)=="jonssonb") {
135 $layout=$cssdesign[2];
136 } elseif (trim($layout)=="jonssonc") {
137 $layout=$cssdesign[3];
138 } elseif (trim($layout)=="jonssond") {
139 $layout=$cssdesign[4];
140 } elseif (trim($layout)=="jonssone") {
141 $layout=$cssdesign[5];
142 } elseif (trim($layout)=="unstyled") {
143 $layout=$cssdesign[6];
144 } else {
145 $layout=$defaultDesign;
146 }
147 }
148
149 setcookie("layout",$layout,time()+24*3600,"/"); // 24 hour lifetime of cookie
150 header("Location: $ref"); // Redirect back to referring page (or root).
151 ?>
152
Generated by ::viewsrc::