What is a PHP session?
You can assume the session in PHP or in other scripting language for that matter, as something which keeps track of a specific request. The normal without any session request is processed by a web server as below.
- The browser makes a request.
- The web server accepts the request and processes it. Finally it sends back some response to the requester (the browser, mostly) and forgets about the request.
- If the same browser again makes some other requests, the web server does not know that it has just served this specific browser. It treats the request as a new request.
A typical transaction using the session object will be something like this.
- The browser makes a request to the web server.
- This time the web server creates a web session or simply session object on its own machine(web server's) and assign some unique name or id to it. This unique Id is often called as the session id.
- The web server processes the request and sends the session id as header while sending response back to the browser.
- When the browser makes request to the web server, it sends the session id also with the request. The web server check for availability of the session corresponding to the id. If it finds the session live, It knows that this is not the first time the browser has requestd some services!
Why do we use sessions?
We use sessions to generally keep the track of a successfully logged in user. We store some user-specific information in the session object. This information might be the user id of a successfully logged in user, or the shopping contents on a cart based website etc.
Advantage of using SESSION object.
As compared to cookies, the advantage of using a session is that all the information are stored on the server machine only hence it is more secure.
Disadvatnage of using session.
The performance might degrade if lots of session objects co-exist on the server machine. This will result in delayed response and might annoy the end users. However using good dedicated hosting solutions may help you. With dedicted hosting the server resources are not shared by anyone else. All the CPU time is available to your own application only, Which results in better performance.
After so much gyan let us learn PHP specific Sessions. In the next section we will learn how to create a PHP session.

