"; return false; } else{ return true; } } function val_loc($var, $global_string){ global $global_string; $temp_string = str_replace(" ","",$var); $len = strlen($temp_string); if($len < 2){ $global_string = $global_string."Location field must have at least two characters. Resubmit with valid location.
"; return false; } else{ return true; } } function val_email($email_string, $global_string){ global $global_string; $pattern = "^[a-zA-Z0-9]+@([a-zA-Z0-9]+.)+"; if(!ereg($pattern,$email_string)){ $global_string= $global_string."E-mail address not valid. Resubmit with valid e-mail address.
"; return false; } else{ return true; } } function write_to_file($record){ #print "$record
"; #$filename = "../data/guestbook.txt"; #$file = fopen($filename,"a"); #fwrite($file,$record); #fclose($file); #$file = fopen($filename,'r'); #$data_echo = fgets($file); #print "$data_echo"; #fclose($file); if($file = fopen('../data/guestbook.txt','a')){ if(flock($file,LOCK_EX)){ if(!fwrite($file,$record)){ print"

Unable to write to file.

"; return; } if(!flock($file,LOCK_UN)){ print"

Unable to unlock the file.

"; } }else{ print"

Unable to lock the file.

"; if(!fclose($file)){ print"

Unable to close file.

"; } return; } if(!fclose($file)){ print"

Unable to close file.

"; } }else{ print"

Unable to open file.

"; return; } print "

Thank you for signing the Guestbook.

"; } function display_Guestbook(){ print "Guestbook Entries:
"; #$path_string = substr($_SERVER['PATH_INFO']); #$pathp = explode("/", $path_string); #$filename = '../data/guestbook.txt'; if($file = fopen('../data/guestbook.txt','r')){ print ""; while($dataread = fgets($file)){ $row = explode("|",$dataread); print ""; print ""; print ""; print ""; print ""; print ""; print ""; print ""; } print "
".$row[0]."".$row[1]."".$row[2]."
".$row[3]."
"; if(!fclose($file)){ print"

Unable to close file.

"; } }else{ print"

Unable to open file.

"; return; } } if(array_key_exists('view',$_REQUEST)){ display_Guestbook(); print "

Return to form

"; } else if((array_key_exists('username',$_REQUEST))and(array_key_exists('location',$_REQUEST))and(array_key_exists('E_mail_address',$_REQUEST))){ $uname = $_REQUEST['username']; $temp_name = str_replace(" ","",$uname); $local = $_REQUEST['location']; $temp_local = str_replace(" ","",$local); $email_val = $_REQUEST['E_mail_address']; $user_comments = $_REQUEST['comments']; $global_string = " "; $valid_name = true; $valid_location = true; $valid_email = true; $record = ""; $correction = false; $name_location_pattern = '^[a-zA-Z]+$'; if(!(ereg($name_location_pattern,$temp_name))){ $global_string = $global_string."Name is not valid. Resubmit with valid name.
"; $correction = true; } if(!eregi($name_location_pattern,$temp_local)){ $global_string = $global_string."Location is not valid. Resubmit with valid location.
"; $correction = true; } $valid_name = val_name($uname,$global_string); $valid_location = val_loc($local,$global_string); $valid_email = val_email($email_val, $global_string); if(($correction != false)||($valid_name != true)||($valid_location != true)||($valid_email!=true)){ print "$global_string"; print "
"; print "

Name:

"; print "

Location:

"; print "

E-mail Address:

"; print "

Comments:

"; print "

"; print "


"; print "
"; } else{ $uname = htmlentities($uname); $uname = str_replace("|","",$uname); $local = htmlentities($local); $local = str_replace("|","",$local); $email_val = htmlentities($email_val); $email_val = str_replace("|","",$email_val); $user_comments = htmlentities($user_comments); $user_comments = str_replace("|","",$user_comments); $user_comments = nl2br($user_comments); $user_comments = str_replace("\n","",$user_comments); $user_comments = str_replace("\r","",$user_comments); $record = $uname."|".$local."|".$email_val."|".$user_comments."| "; write_to_file($record); print "
"; print "

"; print "
"; } } else{ print "

Please complete the data fields to register in the guestbook.

"; print "

Name and Location are required, and each must be at least two characters in length. A valid e-mail Address is also required.

"; print<<<_END_HERE_

Name:

Location:

E-mail Address:

Comments:

_END_HERE_; } ?>

Valid XHTML 1.0!