WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

PHP 5 Sessions


A session is an approach to store data (in factors) to be utilized over numerous pages.

Unlike a treat, the data isn't put away on the clients computer.


What is a PHP Session?

When you work with an application, you open it, do a few changes, and afterward you close it. This is much similar to a Session. The PC knows your identity. It knows when you begin the application and when you end. In any case, on the web there is one issue: the web server does not know your identity or what you do, on the grounds that the HTTP address doesn't keep up state.

Session factors take care of this issue by putting away client data to be utilized over numerous pages (for example username, most loved shading, and so on). By default, session factors last until the client shuts the browser.

So; Session factors hold data around one single client, and are accessible to all pages in one application.


Start a PHP Session

A session is begun with the session_start() function.

Session factors are set with the PHP worldwide variable: $_SESSION.

Now, how about we make another page called "demo_session1.php". In this page, we begin a new PHP session and set some session variables:

Example

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

<?php
//Set session variables
$_SESSION["favcolor"] = "green";
$_SESSION["favanimal"] = "cat";
echo "Session factors are set.";
?>

</body>
</html>
Run precedent »

Get PHP Session Variable Values

Next, we make another page called "demo_session2.php". From this page, we will get to the session data we set on the primary page ("demo_session1.php").

Notice that session factors are not passed independently to each new page, rather they are recovered from the session we open toward the start of each page (session_start()).

Also see that all session variable qualities are put away in the worldwide $_SESSION variable:

Example

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

<?php
//Echo session factors that were determined to past page
echo "Favorite shading is " . $_SESSION["favcolor"] . ".<br>";
echo "Favorite creature is " . $_SESSION["favanimal"] . ".";
?>

</body>
</html>
Run precedent »

Another approach to demonstrate all the session variable qualities for a client session is to run the following code:

Example

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

<?php
print_r($_SESSION);
?>

</body>
</html>
Run precedent »

Modify a PHP Session Variable

To change a session variable, simply overwrite it:

Example

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

<?php
// to change a session variable, simply overwrite it
$_SESSION["favcolor"] = "yellow";
print_r($_SESSION);
?>

</body>
</html>
Run precedent »

Destroy a PHP Session

To expel all worldwide session factors and annihilate the session, use session_unset() also, session_destroy():

Example

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

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

//obliterate the session
session_destroy();
?>

</body>
</html>
Run precedent »