Hi all,
I know very little about PHP, my Dad made a contact form for me some time ago and I have attempted to use it on a new site and it won't to pick up the values from the form fields. I wish I could ask him about it (and about loads of other things ) but he passed away last year.
I have done some research and I managed to run phpinfo on each of the servers and discovered that register_globals is set to off on the new server. Am I right in assuming that this has something to do with the problem I have?
If so, is there anyone out there who can tell me what do I need to do to my code to get it to work on this server, and indeed if I have to use it on PHP6 in the future?
The code is below.
<?php
function field_validator($field_descr, $field_data, $field_type, $min_length="", $max_length="", $field_required=1) {
# array for storing error messages
global $messages;
# first, if no data and field is not required, just return now:
if(!$field_data && !$field_required){ return; }
# initialize a flag variable - used to flag whether data is valid or not
$field_ok=false;
# a hash array of "types of data":
$data_types=array(
"email"=>"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$",
"digit"=>"^[0-9]$",
"number"=>"^[0-9]+$",
"telephone_number"=>"^[ +0-9]+$",
"alpha"=>"^[a-zA-Z]+$",
"alpha_space"=>"^[a-zA-Z ]+$",
"alphanumeric"=>"^[a-zA-Z0-9]+$",
"alphanumeric_space"=>"^[ a-zA-Z0-9\.\,\"\']+$",
"string"=>""
);
# check for required fields
if ($field_required && empty($field_data))
{
$messages[] = "$field_descr is a required field.";
return;
}
# if field type is a string, no need to check regexp:
if ($field_type == "string")
{
$field_ok = true;
}
else
{
# Check the field data against the regexp pattern:
$field_ok = ereg($data_types[$field_type], $field_data);
}
# if field data is bad, add message:
if (!$field_ok)
{
$messages[] = "Please enter a valid $field_descr.";
return;
}
# field data min length checking:
if ($field_ok && $min_length)
{
if (strlen($field_data) < $min_length)
{
$messages[] = "$field_descr is invalid, it should be at least $min_length character(s).";
return;
}
}
# field data max length checking:
if ($field_ok && $max_length)
{
if (strlen($field_data) > $max_length)
{
$messages[] = "$field_descr is invalid, it should be less than $max_length characters.";
return;
}
}
}
// function to remove the strings added by word wrapping on the server
function unwrap ($unwrapped_str)
{
$wrapped1 = str_replace ("\r\n", " ", $unwrapped_str);
$wrapped2 = str_replace ("<BR>", " ", $wrapped1) ;
$wrapped = str_replace ("<br>", " ", $wrapped2) ;
return ($wrapped) ;
}
$domain = "******.com" ;
$reply_to = "enquiry@$domain" ;
$from = $reply_to ;
$subject = "Submission from $domain Website" ;
$subject2 = "Acknowledgement from $domain" ;
$msg = unwrap($msg) ;
$address = ucwords(strtolower(unwrap($address))) ;
$sender_name = ucwords(strtolower($forename." ".$surname)) ;
$c_mail_msg = stripslashes($msg) ;
//validations
field_validator("Email Address", $email, "email", 5, 64, 1);
field_validator("Telephone Number", $phone, "telephone_number", 0, 20, 0);
field_validator("First Name", $forename, "alpha_space", 1, 40, 1);
field_validator("Last Name", $surname, "alpha_space", 3, 40, 1);
field_validator("Address", $address, "alphanumeric_space", 0, 255, 0);
field_validator("Message", $msg, "alphanumeric_space", 0, 2048, 0);
# If any errors were detected they will be stacked in the $messages array.
if($messages)
{
# messages exist, foreach message, print them out:
print("<b>There were problems with the information that you entered: </b>\n<ul>\n");
foreach($messages as $err_msg)
{
print("<li>$err_msg</li>\n");
}
print("</ul>Please click your browser's back button to correct this.\n");
}
else
{
# no errors! let the user know the data was accepted
# (this is where the data would be processed in a real application)
//print "<b>Thankyou. The data you entered was of the correct format</b>\n";
$to = $reply_to ;
//$to = "enquiry@******.com";
$msg = "The following message was transmitted to the Website: \n\n";
$msg = "Message From: $sender_name \n" ;
$msg .= "Email: $email \n" ;
$msg .= "Telephone: $phone \n" ;
$msg .= "Address: $address \n\n";
$msg .= $c_mail_msg ;
$msg .= "\n" ;
$msg1 = "The following message received at www.$domain: \n\n";
$msg1 .= $msg ;
$msg2 = "Thank you for registering at www.$domain\n" ;
$msg2 .= "The information you supplied is detailed below:\n\n" ;
$msg2 .= $msg ;
$as_from = "webmaster@$SERVER_NAME" ;
$headers = "From: Webmaster <$as_from> \r\n" ;
$headers .= "Reply-To: $as_from \r\n" ;
$headers .= "Return-Path: $as_from \r\n" ;
$headers .= "X-Mailer: PHP/". phpversion() ;
$rc=mail($to, $subject, $msg1, $headers);
$rc=mail($email, $subject2, $msg2, $headers);
echo "<p align=\"left\" class=\"normaltext\">Thank you for your input. </P>";
echo "<hr> ";
echo "<p align=\"left\" class=\"normaltext\"><B>From:</B> $sender_name</p>";
echo "<p align=\"left\" class=\"normaltext\"><B>email:</B> $email</p>" ;
echo "<p align=\"left\" class=\"normaltext\"><B>Message:</B> $c_mail_msg</p>" ;
echo "<hr>";
echo "<p align=\"left\" class=\"normaltext\">Your message has been transmitted to Richard A Woodman.</P>";
echo "<p align=\"left\" class=\"normaltext\">Return to <a href=\"/index.htm\"><u>Home</u></a> Page</P> ";
}
//}
?>
Thanks for reading my post.
Lisa