0

On my website i have a contact us form which used to work fine until i upgraded to php 7. Now when i fill the form it returns as invalid email and the logs doesn't show any error.

Earlier it was showing error as

PHP Fatal error:  Uncaught Error: Call to undefined function eregi() in /app/index.php:27

This is line 27:

 if (!empty($email) && !preg_match("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)){
        $err[] = "Invalid email<br>";
    }

So i changed eregi to preg_match but now it just doesn't work.

<?php
$err=array();

if(count($_POST) > 0)
{

    $name = isset($_REQUEST["name"]) ? trim($_REQUEST["name"]) : "";
    $address = isset($_REQUEST["subject"]) ? trim($_REQUEST["subject"]) : "";
    $country = isset($_REQUEST["country"]) ? trim($_REQUEST["country"]) : "";
    $email = isset($_REQUEST["email"]) ? trim($_REQUEST["email"]) : "";
    $contact_no = isset($_REQUEST["contact_no"]) ? trim($_REQUEST["contact_no"]) : "";
    $budget = isset($_REQUEST["budget"]) ? trim($_REQUEST["budget"]) : "";
    $com_name = isset($_REQUEST["com_name"]) ? trim($_REQUEST["com_name"]) : "";
    $message_text = isset($_REQUEST["message_text"]) ? trim(addslashes($_REQUEST["message_text"])) : "";
    $message_text=htmlentities($message_text,ENT_QUOTES);


    $ipaddress = $_SERVER['REMOTE_ADDR'];
    $date = date("F j, Y, g:i a"); 


    if(empty($name)) $err[]="You must enter your name,";
    if(empty($email)) $err[]="proper email address,";

    if (!empty($email) && !preg_match("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)){
        $err[] = "Invalid email<br>";
    }
    if(empty($contact_no)) $err[]="proper contact no";

    if(!empty($contact_no) && !(is_numeric($contact_no)) )
    $err[]="Contact No must be in Number<br>";
That dude
  • 379
  • 1
  • 4
  • 17
  • Voted to close as `why isn't this code working?` You have dumped a lot of code, but given almost no explanation about what the code is supposed to be doing, or what the actual problem is. Please add a problem statement to your question. – Tim Biegeleisen Jun 01 '19 at 08:17
  • 1
    Your regex seems fine, but you have to use delimiters like for example a forward slash `/` at the start and the end of the pattern `preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/"` – The fourth bird Jun 01 '19 at 08:22
  • Validating email addresses is a mugs game. Your regular expression will fail valid addresses in any domain with a TLD longer than 3 characters. Take a look at [this page](https://data.iana.org/TLD/tlds-alpha-by-domain.txt) maintained by IANA to see just how many there are. –  Jun 01 '19 at 08:47
  • You switch from eregi which is case insensitive. You can optimize your regex to something like this pattern: `'/^[\w-]+(?:\.[\w-]+)*@[\w-]+(?:\.[a-z0-9-]+)*\.[a-z]{1,12}$/i'` with [`i` flag](https://www.php.net/manual/en/reference.pcre.pattern.modifiers.php) (*pcre caseless*). There's many new tlds out there and I won't restrict at all to end in `\.[a-z]{2,3}$`. Actually I would use at most `/^\S+@\S+$/`. See [demo of your working line 27](https://3v4l.org/tPVQ7). – bobble bubble Jun 01 '19 at 09:11

0 Answers0