Sessions in php

A PHP session is easily started by making a call to the session_start() function.This function first checks if a session is already started and if none is started then it starts one. It is recommended to put the call to session_start() at the beginning of the page.
Session variables are stored in associative array called $_SESSION[]. These variables can be accessed during lifetime of a session.

Make use of isset() function to check if session variable is already set or not.

When we work with our website, you open it, do some changes, and then you close it. This is much like a Session. The computer knows who we are. It knows when you start the website and when you end. But on the internet there is one problem: the web server does not know who you are or what you do, because the HTTP address doesn’t maintain state.
Session variables solve this problem by storing user information to be used across multiple pages. By default, session variables last until the user closes the browser.

So; Session variables hold information about one single user, and are available to all pages in one application.

A session creates a file in a temporary directory on the server where registered session variables and their values are stored. This data will be available to all pages on the site during that visit.

It is important to ponder if the sessions’ temporary storage is applicable to your website. If you require a more permanent storage you will need to find another solution, like a MySQL database.

How to start PHP Session
A session is started with the session_start() function.
Session variables are set with the PHP global variable: $_SESSION.
Now we create a file and start session store some value in session

 

How to Get Session Variable Values

For getting session value we first create a page. All field values are stored in the global $_SESSION variable. read more about get post method in php

 

 

How to Modify a PHP Session Variable value
We need to just override the variable values example are below:

 

How to Destroy a PHP Session
To remove all global session variables and destroy the session, use session_unset() and session_destroy():
Example

 

 

I, Dayanand Saini,the founder and chief editor of www.webtechsource.com,have been working as a software engineer from last 4+ years. It's my hobby to learn new things and implement on live environment.

Leave a Reply

Your email address will not be published. Required fields are marked *

3 + 5 =

%d bloggers like this: