Working with sessions in PHP

Share

PHP sessions are a way of storing data temporarily on the website. This file will be deleted when you close your browser, and it is available to all the pages on the site in order to access information about the user.

You need to specify the location where you want this file saved by setting the session.save_path attribute in your php.ini file. If you do not set this value, the session might malfunction.

Uses of the session in PHP

PHP allows for the initiation of sessions by using session_start(), which, by default, will check to see if an existing session is available. If no session exists, it will be created.

Sessions in PHP are a mechanism by which the server can store data associated with a user. This information is retained until the session is terminated, at which point it ceases to exist. In this article, we will explore an example and see how it works.

It is always recommended to place the “session_start” function at the beginning of your code, even before any HTML tags.

Starting PHP session

In this example, you will begin a session by tracking the number of times you have visited a specific website page. To do so, create a variable named visit and assign it the value 1.

<?php
   session_start();
   if( isset( $_SESSION['visit'] ) ) {
      $_SESSION['visit'] += 1;
   }else {
      $_SESSION['visit'] = 1;
   }
   $message = "This page is visited ".  $_SESSION['visit']." times";
?>
<html>
   <head>
      <title>PHP session</title>
   </head>
   <body>
      <?php  echo $message; ?>
   </body>
</html>

Output:

Store value in PHP session

You can access the value of a session variable by using the global $_SESSION variable. For example, in the following code snippet, you will create another instance of PHP which stores your name.

<?php  
    session_start();  
?>  
<html>  
<body>  
<?php  
    $_SESSION["name"] = "Codewithsudeep";
    echo "Information stored in session variable.<br/>";  
?>
</body>
</html>

Retrieve session data

You will use a different file to access the variable after it has been set. To access the name variable you just set, create a new file and add the following code.

Please take note that loading both pages will end your session if you then close your browser.

<?php  
    session_start();  
?>  
<html>  
<body>  
<?php  
    echo "Username: ".$_SESSION["name"];  
?>  
</body>  
</html>  

Output

Destroy PHP session

Although the web server will automatically end the session when the browser is closed, you can also end it manually. You can do this with the aid of two functions.

session_destroy(): it will remove all session variables from the system.
unset(): Just the provided session variable will be killed when this function is called.
To eliminate all the variables in a session, use the session_unset() function.

Let’s examine how to eliminate the counter variable that you set up during one of the aforementioned sessions.

Example: Using unset() to Destroy a Session Variable

?php
    unset($_SESSION[‘visit’]);
?>

Example: Using session_destroy() to Destroy a Complete Session in PHP

<?php
    session_destroy();
?>

There are no parameters that the PHP session destroy() method accepts. In PHP, simply invoking this function will end the current session.

Other PHP articles: PHP

Github repo to practice PHP: https://github.com/CodewithSudeep/Guide-For-PHP

Share
Sudeep Mishra

Sudeep Mishra

Healing