• PHP session is used to store and pass information from one page to another page temporarily until the user closes website.
  • In shopping website PHP website is widely used where we need to store and pass cart information from one page to another.
  • For example. username, product code, product name, product price etc.
  • The session creates unique user id for each browser to recognize the user and avoid conflict between multiple browsers.
  • PHP session_start () function is used to start the session and starts new and resume existing session.
  • If session is created already then it returns existing session and if session is not available, it returns and creates new session.
  • It is an associative array that contains all session variables and it is used to get and set session variables.

Syntax

<?php

session_start();

?>

Sample Code

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

<?php
// Echo session variables that were set on previous page
echo "Favorite color is " . $_SESSION["favcolor"] . ".<br>";
echo "Favorite animal is " . $_SESSION["favanimal"] . ".";
?>

</body>
</html>

Output

Categorized in: