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

    I suspect that your guess is correct. If register_globals is turned off, then you must reference your form variables via the $POST or $GET array as applicable (which also works if it is on). So if you have a form field named "email" and your form uses the post method, you would need to access $_POST['name'] instead of just $name.

    For a "quick fix", you could use the [man]import_request_variables/man function at the start of your script, though that could potentially inflict the same problems that register_globals does (really only a problem with poorly written code).

      Thanks so much for your reply NogDog. I am really glad that I might have guessed the reason for the problem. However, :as I don't know anything about PHP, I am a bit clueless as to how or where to reference my form variables as you suggest. Is there any chance you can be a little more explicit for me?
      😕

      Lisa

        In your code the: $msg, $address, $forename, $surname, $email, $phone "words" (variables)
        should replaced into $POST["msg"] , $POST["address"] , $_POST["forename"] , ect...
        BUT!
        You must not change them evrywhere in your whole program. But its hard to explain if you haven't got the php basics.

        This is a method how you reach the variables from your form.

        before this line,

         $domain      = "******.com"                                ;   

        copy this php code:

        /* NEW LINES*/
        function un($var);
        {
           return ( (isset($_POST[$var])) ? $_POST[$var] : NULL  ) ;
        }
        
        $phone=un("phone");
        $address=un("address");
        $forename=un("forename");
        $surname=un("surname");
        $email=un("email");
        $phone=un("phone");
        $msg=un("msg");
        /* END OF NEW LINES*/ 
        

        these new lines will set those variables which was "missing" becouse of the new server version.

        Might this helped.

        jjozsi.

          jjozsi

          This is absolutely fantastic! Many thanks.

          By the way, I had to remove the semi-colon after

          function un($var)

          to get it to work.

          Thanks SO much.
          Lisa

            Your Welcome,

            And don't forget to set this thread as RESOLVED

            Hello, jjozsi.

              Write a Reply...