FRIHOSTFORUMSFAQTOSBLOGSDIRECTORY
You are invited to Log in or Register a Frihost Account!

verifing the email

 


AlexandruDan
I try to verify if the email address as something@something.domain
for now i tryed this:

if (!eregi('^[[:alpha:]]+$', $email)){
echo "mail valid <br />";
}
else{
header ("Location: logare.php");
echo "mail invalid <br />";
exit;
}

but is not enough, becouse it doesn't check the strings after @, and i didn't figure it out how to do it.

*echo "mail invalid is not displayed - don't know why, and if i switch them (first echo, then header...), it returns an error.
AftershockVibe
Because this is such a common thing to do in PHP, thousands of people have done it before and examples are all over the place. Like on the comments for the PHP eregi Reference page (http://uk2.php.net/eregi)

Code:
if(!eregi("^[[:alnum:]][a-z0-9_.-]*@[a-z0-9.-]+\.[a-z]{2,4}$", $_POST['EmailAddress'])) {
     echo "<p>Not a valid email address</p>\n";
}


You might want to have a look at regular expressions and how they work to understand what it's doing though.
AlexandruDan
Thanks, it works.
Still i have this problem:
when the email address has a wrong format, i can't return the user in the log in formular and give an error message.
I have tried with:
if(!eregi("^[[:alnum:]][a-z0-9_.-]*@[a-z0-9.-]+\.[a-z]{2,4}$", $email)) {
header ("Location: logare.php");
echo "<p>Not a valid email address</p>\n";
exit;
}

and with
if(!eregi("^[[:alnum:]][a-z0-9_.-]*@[a-z0-9.-]+\.[a-z]{2,4}$", $email)) {
echo "<p>Not a valid email address</p>\n";
header ("Location: logare.php");
exit;
}

but no succes at all.
badai
here, script to really validate e-mail

Code:
function getmxrr($hostname, &$mxhosts)
{
   $mxhosts = array();
   exec('nslookup -type=mx '.$hostname, $result_arr);
   foreach($result_arr as $line)
   {
     if (preg_match("/.*mail exchanger = (.*)/", $line, $matches))
         $mxhosts[] = $matches[1];
   }
   return( count($mxhosts) > 0 );
}

function ValidateMail($Email) {
   global $HTTP_HOST;
   $result = array();
   if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $Email)) {

      $result[0]=false;
      $result[1]="The e-mail $Email is not properly formatted.";
      return $result;
   }
   list ( $Username, $Domain ) = split ("@",$Email);

      if (getmxrr($Domain, $MXHost))  {

           $ConnectAddress = $MXHost[0];
       } else {

           $ConnectAddress = $Domain;

       }
   error_reporting(0);
   $Connect = fsockopen ( $ConnectAddress, 25 );
   error_reporting(2047);

       if ($Connect) {

           if (ereg("^220", $Out = fgets($Connect, 1024))) {
   
              fputs ($Connect, "HELO $HTTP_HOST\r\n");
              $Out = fgets ( $Connect, 1024 );
              fputs ($Connect, "MAIL FROM: <{$Email}>\r\n");
              $From = fgets ( $Connect, 1024 );
              fputs ($Connect, "RCPT TO: <{$Email}>\r\n");
              $To = fgets ($Connect, 1024);
              fputs ($Connect, "QUIT\r\n");
              fclose($Connect);
              if (!ereg ("^250", $From) ||!ereg ( "^250", $To )) {
         $result[0]=false;
              $result[1]="Server $ConnectAddress rejected address $Email. Please check your spelling";
              return $result;

              }
           } else {

               $result[0] = false;
               $result[1] = "No response from server $ConnectAddress. Can not validate e-mail address. Please try again later.";
               return $result;
           }

   }  else {

           $result[0]=false;
           $result[1]="Can not connect to E-Mail server $ConnectAddress.  Can not validate e-mail address. Please try again later or check your spelling.";
           return $result;
   }
   $result[0]=true;
   $result[1]="$Email appears to be valid.";
   return $result;
}


you just need to call function ValidateMail($email), where $email is the e-mail to validate, and it will return array with 2 value, first value true or false for valid or invalid e-mail, second value, if true, just that "$Email appears to be valid" string, or if false, the reason why cannot validate.
AlexandruDan
Thanks, it works.
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2007 Frihost, forums powered by phpBB.