php start session code example

Example 1: end session variable php

// destroy the session
<?php
session_destroy();
?>

Example 2: php start a session

session_start();
if(!isset($_SESSION["started"]){ //Check if the session already exist
    //If it does not exist we append attributes we need in
	$_SESSION["started"] = true;
  	$_SESSION["attribute_1"] = $value_1;  
  	...
    $_SESSION["attribute_n"] = $value_n;      
}

Example 3: session initialize php

session_start();

Example 4: destroy session php

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

Example 5: 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 6: 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.";
?>