php create session variable code example

Example 1: session initialize php

session_start();

Example 2: destroy session php

<?php 
 session_start(); // start session
 session_destroy();  // Delete whole session
// OR
unset($_SESSION['username']); // delete any specific session only
?>

Example 3: set php varibianle session

<?php 
  // Start the session
  session_start();
?>
<!DOCTYPE html>
<html>
  <body>
  <?php
    // Set session variables
    $_SESSION["color"]= "blue";
    $_SESSION["animal"]= "dog";
    echo "The session variable are set up.";
  ?>
  </body>
</html>

Example 4: create session in php

<?php 
  // Start the session
  session_start();

  // Set session variables
  $_SESSION["color"]= "blue";
  $_SESSION["animal"]= "dog";
  echo "The session variable are set up.";
?>

Example 5: php session variables

<?php
   session_start();
   $_SESSION['var'];
?>

Example 6: echo session

print_r($_SESSION);

Tags:

Php Example