sessionfirst.php

<html>

<head>

<title>Session</title>

</head>

 

<body>

<form  action="sessionsecond.php" id="formfirst" name="formfirst" method="post">

  <p>

    <label for="name">Name:</label>

    <input type="text" name="name" id="name" />

    <br /><br />

    <label for="major">Major:</label>

    <input type="text" name="major" id="major" />

    <br /><br />

    <input type="submit" name="Submit" value="Submit" />

  </p>

</form>

</body>

</html>

 

sessionsecond.html

<?php

session_start();

if ($_POST && !empty($_POST['name'])) {

  $_SESSION['name'] = $_POST['name'];

  $_SESSION['major'] = $_POST['major'];

  }

?>

<html>

<head>

<title>Session Second</title>

</head>

<body>

<?php

if (isset($_SESSION['name'])) {

  echo 'Hello, '.$_SESSION['name']. ' your major is ' . $_SESSION['major'].'. <a href="sessionthird.php">Continue to third page</a>';

  }

else {

  echo 'I do not recognize you! <a href="sessionfirst.php">Please login</a>';

  }

?>

</body>

</html>

 

sessionthird.php

<?php

session_start();

ob_start();

?>

<html>

<head>

<title>Session third</title>

</head>

<body>

<?php

if (isset($_SESSION['name'])) {

  echo 'Hello, '.$_SESSION['name']. ' your major is '.$_SESSION['major'].'. This is the third page and it remembered your name and major!<br />';

  unset($_SESSION['name']);

  if (isset($_COOKIE[session_name()])) {

    setcookie(session_name(), '', time()-86400, '/');

    }

  ob_end_flush();

  session_destroy();

  echo '<a href="sessionfourth.php">Page 4</a>';

  }

else {

  echo 'The name is not remembered. You will have to login again<br />';

  echo '<a href="sessionfirst.php">Return to first page to login</a>';

  }

?>

</body>

</html>

 

This program starts the session.  Make sure that session_start() is put in immediately after the opening PHP tag and no white space ahead of the PHP tag.  Note that to end the session I am using session_destroy().

Then using ob_start() lets you set things to save output in a buffer.  Note that ob_end_flush () can be used to flush the buffer.

The if(isset… checks to make sure that the session variable is set.  You can unset the session variable using the unset/

I have also invalidated the session cookie once I have used it.

 

sessionfour.php

<?php

session_start();

if ($_POST && !empty($_POST['name'])) {

  $_SESSION['name'] = $_POST['name'];

  $_SESSION['major'] = $_POST['major'];

  }

?>

<html>

<head>

<title>Session Second</title>

</head>

<body>

<?php

if (isset($_SESSION['name'])) {

  echo 'Hello, '.$_SESSION['name']. ' your major is ' . $_SESSION['major'].'. <a href="sessionthird.php">Continue to third page</a>';

  }

else {

  echo 'I do not recognize you! <a href="sessionfirst.php">Please login</a>';

  }

?>

</body>

</html>

 

Now I am going to look at a few ways to change sessionone.php.  First I am going to use a JavaScript event to set the focus on the form to the first text box.

 

sessionfirsta.php

<html>

<head>

<title>Session</title>

</head>

 

<body onload="document.formfirst.name.focus();">

<form  action="sessionsecond.php" id="formfirst" name="formfirst" method="post">

  <p>

    <label for="name">Name:</label>

    <input type="text" name="name" id="name" />

    <br /><br />

    <label for="major">Major:</label>

    <input type="text" name="major" id="major" />

    <br /><br />

    <input type="submit" name="Submit" value="Submit" />

  </p>

</form>

</body>

</html>

 

 

Now just an aside:

This can be used to check and see what is available in our version of PHP.

 

Phpinfochk.php

<?php

phpinfo();

?>