How to make a session in PHP language

By using session you can store multiple information (e.g. Username, address, product, colour etc.) in it and easily start session on any page .session end when the user close the browser. Session can hold complete information of single user and can use in multiple pages and it’s by default a global variable $_SESSION.

 

  • Session is started with session_start()

 

  • Start session must be first thing in document.

 

Now here we create a page “example_session1.php”.

<?php
//From here we Start the session

session_start();

?>
<!DOCTYPE html>
<html>
<body>

<?php
// Set session variables
$_SESSION[“myToyota”] = ” Prado tx “;
$_SESSION[“myNokia”] = ” lumia “;
echo “Here your session variable is stored.”;
?>
</body>
</html>

  • Now create another page with “ php “ to echo your stored session on other page.

 

<?php

Session_start();

?>

<! DOCTYPE html>
<html>
<body>

<?php
// Echo your stored session variables from example_session1.php.
echo “My favorite car is Toyota ” . $_SESSION[“mytoyota”] . “.<br>”;
echo ” My favorite cellphone is Nokia ” . $_SESSION[“mynokia”] . “.”;
?>

</body>
</html>