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.








All session delete.

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.

[table id=10 /]