WebReference.com - 2 Ways to Implement Session Tracking (4/4)
2 Ways to Implement Session Tracking
This is one of the most famous methods and the one used by almost all professional sites. This allows you complete flexibility and whatever you want as far as session tracking is concerned; but it is not as easy as the other 2 methods. Besides, some applications may not allow cookies in which case you have to revert back to the other 2 methods. I had designed Web sites using WML (Wireless Markup Language) which worked on WAP based cell phones. Unfortunately the cellphones did not have enough memory to support cookies, so I had to use hidden fields to get session tracking working. But cookies generally work on almost every computer, except when a user may have blocked all cookies for security reasons in which case you would once again have to use either of the other 2 methods.
Using cookies is probably the best and the neatest of all the methods to maintain sessions. Cookies are basically small text files that are stored on the user's computers. They have information pertaining to that user. Once the cookie is created on the user's computer then for every further request made by that user in that session, the cookie is sent along with the request. The value of every cookie can be unique (for users browsing a particular Web site), so the server side program can differentiate between various users.
The method to program cookies is different for different languages.
Most of the languages provide some class that covers all the details
of cookie creation and maintenance. For example in Java you have
javax.servlet.http.Cookie class that is used to work
with cookies. Since I have decided to keep this article language
neutral and I had not planned to discuss cookies in depth I will
not go into the details of cookie programming here, but leave that
as an exercise for readers who are interested in pursuing it.
For beginners however I suggest either of the first two methods to implement session tracking. Rather than facing the learning curve associated with cookies you could manage with one of the earlier 2 techniques that you could implement using any language. My first preference is always for hidden fields. But in cases where I am not dealing with forms as such (which generally doesn't happen) I also use URL Rewriting.
I hope this article gave you a sound introduction to session tracking. I am sure you can use the knowledge presented here for your personal programming needs. However in case you plan to implement a professional Web site then I would suggest that you look into APIs specifically designed for session tracking which would do all the above mentioned stuff for you automatically without you having to worry about the nitty-gritty details.
# # # #
About the Author
Kiran Pai has been developing software for the last 4 years and has been actively involved in Web development for the last couple of years. Besides coding, Floyd and Quake are the only two things that interest him.
Created: March 27, 2002
Revised: March 27, 2002