Function eregi() is deprecated and Overall Check

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
baron0811
Forum Newbie
Posts: 2
Joined: Sun Mar 06, 2011 10:57 pm

Function eregi() is deprecated and Overall Check

Post by baron0811 »

First, I'm very fresh in PHP coding. I will be thankful if someone willing to spend some time to guide me a little for starting.
This is an Enquiry Form script.

Code: Select all

<form method="post" action="enquiry.html">

              <fieldset><legend>&nbsp;CONTACT DETAILS&nbsp;</legend>

                <p><label for="first_name" class="left">First name&nbsp;<font color="red">*</font></label>

                   <input type="text" name="first_name" id="first_name" class="field" value="" tabindex="1" /></p>

                <p><label for="last_name" class="left">Last name&nbsp;<font color="red">*</font></label>

                   <input type="text" name="last_name" id="last_name" class="field" value="" tabindex="1" /></p>

                <p><label for="street" class="left">Street&nbsp;<font color="red">*</font></label>

                   <input type="text" name="street" id="street" class="field" value="" tabindex="1" /></p>

                <p><label for="postalcode" class="left">Postal code&nbsp;<font color="red">*</font></label>

                   <input type="text" name="postalcode" id="postalcode" class="field" value="" tabindex="1"/></p>

                <p><label for="city" class="left">City&nbsp;<font color="red">*</font></label>

                   <input type="text" name="city" id="city" class="field" value="" tabindex="1" /></p>

                <p><label for="country" class="left">Country&nbsp;<font color="red">*</font></label>



                   <input type="text" name="country" id="country" class="field" value="" tabindex="1" /></p>

 <p><label for="company" class="left">Company</label>



                   <input type="text" name="company" id="company" class="field" value="" tabindex="1" /></p>
                   
                <p>
                  <label for="=phone" class="left">Phone&nbsp;<font color="red">*</font><br>
               </label>

                  <input type="text" name="phone" id="phone" class="field" value="" tabindex="2" /></p>

                <p>
                  <label for="fax" class="left">Fax</label>

                   <input type="text" name="fax" id="fax" class="field" value="" tabindex="2" /></p>
                   
                <p><label for="email" class="left">E-mail&nbsp;<font color="red">*</font></label>

                   <input type="text" name="email" id="email" class="field" value="" tabindex="2" /></p>

               
              </fieldset>

              <fieldset><legend>&nbsp;MESSAGE DETAILS&nbsp;</legend>

                <p><label for="subject" class="left">Subject&nbsp;<font color="red">*</font></label>

                   <input type="text" name="subject" id="subject" class="field" value="" tabindex="4" /></p>

                <p><label for="message" class="left">Message&nbsp;<font color="red">*</font></label>

                   <textarea name="message" id="message" cols="45" rows="10" tabindex="5"></textarea></p>

                <p><input type="reset" name="Submit2" class="button" value="Reset">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="submit" name="submit" id="submit" class="button" value="Send message" tabindex="6" /></p>

              </fieldset>

            </form>

Code: Select all

<?php
if(isset($_POST['email'])) {
     
    // EDIT THE 2 LINES BELOW AS REQUIRED
    $email_to = "xxxxx@hotmail.com";
    $email_subject = "$subject";
     
     
    function died($error) {
        // your error code can go here
        echo "We are very sorry, but there were error(s) found with the form you submitted. ";
        echo "These errors appear below.<br /><br />";
        echo $error."<br /><br />";
        echo "Please go back and fix these errors.<br /><br />";
        die();
    }
     
    // validation expected data exists
    if(!isset($_POST['first_name']) ||
        !isset($_POST['last_name']) ||
	!isset($_POST['street']) ||
	!isset($_POST['postalcode']) ||
	!isset($_POST['city']) ||
	!isset($_POST['country']) ||
        !isset($_POST['phone']) ||
	!isset($_POST['email']) ||
	!isset($_POST['subject']) ||
        !isset($_POST['message'])) {
        died('We are sorry, but there appears to be a problem with the form you submitted.');       
    }
     
    $first_name = $_POST['first_name'];
    $last_name = $_POST['last_name'];
    $street = $_POST['street'];
    $postalcode = $_POST['postalcode'];
    $city = $_POST['city'];
    $country = $_POST['country'];
    $company = $_POST['company'];
    $email_from = $_POST['email'];
    $phone = $_POST['phone'];
    $fax = $_POST['fax'];
    $subject = $_POST['subject'];
    $message = $_POST['message'];
    
     
    $error_message = "";
    $email_exp = "^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$";
  if(!eregi($email_exp,$email_from)) {
    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
  }
    $string_exp = "^[a-z .'-]+$";
  if(!eregi($string_exp,$first_name)) {
    $error_message .= 'The First Name you entered does not appear to be valid.<br />';
  }
  if(!eregi($string_exp,$last_name)) {
    $error_message .= 'The Last Name you entered does not appear to be valid.<br />';
  }
    $street_exp = "^[a-z .0-9-]$";
  if(!eregi($street_exp,$street)) {
    $error_message .= 'The Street name you entered does not appear to be valid.<br />';
  }
    $postalcode_exp = "^[0-9-]$";
  if(!eregi($postalcode,$postalcode)) {
    $error_message .= 'The Postal Code you entered does not appear to be valid.<br />';
  }
    $city_exp = "^[a-z '-]$";
  if(!eregi($city_exp,$city)) {
    $error_message .= 'The City name you entered does not appear to be valid.<br />';
  }
    $country_exp = "^[a-z '-]$";
  if(!eregi($country_exp,$country)) {
    $error_message .= 'The Country name you entered does not appear to be valid.<br />';
  }
    $fax_exp = "^[0-9-]$";
  if(!eregi($fax_exp,$fax)) {
    $error_message .= 'The Fax number you entered does not appear to be valid.<br />';
  }
    $phone_exp = "^[0-9-]$";
  if(!eregi($phone_exp,$phone)) {
    $error_message .= 'The Phone number you entered does not appear to be valid.<br />';
  }
  if(strlen($message) < 2) {
    $error_message .= 'The Message you entered does not appear to be valid.<br />';
  }
  if(strlen($error_message) > 0) {
    died($error_message);
  }
    $email_message = "Form details below.\n\n";
     
    function clean_string($string) {
      $bad = array("content-type","bcc:","to:","cc:","href");
      return str_replace($bad,"",$string);
    }
     
    $email_message .= "First Name: ".clean_string($first_name)."\n";
    $email_message .= "Last Name: ".clean_string($last_name)."\n";
    $email_message .= "Street: ".clean_string($street)."\n";
    $email_message .= "Postal Code: ".clean_string($postalcode)."\n";
    $email_message .= "City: ".clean_string($city)."\n";
    $email_message .= "Country: ".clean_string($country)."\n";
    $email_message .= "Company: ".clean_string($company)."\n";
    $email_message .= "Email: ".clean_string($email_from)."\n";
    $email_message .= "Phone: ".clean_string($phone)."\n";
    $email_message .= "Fax: ".clean_string($fax)."\n";
    $email_message .= "Subject: ".clean_string($subject)."\n";
    $email_message .= "Message: ".clean_string($message)."\n";
     
     
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);  
?>
 
 
Thank you for contacting us. We will be in touch with you very soon.
 
<?php
}
?>
To be honest, I just copied codes and tweaked it around, but i wish to learn from my mistakes.
Anybody would like to give me a hand on this? :D

Thanks in advance, everybody.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Function eregi() is deprecated and Overall Check

Post by Christopher »

To convert from ereg to preg just you just need to rename the functions and add /'s around you regular expressions.

Code: Select all

    $email_exp = "/^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/";
  if(!preg_match($email_exp,$email_from)) {
(#10850)
baron0811
Forum Newbie
Posts: 2
Joined: Sun Mar 06, 2011 10:57 pm

Re: Function eregi() is deprecated and Overall Check

Post by baron0811 »

Thanks a lot! :D

By the way, you don't find any problems with the codes other than that? :)
Post Reply