Source of: /ADI/more_sessions-withCookies/input_validation.php

<?php
session_start
();
$_SESSION['firstname'] = $_POST['firstname'];
$_SESSION['lastname'] = $_POST['lastname'];
$_SESSION['phone'] = $_POST['phone'];
$_SESSION['try']++;

//set-up an empty array to hold errors
$errors=array();

//validate data
if (empty($_POST['firstname']))
 
$errors['firstname'] = 'the first name field can not be blank';

if (empty(
$_POST['lastname']))
 
$errors['lastname'] = 'the last name field can not be blank';

if (!
ereg('^([0-9]){9}$',$_POST['phone']))
  
$errors['phone'] = 'the phone number must have 9 digits';

//store the errors as a session variable
$_SESSION['errors']  = $errors;

if(!empty(
$errors)) {
 
// there are errors: try again
 
header('Location: entry_form.php');
  exit;
}
else {
 
//there are no errors: life goes on
 //destroy session
  
session_destroy();
 
  
printf("<html>\n");
  
printf("<head>\n");
  
printf("<title>Success</title>\n");
  
printf("</head>\n");
  
printf("<body>\n");
  
printf("<p>Your data is valid and is ready for further processing</p>\n");
  
printf("<p>A sessao terminou. Clique aqui para <a href=\"entry_form.php\">recomecar de novo</a>.</p>\n");
  
printf("</body>\n");
  
printf("</html>\n");

}

?>