CGI-Session
view release on metacpan or search on metacpan
lib/CGI/Session/Tutorial.pm view on Meta::CPAN
package CGI::Session::Tutorial;
# $Id$
$CGI::Session::Tutorial::VERSION = '4.43';
=pod
=head1 NAME
CGI::Session::Tutorial - Extended CGI::Session manual
=head1 STATE MAINTENANCE OVERVIEW
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 make...
For our rescue come such technologies as I<HTTP Cookies> and I<QUERY_STRING>s that help us save the users' session for a certain period. Since I<HTTP Cookies> and I<QUERY_STRING>s alone cannot take us too far (B<RFC 2965, Section 5, "Implementation L...
Before we discuss this library, let's look at some alternative solutions.
=head2 COOKIE
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 re...
Although I<HTTP Cookies> 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 stor...
=head2 QUERY STRING
Query string is a string appended to URL following a question mark (?) such as:
http://my.dot.com/login.cgi?user=sherzodr;password=top-secret
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 ...
=head2 HIDDEN FIELDS
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...
Query strings and hidden fields are also lost easily by closing the browser, or by clicking the browser's "Back" button.
=head2 SERVER SIDE SESSION MANAGEMENT
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 us...
Advantages:
=over 4
=item *
We no longer need to depend on User Agent constraints in cookie size.
=item *
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<5767393932698093d0b75ef61437...
=item *
User will not have sensitive data stored in his/her computer in unsecured file (which is a cookie file).
=item *
It's possible to handle very big and even complex data structures transparently (which I<HTTP Cookies> do not handle).
=back
That's what CGI::Session is all about - implementing server side session management. Now is a good time to get feet wet.
=head1 PROGRAMMING STYLE
Server side session management system might be seeming awfully convoluted if you have never dealt with it. Fortunately, with L<CGI::Session|CGI::Session> all the complexity is handled by the library transparently. This section of the manual can be tr...
All applications making use of server side session management rely on the following pattern of operation regardless of the way the system is implemented:
=over 4
=item 1
Check if the user has session cookie dropped in his computer from previous request
=item 2
If the cookie does not exist, create a new session identifier, and drop it as cookie to the user's computer.
=item 3
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 expirat...
=item 4
Store any necessary data in the session that you want to make available for the next HTTP request.
=back
lib/CGI/Session/Tutorial.pm view on Meta::CPAN
$session->delete();
The above call to L<delete()|CGI::Session/delete()> deletes the session from the disk for good. Do not confuse it with L<clear()|CGI::Session/clear()>, which only clears certain session parameters but keeps the session open.
See L<delete()|CGI::Session/delete()> for details.
=head2 EXPIRATION
L<CGI::Session|CGI::Session> 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 b...
$session->expire(3600); # expire after 3600 seconds
$session->expire('+1h'); # expire after 1 hour
$session->expire('+15m'); # expire after 15 minutes
$session->expire('+1M'); # expire after a month and so on.
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 ...
See L<expire()|CGI::Session/expire()> for details.
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<load()|CGI::Session/load()>, which is identical in use to L<new()|CGI::Session/new()>, but is not allowed to...
$session = CGI::Session->load() or die CGI::Session->errstr;
if ( $session->is_expired ) {
die "Your session expired. Please refresh your browser to re-start your session";
}
if ( $session->is_empty ) {
$session = $session->new();
}
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<is_expired()> before checking with C<is_empty()>. There is another th...
For example:
$session = CGI::Session->load("driver:mysql;serializer:storable", undef, {Handle=>$dbh});
if ( $session->is_expired ) {
die "Your session is expired. Please refresh your browser to re-start your session";
}
if ( $session->is_empty ) {
$session = $session->new();
}
Initial C<$session> object was configured with B<mysql> as the driver, B<storable> as the serializer and B<$dbh> as the database handle. Calling C< new() > on this object will return an object of the same configuration. So C< $session > object return...
See L<is_expired()|CGI::Session/is_expired()>, L<is_empty()|CGI::Session/is_empty()>, L<load()|CGI::Session/load()> for details.
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 as...
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 informa...
$session->expire(_profile_access, '1h');
$session->expire(_cc_access, '5m');
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.
See L<expire()|CGI::Session/expire()> for details.
This concludes our discussion of CGI::Session programming style. The rest of the manual covers some L<"SECURITY"> issues. Driver specs from the previous manual were moved to L<CGI::Session::Driver|CGI::Session::Driver>.
=head1 SECURITY
"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.
=head2 STORAGE
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 s...
=over 4
=item *
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 We...
=item *
Default configuration of the driver makes use of L<Data::Dumper|Data::Dumper> 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. I...
=item *
Do not allow anyone to update contents of session files. If you're using L<default serializer|CGI::Session::Serialize::default> serialized data string needs to be eval()ed to bring the original data structure back to life. Of course, we use L<Safe|Sa...
=item *
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<expire()|CGI::Session/expire()> method to set expiration ticker. Th...
=back
=head2 SESSION IDs
Session ids are not easily guessed (unless you're using L<incr ID generator|CGI::Session::ID::incr>)! Default configuration of CGI::Session uses L<Digest::MD5|CGI::Session::ID::md5> to generate random, 32 character long identifier. Although this stri...
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 initia...
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 ch...
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 e...
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:
use CGI::Session '-ip_match';
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...
=head1 LICENSING
For support and licensing see L<CGI::Session|CGI::Session>
=cut
( run in 1.593 second using v1.01-cache-2.11-cpan-39bf76dae61 )