PHP Session

What is Session?
Session means a period of time. Day start from 12.00AM and end 11.59PM, during this period 12.00AM to 3.59AM is a mid-night session, 4.00AM to 7.59AM morning session, 8.00AM to 11.59AM day session and similarly goes on.

In PHP session is much similar to this. When a user opens an application do some changes and closed it, it’s also a session, the user session in this application.

Purpose of the session
The computer knows the user, who open it or which activities are done and then close it by cookies. But HTTP has no way to identify the user because HTTP is stateless.

When at a time many users come in any website HTTP does not recognize who is come and what they do?
That’s why PHP gives SESSION. When user1 open any website through browser one unique session id create in the server for the user1 browser and then user2 open same website through another browser create another new unique session id in the server for user2. In this way, the above problem can be solved by session.
In PHP any value (using variable) can store in a server for certain time and use it in multiple web pages in one web application. Session creates the unique id for every user in the server and using those unique id servers knows the user and their activities.

Syntax of Session
Use session in the program always starts the session at first. session_start() is a function which used to start a session. Store value in the session using $_SESSION global variable.

Example:

menu.php

set.php

file1.php

file2.php

unset.php

destroy.php

Output
All session delete.

Explanation
In this above example when set.php run on the server a session file create with sess_5bcttfn143tev4uaj835t3e041 name. 5bcttfn143tev4uaj835t3e041 this is the unique id where session value saves on the server.
Session file creates in the server tmp folder. In every browser, this unique id is changed. If user open browser and run set.php then another session id create in the server.
session_id() function return the current session id value on the browser.
Using session any variable easily access multiple files by $_SESSION global variable so above example file1 and file2 is used to the show this. User can use $_SESSION[‘fstnm’] and $_SESSION[‘lstnm’] variable any other pages also in this application.
Session expiry time in the server until user closed the browser.
If the user closed the browser and the run file1.php or file2.php again then the value will be blank only return

Set | File1 | File2 | Unset | Destroy

File 1 User name

If again user wants to set session then set.php runs in the server.

Session variable Unset and Destroy
session_destroy() function is used to manually destroy the session.

In above example, if the user runs destroy.php in the server the session file delete from tmp folder and all session variables are empty.

If the user does not want to delete all session the unset() function is used to empty particularly any session variable.
In the above example, unset.php is shown this.

You have already seen PHP Cookies. Lets see the what is the difference
Difference between Session and Cookie:
Session and Cookie is much similar in using purpose in PHP because both can store value one time and access multiple pages by the global variable in any one application.
But session and cookie both are worked in different field. Cookie worked in browser and session in server.

Sr. NoSESSIONCOOKIE
1Session is server side. So session is secured than cookie. Session value stored in the server, so difficult to directly access session file.Cookie is client side. If anyone wants to know the cookie value then easily access from browser and also can modify it directly in the cookie file.
2In session can store any type of value. In cookie can’t store array.
3Session file size is large then cookie. There are no size limit to store data in sessionCookie file size maximum 4kb. User can’t store more than that value.
4Session expire when browser is closedCookie default time also same, if expiry parameter is not set in cookie. Otherwise cookie is valid till the expiry time.
5Set and retrieve session always need session_start() at the beginning of the program. In cookie there have no need to cookie start.
6Destroy a session need to use session_destroy(). Destroy cookie there have no cookie destroy function. Cookie automatically delete after complete the expiry time.
Otherwise set cookie for past time.