session php variable code example

Example 1: 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 2: session php

<?php
// start session before any HTML
session_start();

// create some variales in $_SESSION
$_SESSION['name'] = 'myname';
$_SESSION['surname'] = 'mysurname';
$_SESSION['age'] = 24;
?>

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title>My page</title>
    </head>
    <body>
    <p>
        Hey <?php echo $_SESSION['name']; ?> !<br />
        How are you today ?
    </p>