Author: Khawar Abbas
My Articles :: Session concepts for beginners

Introduction
Session! This article describes session for beginners so that they can easily understand and use it in different applications.
Explanation
Session! Here are few questions related to sessions.
Q: What is a session?
A: A session is defined as the period of time that a unique user interacts with a Web application.
Q: When does session start?
A: The answer is very simple. When a browser sends a first request to a server, session starts. For beginners when you open a browser and type any website and hit enter key, a browser sends a request to server. When server receives a request session starts. So do not mix it with session variables. Some developers say session starts when we login to any website which is wrong. Actually session starts when a first request is sent to a server from your browser.
Q: How can I test when session starts?
A: If you are a Dot net developer, you can make a web application and add a file global.asax. In this file you can find an event "session_start" as shown below:
void Session_Start(object sender, EventArgs e)
{
Int I = 0;
}
You can write some code in this event and can apply break point on the code in this event. When you run the application you will see that the application will go to this break point which will show you that session is started.
Q: What is a session variable?
A: A session variable is like a normal variable. The session variable stores or keeps the value/data. The life of session variable is associated with session. If session expires, all of the session variables destroy. After session expire you cannot get the value of session variable.
Q: Where session is stored:
A: Session is stored on the server.
Q: How can I save value in session variable?
A: Here is the code to save the value in session variable. The name of the variable in the below code is UserID.
Session["UserID"] = 1;
Note: You can access session variable value on any page of the application if session is alive.
Q: How can I retrieve the value of session variable?
A: The code is given below:
String strId = Session["UserId"].ToString();
Q: What is a session state?
A: A Session state is nothing more than memory in the shape of a dictionary or hash table, e.g. key-value pairs, which can be set and read for the duration of a user's session.
Q: Where does session variable store?
A: Session variables are stored on server.