'[select one]','rant'=>'rant','praise'=>'praise','question'=>'question','observation' =>'observation'); $defaults = array('subject'=>'','comments'=>'','name'=>'','email'=>''); $TITLE = "Guestbook Demo"; include("simple_head.php"); function display_comments() { $fp = @fopen(COMMENT_FILE,'r') ; if (! $fp or !flock($fp,LOCK_SH)) return false; while (!feof($fp)) { echo nl2br(htmlentities(fgets($fp))); } flock($fp,LOCK_UN); fclose($fp); return true; } function save_comment($data) { if (filesize(COMMENT_FILE) > 10240) { $mode = 'w'; echo "(file getting too large, purging...sorry!)"; } else { $mode = 'a'; } if (!$fp = @fopen(COMMENT_FILE,$mode)) return false; if (! flock($fp,LOCK_EX)) return false; $data['comments'] .= "\n[$data[subject] from $data[name] posted " . date('d-M-Y h:i a') . "]\n\n" ; $result = @fputs($fp,strip_tags($data['comments'])); flock($fp,LOCK_UN); fclose($fp); return true; } function validate_form(&$data) { $errors = array(); $data['email']=trim($data['email']); $data['comments']=trim($data['comments']); $data['name']=trim($data['name']); if (empty($data['email'])) { $errors[] = "Email address is required"; } elseif (! preg_match( '/^[^@\s]+@([-a-z0-9]+\.)+[a-z]{2,}$/i',$data['email'])) { $errors[] = 'Email address appears to be invalid'; } if (empty($data['comments'])) { $errors[] = 'The comments field is required'; } if (strlen($data['comments']) > 250 ) { $errors[] = 'The comments field exceeds the maximum length'; } $data['comments'] = strip_tags($data['comments']); $data['name'] = strip_tags($data['name']); if (! strlen($data['name'])) { $data['name'] = 'anonymous'; } if (strlen($data['name']) > 36) { // truncate it $data['name'] = substr($data['name'],0,36); $errors[] = 'Your name exceeds the max field length' ; } if (empty($data['subject'])) { $errors[] = 'Subject field is required'; } if (! array_key_exists($data['subject'],$GLOBALS['subject_options'])) { $errors[] = 'Invalid subject field'; } return $errors; } /* courtesy of "Learning PHP 5" by David Sklar */ function input_select($element_name, $selected, $options, $multiple = false) { // print out the tag print ''; // set up the list of things to be selected $selected_options = array(); if ($multiple) { foreach ($selected[$element_name] as $val) { $selected_options[$val] = true; } } else { $selected_options[ $selected[$element_name] ] = true; } // print out the tags foreach ($options as $option => $label) { print '' . htmlentities($label) . ''; } print ''; } function display_form($defaults) { global $subject_options; ?> Subject Your email Your name Comments } if ($_POST) { $errors = validate_form($_POST); if ($errors) { echo "Sorry, we are unable to process your submission because:", implode('',$errors), "", "Please correct your form below and re-submit it"; display_form($_POST); } else { save_comment($_POST) or die("Sorry, our guestbook is out of order. Please try again later"); display_comments() or die("Sorry, our guestbook is out of order. Please try again later"); display_form($defaults); } } else { display_comments() or die("Sorry, our guestbook is out of order. Please try again later");; display_form($defaults); } ?>
Sorry, we are unable to process your submission because:
Please correct your form below and re-submit it