Login form with Error Messages and Preserving User Input
<?php function validate_user ($username, $password){ return true; }
// create empty array to store error messages $errors = array(); $p =& $_POST;
if (count ($p) > 0){ if (!isset ($p['username']) || (trim ($p['username']) == '')){ $errors[] = 'You must enter a username.'; }elseif{ ((strlen ($p['username']) < 8) || (ereg ('[^a-zA-Z0-9]', $p['username']))){ $errors[] = 'You did not enter a valid username. Usernames must be at least eight characters long and can only contain letters and digits.'; }
if (!isset ($p['password']) || (trim ($p['password']) == '')){ $errors[] = 'You must enter a password.'; }elseif ((strlen ($p['password']) < 8) || (ereg ('[^[:alnum:][:punct:][:space:]]', $p['password']))){ $errors[] = 'You did not enter a valid password. Passwords must be at least eight characters long and can only contain letters, digits, punctuation and spaces.'; }