PHP Session Function (Start and Destroy)

What is a Session in PHP?

PHP Sessions is a way to keep information from one web page to the next so that users can be recognized as they move around a site or app.

Do you want to know why a website needs sessions?

To understand why sessions are important, we need to look at how the HTTP protocol is set up.

The HTTP protocol is stateless, which means that a server can’t remember a specific user between requests.

For instance, when you visit a website, the server’s only job is to give you the information you asked for.

So, when you go to other pages on the same website, the web server handles each request as if it had nothing to do with the others.

The server has no way of knowing that each request comes from the same user.

Look at the illustration below:

Session In PHP

How do you start a PHP Session?

A session started with the session_start() function.

Session variables are set with the Hypertext Preprocessor global variable $_SESSION.

Now, let’s create a program with the file name “test_session1.php” to start a new session and set some session variables:

<?php
// Start the session
session_start();
?>
<!DOCTYPE html>
<html>
<body>

<?php
// Set session variables
$_SESSION["name"] = "Angel Jude Suarez";
$_SESSION["position"] = "Programmer";
echo "Session variables are set.";
?>

</body>
</html>

Note: The session_start() must be placed in the first part of the code of your php file before any HTML tags.

GET the values of PHP Session variables

Next, we will create another page with the file name “test_session2.php“.

From this page, we will GET the values of session variables we set on the first page with the file name “test_session1.php“.

Notice that the session variables in Hypertext Preprocessor 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().

Here’s the program for getting the values of session variables set on the first-page name “test_session1.php“.

<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>

<?php
// Echo session variables that were set on previous page
echo "Name is: " . $_SESSION["name"] . ".<br>";
echo "Position is: " . $_SESSION["position"] . ".";
?>

</body>
</html>

When you execute the program this will be the output:

Name is: Angel Jude Suarez
Position is: Programmer

Destroy a PHP Session

To remove all global session variables and destroy the session, we will use the session_unset() and session_destroy() functions.

Here’s the program for destroying global session variables:

<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>

<?php
// remove all session variables
session_unset();

// destroy the session
session_destroy();
?>

</body>
</html>

Frequently Asked Questions (FAQs)

What is PHP (Hypertext Preprocessor) Session and how does it work?

PHP responds by sending a unique token that identifies the current session.

This is known as the session ID. In all subsequent requests, the browser sends the session ID to say, “Hello IT Source Coders.

All other data related to the session is stored on the web server. Only the session ID gets passed back and forth.

How do you start a PHP Session?

A session started with the session_start() function.

When should I start a Session Hypertext Preprocessor?

Before you can store any information in session variables. The session_start() must be placed in the first part of the code of your php file before any HTML tag.

Conclusion

We have completely discussed the Session functions from session_start() and session_destroy().

In this tutorial, we have learned the function of Session with the help of examples. I hope this simple PHP Tutorial will help you a lot.

By the way, if you have any questions or suggestions about this article entitled “Session in PHP“. Please feel free to comment below. Thank You!

Leave a Comment