none
[openafs-wiki.git] / TWiki / CGISessionTutorialDotPm.mdwn
1 # <a name="Package &lt;code&gt;="></a> Package =
2
3 <div>
4   <ul>
5     <li><a href="#Package =="> Package ==</a></li>
6   </ul>
7 </div>
8
9 =head1 NAME
10
11 CGI::Session::Tutorial - Extended CGI::Session manual
12
13 =head1 STATE MAINTENANCE OVERVIEW
14
15 Since HTTP is a stateless protocol, each subsequent click to a web site is treated as new request by the Web server. The server does not relate a visit with a previous one, thus all the state information from the previous requests are lost. This makes creating such applications as shopping carts, web sites requiring users to authenticate, impossible. So people had to do something about this despair situation HTTP was putting us in.
16
17 For our rescue come such technologies as I and Is that help us save the users' session for a certain period. Since I and Is alone cannot take us too far (B), several other libraries have been developed to extend their capabilities and promise a more reliable solution. L&lt;CGI::Session|CGI::Session&gt; is one of them.
18
19 Before we discuss this library, let's look at some alternative solutions.
20
21 =head2 COOKIE
22
23 Cookie is a piece of text-information that a web server is entitled to place in the user's hard disk, assuming a user agent (such as Internet Explorer, Mozilla, etc) is compatible with the specification. After the cookie is placed, user agents are required to send these cookies back to the server as part of the HTTP request. This way the server application ( CGI, for example ) will have a way of relating previous requests by the same user agent, thus overcoming statelessness of HTTP.
24
25 Although I seem to be promising solution for the statelessness of HTTP, they do carry certain limitations, such as limited number of cookies per domain and per user agent and limited size on each cookie. User Agents are required to store at least 300 cookies at a time, 20 cookies per domain and allow 4096 bytes of storage for each cookie. They also rise several Privacy and Security concerns, the lists of which can be found on the sections B&lt;6-"Privacy"&gt; and B&lt;7-"Security Considerations"&gt; of B.
26
27 =head2 QUERY STRING
28
29 Query string is a string appended to URL following a question mark (?) such as:
30
31 <http://my.dot.com/login.cgi?user=sherzodr;password=top-secret>
32
33 As you probably guessed, it can also help you pass state information from a click to another, but how secure is it do you think, considering these URLs tend to get cached by most of the user agents and also logged in the servers access log, to which everyone can have access.
34
35 =head2 HIDDEN FIELDS
36
37 Hidden field is another alternative to using query strings and they come in two flavors: hidden fields used in POST methods and the ones in GET. The ones used in GET methods will turn into a true query strings once submitted, so all the disadvantages of QUERY\_STRINGs apply. Although POST requests do not have limitations of its sister-GET, the pages that hold them get cached by Web browser, and are available within the source code of the page (obviously). They also become unwieldily to manage when one has oodles of state information to keep track of ( for instance, a shopping cart or an advanced search engine).
38
39 Query strings and hidden fields are also lost easily by closing the browser, or by clicking the browser's "Back" button.
40
41 =head2 SERVER SIDE SESSION MANAGEMENT
42
43 This technique is built upon the aforementioned technologies plus a server-side storage device, which saves the state data on the server side. Each session has a unique id associated with the data in the server. This id is also associated with the user agent either in the form of a I, a I, hidden field or any combination of the above. This is necessary to make the connection with the client and his data.
44
45 Advantages:
46
47 =over 4
48
49 =item \*
50
51 We no longer need to depend on User Agent constraints in cookie size.
52
53 =item \*
54
55 Sensitive data no longer need to be traveling across the network at each request (which is the case with query strings, cookies and hidden fields). The only thing that travels is the unique id generated for the session (B&lt;5767393932698093d0b75ef614376314&gt;, for instance), which should make no sense to third parties.
56
57 =item \*
58
59 User will not have sensitive data stored in his/her computer in unsecured file (which is a cookie file).
60
61 =item \*
62
63 It's possible to handle very big and even complex data structures transparently (which I do not handle).
64
65 =back
66
67 That's what CGI::Session is all about - implementing server side session management. Now is a good time to get feet wet.
68
69 =head1 PROGRAMMING STYLE
70
71 Server side session management system might be seeming awfully convoluted if you have never dealt with it. Fortunately, with L&lt;CGI::Session|CGI::Session&gt; all the complexity is handled by the library transparently. This section of the manual can be treated as an introductory tutorial to both logic behind session management, and to CGI::Session programming style.
72
73 All applications making use of server side session management rely on the following pattern of operation regardless of the way the system is implemented:
74
75 =over 4
76
77 =item 1
78
79 Check if the user has session cookie dropped in his computer from previous request
80
81 =item 2
82
83 If the cookie does not exist, create a new session identifier, and drop it as cookie to the user's computer.
84
85 =item 3
86
87 If session cookie exists, read the session ID from the cookie and load any previously saved session data from the server side storage. If session had any expiration date set it's useful to re-drop the same cookie to the user's computer so its expiration time will be reset to be relative to user's last activity time.
88
89 =item 4
90
91 Store any necessary data in the session that you want to make available for the next HTTP request.
92
93 =back
94
95 CGI::Session will handle all of the above steps. All you have to do is to choose what to store in the session.
96
97 =head2 GETTING STARTED
98
99 To make L&lt;CGI::Session|CGI::Session&gt;'s functionality available in your program do either of the following somewhere on top of your program file:
100
101 use CGI::Session; # or require CGI::Session;
102
103 Whenever you're ready to create a new session in your application, do the following:
104
105 $session = new CGI::Session() or die CGI::Session-&gt;errstr;
106
107 Above line will first try to re-initialize an existing session by consulting cookies and necessary QUERY\_STRING parameters. If it fails will create a brand new session with a unique ID, which is normally called I, I for short, and can be accessed through L&lt;id()|CGI::Session/id()&gt; - object method.
108
109 We didn't check for any session cookies above, did we? No, we didn't, but CGI::Session did. It looked for a cookie called C, and if it found it tried to load existing session from server side storage (B in our case). If cookie didn't exist it looked for a QUERY\_STRING parameter called C. If all the attempts to recover session ID failed, it created a new session.
110
111 NOTE: For the above syntax to work as intended your application needs to have write access to your computer's I folder, which is usually F in UNIX. If it doesn't, or if you wish to store this application's session files in a different place, you may pass the third argument like so:
112
113 $session = new CGI::Session(undef, undef, \{Directory=&gt;'../tmp/sessions'\});
114
115 Now it will store all the newly created sessions in (and will attempt to initialize requested sessions from) that folder. Don't worry if the directory hierarchy you want to use doesn't already exist. It will be created for you. For details on how session data are stored refer to L&lt;CGI::Session::Driver::file|CGI::Session::Driver::file&gt;, which is the default driver used in our above example.
116
117 There is one small, but very important thing your application needs to perform after creating CGI::Session object as above. It needs to drop Session ID as an I into the user's computer. CGI::Session will use this cookie to identify the user at his/her next request and will be able to load his/her previously stored session data.
118
119 To make sure CGI::Session will be able to read your cookie at next request you need to consult its C&lt;name()&gt; method for cookie's suggested name:
120
121 $cookie = $query-&gt;cookie( -name =&gt; $session-&gt;name, -value =&gt; $session-&gt;id ); print $query-&gt;header( -cookie=&gt;$cookie );
122
123 C&lt;name()&gt; returns C by default. If you prefer a different cookie name, you can change it as easily too, but you have to do it before CGI::Session object is created:
124
125 CGI::Session-&gt;name("SID"); $session = new CGI::Session();
126
127 Baking the cookie wasn't too difficult, was it? But there is an even easier way to send a cookie using CGI::Session:
128
129 print $session-&gt;header();
130
131 The above will create the cookie using L&lt;CGI::Cookie|CGI::Cookie&gt; and will return proper http headers using L&lt;CGI.pm|CGI&gt;'s L&lt;CGI|CGI/header()&gt; method. Any arguments to L&lt;CGI::Session|CGI::Session/header()&gt; will be passed to L&lt;CGI::header()|CGI/header()&gt;.
132
133 Of course, this method of initialization will only work if client is accepting cookies. If not you would have to pass session ID in each URL of your application as QUERY\_STRING. For CGI::Session to detect it the name of the parameter should be the same as returned by L&lt;name()|CGI::Session/name()&gt;:
134
135 printf ("[click me](\"$ENV{SCRIPT_NAME}?%s=%s\")", $session-&gt;name, $session-&gt;id);
136
137 If you already have session id to be initialized you may pass it as the only argument, or the second argument of multi-argument syntax:
138
139 $session = new CGI::Session( $sid ); $session = new CGI::Session( "serializer:freezethaw", $sid ); $session = new CGI::Session( "driver:mysql", $sid, \{Handle=&gt;$dbh\} );
140
141 By default CGI::Session uses standard L&lt;CGI|CGI&gt; to parse queries and cookies. If you prefer to use a different, but compatible object you can pass that object in place of $sid:
142
143 $cgi = new CGI::Simple(); $session = new CGI::Session ( $cgi ); $session = new CGI::Session( "driver:db\_file;serializer:storable", $cgi); # etc
144
145 See L&lt;CGI::Simple|CGI::Simple&gt;
146
147 =head2 STORING DATA
148
149 L&lt;CGI::Session|CGI::Session&gt; offers L&lt;param() method|CGI::Session/param()&gt;, which behaves exactly as L&lt;CGI.pm's param()|CGI/param()&gt; with identical syntax. L&lt;param()|CGI::Session/param()&gt; is used for storing data in session as well as for accessing already stored data.
150
151 Imagine your customer submitted a login form on your Web site. You, as a good host, wanted to remember the guest's name, so you can a) greet him accordingly when he visits your site again, or b) to be helpful by filling out I part of his login form, so the customer can jump right to the I field without having to type his username again.
152
153 my $name = $cgi-&gt;param('username'); $session-&gt;param('username', $name);
154
155 Notice, we're grabbing I value of the field using CGI.pm's (or another compatible library's) C&lt;param()&gt; method, and storing it in session using L&lt;CGI::Session|CGI::Session&gt;'s L&lt;param()|CGI::Session/param()&gt; method.
156
157 If you have too many stuff to transfer into session, you may find yourself typing the above code over and over again. I've done it, and believe me, it gets very boring too soon, and is also error-prone. So we introduced the following handy method:
158
159 $session-&gt;save\_param(['name']);
160
161 If you wanted to store multiple form fields just include them all in the second list:
162
163 $session-&gt;save\_param(['name', 'email']);
164
165 If you want to store all the available I parameters you can omit the arguments:
166
167 $session-&gt;save\_param();
168
169 See L&lt;save\_param()|CGI::Session/save\_param&gt; for more details.
170
171 When storing data in the session you're not limited to strings. You can store arrays, hashes and even most objects. You will need to pass them as references (except objects).
172
173 For example, to get all the selected values of a scrolling list and store it in the session:
174
175 my @fruits = $cgi-&gt;param('fruits'); $session-&gt;param('fruits', \\@fruits);
176
177 For parameters with multiple values save\_param() will do the right thing too. So the above is the same as:
178
179 $session-&gt;save\_param($cgi, ['fruits']);
180
181 All the updates to the session data using above methods will not reflect in the data store until your application exits, or C&lt;$session&gt; goes out of scope. If, for some reason, you need to commit the changes to the data store before your application exits you need to call L&lt;flush()|CGI::Session/flush()&gt; method:
182
183 $session-&gt;flush();
184
185 I've written a lot of code, and never felt need for using C&lt;flush()&gt; method, since CGI::Session calls this method at the end of each request. There are, however, occasions I can think of one may need to call L&lt;flush()|CGI::Session/flush()&gt;.
186
187 =head2 ACCESSING STORED DATA
188
189 There's no point of storing data if you cannot access it. You can access stored session data by using the same L&lt;param() method|CGI::Session/param()&gt; you once used to store them. Remember the Username field from the previous section that we stored in the session? Let's read it back so we can partially fill the Login form for the user:
190
191 $name = $session-&gt;param("name"); printf "
192
193 <input name="\"name\"" type="\"text\"" value="\"%s\"" />
194
195 ", $name;
196
197 To retrieve previously stored @fruits do not forget to de reference it:
198
199 @fruits = @\{ $session-&gt;param('fruits') \};
200
201 Very frequently, you may find yourself having to create pre-filled and pre-selected forms, like radio buttons, checkboxes and drop down menus according to the user's preferences or previous action. With text and textareas it's not a big deal - you can simply retrieve a single parameter from the session and hard code the value into the text field. But how would you do it when you have a group of radio buttons, checkboxes and scrolling lists? For this purpose, CGI::Session provides L&lt;load\_param()|CGI::Session/load\_param()&gt; method, which loads given session parameters to a CGI object (assuming they have been previously saved with L&lt;save\_param()|CGI::Session/save\_param()&gt; or alternative):
202
203 $session-&gt;load\_param($cgi, ["fruits"]);
204
205 Now when you say:
206
207 print $cgi-&gt;checkbox\_group(fruits=&gt;['apple', 'banana', 'apricot']);
208
209 See L&lt;load\_param()|CGI::Session/load\_param()&gt; for details.
210
211 Generated checkboxes will be pre-filled using previously saved information. To see example of a real session-powered application consider <http://handalak.com/cgi-bin/subscriptions.cgi>
212
213 If you're making use of L&lt;HTML::Template|HTML::Template&gt; to separate the code from the skin, you can as well associate L&lt;CGI::Session|CGI::Session&gt; object with HTML::Template and access all the parameters from within HTML files. We love this trick!
214
215 $template = new HTML::Template(filename=&gt;"some.tmpl", associate=&gt;$session); print $template-&gt;output();
216
217 Assuming the session object stored "first\_name" and "email" parameters while being associated with HTML::Template, you can access those values from within your "some.tmpl" file now:
218
219 Hello [ ](mailto:<TMPL_VAR email>)!
220
221 See L&lt;HTML::Template's online manual|HTML::Template&gt; for details.
222
223 =head2 CLEARING SESSION DATA
224
225 You store session data, you access session data and at some point you will want to clear certain session data, if not all. For this purpose L&lt;CGI::Session|CGI::Session&gt; provides L&lt;clear()|CGI::Session/clear()&gt; method which optionally takes one argument as an arrayref indicating which session parameters should be deleted from the session object:
226
227 $session-&gt;clear(["~logged-in", "email"]);
228
229 Above line deletes "~logged-in" and "email" session parameters from the session. And next time you say:
230
231 $email = $session-&gt;param("email");
232
233 it returns undef. If you omit the argument to L&lt;clear()|CGI::Session/clear()&gt;, be warned that all the session parameters you ever stored in the session object will get deleted. Note that it does not delete the session itself. Session stays open and accessible. It's just the parameters you stored in it gets deleted
234
235 See L&lt;clear()|CGI::Session/clear()&gt; for details.
236
237 =head2 DELETING A SESSION
238
239 If there's a start there's an end. If session could be created, it should be possible to delete it from the disk for good:
240
241 $session-&gt;delete();
242
243 The above call to L&lt;delete()|CGI::Session/delete()&gt; deletes the session from the disk for good. Do not confuse it with L&lt;clear()|CGI::Session/clear()&gt;, which only clears certain session parameters but keeps the session open.
244
245 See L&lt;delete()|CGI::Session/delete()&gt; for details.
246
247 =head2 EXPIRATION
248
249 L&lt;CGI::Session|CGI::Session&gt; provides limited means to expire sessions. Expiring a session is the same as deleting it via delete(), but deletion takes place automatically. To expire a session, you need to tell the library how long the session would be valid after the last access time. When that time is met, CGI::Session refuses to retrieve the session. It deletes the session and returns a brand new one. To assign expiration ticker for a session, use L&lt;expire()|CGI::Session/expire()&gt;:
250
251 $session-&gt;expire(3600); # expire after 3600 seconds $session-&gt;expire('+1h'); # expire after 1 hour $session-&gt;expire('+15m'); # expire after 15 minutes $session-&gt;expire('+1M'); # expire after a month and so on.
252
253 When session is set to expire at some time in the future, but session was not requested at or after that time has passed it will remain in the disk. When expired session is requested CGI::Session will remove the data from disk, and will initialize a brand new session.
254
255 See L&lt;expire()|CGI::Session/expire()&gt; for details.
256
257 Before CGI::Session 4.x there was no way of intercepting requests to expired sessions. CGI::Session 4.x introduced new kind of constructor, L&lt;load()|CGI::Session/load()&gt;, which is identical in use to L&lt;new()|CGI::Session/new()&gt;, but is not allowed to create sessions. It can only load them. If session is found to be expired, or session does not exist it will return an empty CGI::Session object. And if session is expired, in addition to being empty, its status will also be set to expired. You can check against these conditions using L&lt;empty()|CGI::Session/empty()&gt; and L&lt;is\_expired()|CGI::Session/is\_expired()&gt; methods. If session was loaded successfully object returned by C&lt;load()&gt; is as good a session as the one returned by C&lt;new()&gt;:
258
259 $session = CGI::Session-&gt;load() or die CGI::Session-&gt;errstr; if ( $session-&gt;is\_expired ) \{ die "Your session expired. Please refresh your browser to re-start your session"; \} if ( $session-&gt;is\_empty ) \{ $session = $session-&gt;new(); \}
260
261 Above example is worth an attention. Remember, all expired sessions are empty sessions, but not all empty sessions are expired sessions. Following this rule we have to check with C&lt;is\_expired()&gt; before checking with C&lt;is\_empty()&gt;. There is another thing about the above example. Notice how its creating new session when un existing session was requested? By calling C&lt;new()&gt; as an object method! Handy thing about that is, when you call C&lt;new()&gt; on a session object new object will be created using the same configuration as the previous object.
262
263 For example:
264
265 $session = CGI::Session-&gt;load("driver:mysql;serializer:storable", undef, \{Handle=&gt;$dbh\}); if ( $session-&gt;is\_expired ) \{ die "Your session is expired. Please refresh your browser to re-start your session"; \} if ( $session-&gt;is\_empty ) \{ $session = $session-&gt;new(); \}
266
267 Initial C&lt;$session&gt; object was configured with B as the driver, B as the serializer and B&lt;$dbh&gt; as the database handle. Calling C&lt; new() &gt; on this object will return an object of the same configuration. So C&lt; $session &gt; object returned from C&lt; new() &gt; in the above example will use B as the driver, B as the serializer and B&lt;$dbh&gt; as the database handle.
268
269 See L&lt;is\_expired()|CGI::Session/is\_expired()&gt;, L&lt;is\_empty()|CGI::Session/is\_empty()&gt;, L&lt;load()|CGI::Session/load()&gt; for details.
270
271 Sometimes it makes perfect sense to expire a certain session parameter, instead of the whole session. I usually do this in my login enabled sites, where after the user logs in successfully, I set his/her "\_logged\_in" session parameter to true, and assign an expiration ticker on that flag to something like 30 minutes. It means, after 30 idle minutes CGI::Session will L&lt;clear|CGI::Session/clear()&gt; "\_logged\_in" flag, indicating the user should log in over again. I agree, the same effect can be achieved by simply expiring() the session itself, but by doing this we would loose other session parameters, such as user's shopping cart, session-preferences and the like.
272
273 This feature can also be used to simulate layered authentication, such as, you can keep the user's access to his/her personal profile information for as long as 60 minutes after a successful login, but expire his/her access to his credit card information after 5 idle minutes. To achieve this effect, we will use L&lt;expire()|CGI::Session/expire()&gt; method again:
274
275 $session-&gt;expire(\_profile\_access, '1h'); $session-&gt;expire(\_cc\_access, '5m');
276
277 With the above syntax, the person will still have access to his personal information even after 5 idle hours. But when he tries to access or update his/her credit card information, he may be displayed a "login again, please" screen.
278
279 See L&lt;expire()|CGI::Session/expire()&gt; for details.
280
281 This concludes our discussion of CGI::Session programming style. The rest of the manual covers some L&lt;"SECURITY"&gt; issues. Driver specs from the previous manual were moved to L&lt;CGI::Session::Driver|CGI::Session::Driver&gt;.
282
283 =head1 SECURITY
284
285 "How secure is using CGI::Session?", "Can others hack down people's sessions using another browser if they can get the session id of the user?", "Are the session ids easy to guess?" are the questions I find myself answering over and over again.
286
287 =head2 STORAGE
288
289 Security of the library does in many aspects depend on the implementation. After making use of this library, you no longer have to send all the information to the user's cookie except for the session id. But, you still have to store the data in the server side. So another set of questions arise, can an evil person get access to session data in your server, even if he does, can he make sense out of the data in the session file, and even if he can, can he reuse the information against a person who created that session. As you see, the answer depends on yourself who is implementing it.
290
291 =over 4
292
293 =item \*
294
295 First rule of thumb, do not store users' passwords or other sensitive data in the session, please. If you have to, use one-way encryption, such as md5, or SHA-1-1. For my own experience I can assure you that in properly implemented session-powered Web applications there is never a need for it.
296
297 =item \*
298
299 Default configuration of the driver makes use of L&lt;Data::Dumper|Data::Dumper&gt; class to serialize data to make it possible to save it in the disk. Data::Dumper's result is a human readable data structure, which, if opened, can be interpreted easily. If you configure your session object to use either L&lt;Storable|CGI::Session::Serialize::storable&gt; or L&lt;FreezeThaw|CGI::Session::Serialize::freezethaw&gt; as a serializer, this would make it more difficult for bad guys to make sense out of session data. But don't use this as the only precaution. Since evil fingers can type a quick program using L&lt;Storable|Storable&gt; or L&lt;FreezeThaw|FreezeThaw&gt; to decipher session files very easily.
300
301 =item \*
302
303 Do not allow anyone to update contents of session files. If you're using L serialized data string needs to be eval()ed to bring the original data structure back to life. Of course, we use L&lt;Safe|Safe&gt; to do it safely, but your cautiousness does no harm either.
304
305 =item \*
306
307 Do not keep sessions open for very long. This will increase the possibility that some bad guy may have someone's valid session id at a given time (acquired somehow). To do this use L&lt;expire()|CGI::Session/expire()&gt; method to set expiration ticker. The more sensitive the information on your Web site is, the sooner the session should be set to expire.
308
309 =back
310
311 =head2 SESSION IDs
312
313 Session ids are not easily guessed (unless you're using L)! Default configuration of CGI::Session uses L&lt;Digest::MD5|CGI::Session::ID::md5&gt; to generate random, 32 character long identifier. Although this string cannot be guessed as easily by others, if they find it out somehow, can they use this identifier against the other person?
314
315 Consider the scenario, where you just give someone either via email or an instant messaging a link to a Web site where you're currently logged in. The URL you give to that person contains a session id as part of a query string. If the site was initializing the session solely using query string parameter, after clicking on that link that person now appears to that site as you, and might have access to all of your private data instantly.
316
317 Even if you're solely using cookies as the session id transporters, it's not that difficult to plant a cookie in the cookie file with the same id and trick the web browser to send that particular session id to the server. So key for security is to check if the person who's asking us to retrieve a session data is indeed the person who initially created the session data.
318
319 One way to help with this is by also checking that the IP address that the session is being used from is always same. However, this turns out not to be practical in common cases because some large ISPs (such as AOL) use proxies which cause each and every request from the same user to come from different IP address.
320
321 If you have an application where you are sure your users' IPs are constant during a session, you can consider enabling an option to make this check:
322
323 use CGI::Session ( '-ip\_match' );
324
325 For backwards compatibility, you can also achieve this by setting $CGI::Session::IP\_MATCH to a true value. This makes sure that before initializing a previously stored session, it checks if the ip address stored in the session matches the ip address of the user asking for that session. In which case the library returns the session, otherwise it dies with a proper error message.
326
327 =head1 LICENSING
328
329 For support and licensing see L&lt;CGI::Session|CGI::Session&gt;