PHP Sessions - ProgrammerTech
Language:

Search

PHP Sessions

  • Share this:
PHP Sessions

What is a PHP Session?

session, our lesson today is useful for those who study the php language and it is about session, we will explain how to create a variable of type session and this type of variable is used to save data in the cache in the browser. This means when a variable of type session is created and since the data is saved in the browser's cache. You can later use this data on any other page you want, even if it is not the page where you created this variable. And since the data is saved in the cache in the browser, it means that when the user closes the browser, that data will be deleted automatically, and this point is important.

Before we explain to you how to create a variable of type session, we will explain some of the functions that we will use.

Functions that are used with sessions

  1. session_start.
  2. session_unset.
  3. session_destroy.

 

Start a PHP Session

session_start means the start of the session, meaning after a variable of type session is created or the value of this variable is modified to access the data previously stored within one of the variables of type session...etc.

 

Unset a PHP Session

session_unset  means when executing this function, the variables of the session type will be deleted, and you can specify by yourself when you want the following command to be executed, an important point before executing this command, you must be the initiator of the session before in order to be able to use this command.

 

Destory a PHP Session

session_destroy means to destroy the session and is used to terminate the started session.

 

Difference between session_unset and session_destroy

In short, the difference is that when you use session_unset it will tell the browser to delete all variables of type session. And after this command is executed, other variables of the type session can be created, meaning you have not yet ended the session. And when we use session_destroy, it will destroy the session and end it. If we want to start a new session, we must call the session_start command. And then we use the destroy command. The previous session has been terminated, only that's the difference between them.

Its general form is

session_start();
$_SESSION["name"] = "Ahmed kaissar";

In the beginning, we started the session through session_start, and then we created a variable of type session and written in the form. We put the $ sign first, followed by the _ sign, and we write the word SESSION in capital letters, it should not be placed in lowercase letters. And then we opened square brackets and wrote within it the name of the variable, then we assigned the value to it after the equal sign.

example 1

<?php
session_start();
$_SESSION["email"] = "javascript23685@gmail.com";
?>
<h1><?php echo $_SESSION["email"] ?></h1>

We have started the session at the beginning with the start command session_start and then we created a variable of type session and name email and stored in it. And we display the data by using the h1 tag to print what is in the variable via the echo function to print. And then we wrote the name of the variable with its definition. And when we open this code in the browser, the result will be:

javascript23685@gmail.com

 

Modify the value owned by session

We can modify the values ​​owned by this variable by writing the same as the previous variable and setting a new value directly.

<?php
session_start();
$_SESSION["email"] = "javascript23685@gmail.com";
$_SESSION["email"] = "programmertech@gmail.com";
?>
<h1><?php echo $_SESSION["email"] ?></h1>

We have written the same variable definition and name and then assign it the new value we want to it directly. And when we execute this code, we will get the following result.

programmertech@gmail.com

 

Delete all variables of type session

<?php
session_start();
$_SESSION["email"] = "javascript23685@gmail.com";
$_SESSION["email"] = "programmertech@gmail.com";
session_unset();
?>
<h1><?php echo $_SESSION["email"] ?></h1>

We have deleted all variables of type session after the modification command to the value of the first variable. And we used the command session_unset to delete these variables, which means that all the variables have been deleted. And when this code is executed, an error will appear.

Sessions php

 

We notice an error has appeared, and when using session_destroy only it will end the session, it means that the variables that reside in the cache will not be deleted. There are some points for protection and sometimes we don't want to display the error. We first want php to check if the variable we want to use exists or not. If it is present, we want to execute orders, and if it is not present, we want to display other data.

<?php
session_start();
$_SESSION["email"] = "javascript23685@gmail.com";
$_SESSION["email"] = "programmertech@gmail.com";
session_unset();
?>
<h2>
    <?php
    if(!empty($_SESSION["email"])){
        echo $_SESSION["email"];
    }else{
        echo "The variable does not exist";
    }
    ?>
</h2>
<h1><?php echo $_SESSION["email"] ?></h1>

We terminate the sessions and then use the conditions with the empty function to reveal whether the variable exists or not. We set an if condition. If the variable exists, print the e-mail, meaning the value of the variable, and if it does not exist, print the variable that does not exist. And when we run the code, we will get the result.

php sessions

 

Here we have learned how to create a variable of the type session and how to modify it and also how to use it to start and end the session and delete the variables.


Tags:
Muhammad Alush

Muhammad Alush

إسمي محمد تركي العلوش من سورية من ريف حلب الشرقي 1994, محب للتقنية و التعلم الذاتي, و جميع خبراتي تعلمتها ذاتياً, و اللغات البرمجية التي أتقنها هي html, css, javascript, php, c, python بالإضافة إلى حبي الكبير لمجال الشبكات.

موقعي programmer-tech.com