Tags
Asked 2 years ago
13 Jul 2021
Views 141
Rosemarie

Rosemarie posted

Why do we need to use a session to preserve data across multiple PHP pages ?

Why do we need to use a session to preserve data across multiple PHP pages ?

answered Aug 22 '21 15:13


the session is used to preserve data across multiple PHP Pages, that's true. it is because
1. it is easy to store and retrieve data
how to store in session :

$_SESSION['logged_in_user_id']=123;


how to retrieve session data :

if(!empty($_SESSION['logged_in_user_id'])){
// user logged in do something.
}



2. we can save database interaction for data that we need frequently per page.
some data(cart info, logged in user info, ) need to load every time page load, so instead of asking every time to database store at session or cookies and it saves some energy on this planet called earth.

at end of the day, the session is stored on files or database. but it is very efficient .

3. session holds data until the specific time after auto delete it which is the auto clean system.
server manager can configure the holding time of session data at the server.
go to php.ini and change the holding time of the session at the server as per need.

the session will be stored in a server so it is easy to track some temporary data like user info, who logged in, and shopping cart info, etc
you can use cookies as well instead of session
Post Answer