Physics & Computer Science > CP363 : Home > Implementation Information
Version: 2020-02-06 11:02

CP363 : PHP Sessions and Redirects

choices.php

<html>
  <head>
    <title>choice</title>
  </head>
  <body>
    <h1>Choose a Link</h1>

    <form action="jump.php" method="post">
      <input name="stuff" type="hidden" value="junk" />
      <select name="url">
        <option value="first.php">First Page</option>
        <option value="second.php">Second Page</option>
      </select>
      <input type="submit" value="Go">
    </form>

  </body>
</html>

jump.php

<?php
// All other processing code here.
// *No* printing to browser before 'header' function.

session_start();

if( $_SERVER['HTTP_REFERER'] == 'http://bohr.wlu.ca/dbrown/test/choice.php' ) {
  $_SESSION['post'] = $_POST;
  header( "Location: {$_POST['url']}" );
} else {
  header( 'Location: nasty.htm' );
}
?>

first.php

<?php
session_start();
?>

<html>
  <head>
    <title>First</title>
  </head>
  <body>
    <h1>First</h1>

    <p>$_SESSION['post']:</p>
<?php
print( '<pre>' );
print_r( $_SESSION['post'] );
print( '</pre>' );
?>

  </body>
</html>

second.php

<html>
  <head>
    <title>Second</title>
  </head>
  <body>
    <h1>Second</h1>

<?php		
if(isset($_SESSION['view'] )) {
  $_SESSION['views'] = $_SESSION['views'] + 1;
} else {
  $_SESSION['views'] = 1;
}
print( "Views= {$_SESSION['views']}" );
?>

  </body>
</html>