Blogs

image
image
image

Create Session Use Php

When you use an application, you open it, make changes, and then close it. This is similar to a Session. The computer recognises you. It recognises when you start and stop the application. However, there is one issue on the internet: the web server has no idea who you are or what you do because the HTTP address does not maintain state. Session variables fix this problem by saving user information that may be utilised across different pages (for example, username, favourite colour, and so on). Session variables are retained by default until the user exits the browser. So, session variables store information about a single user and are accessible to all pages within a single application.

Start a Session with PHP

The session_start() function initiates a session.
Session variables are set with the PHP global variable: $_SESSION.

 

Get the PHP Session Variable Values

Next, we create another page called "Test_session02.php". From this page, we will access the session information we set on the first page ("test_session01.php").

Notice that session variables are not passed individually to each new page, instead, they are retrieved from the session we open at the beginning of each page (session_start()).

Also, notice that all session variable values are stored in the global $_SESSION variable:
  

 

Modify Session Variable value in PHP

Overwrite a session variable to change it:

Destroy a PHP Session

To remove all global session variables and destroy the session, use session_destroy() And session_unset():