SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2001
    Location
    Tucson, AZ
    Posts
    80
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Email validation with PHP

    Using the code below, how can I validate the email address?

    Code:
    <? include ("header.inc") ?>
    
    <script language="Javascript" type="text/javascript">
    <!--
    function launch(){
    emoticons=window.open("../emoticons.html","emoticons","width=375,height=450,top=0,left=0,resizable=yes,scrollbars=yes,menubar=no,toolbar=no,status=no,location=no")}
    //-->
    </script>	   
    
    <div><!-- This is where the message database is loaded -->
    
    
    
    <!-- End messages -->
    
    
    <!-- This is the form to add messages -->
    
    
    <div><font size="3" class="s4" face="Arial, Helvetica">Add a message to my guest book:</FONT>
    
    <FORM ACTION="guestbook.php" METHOD=POST>
       <div>
    <font size="3" class="s4" face="Arial, Helvetica">
       Name:<BR>
       	<INPUT TYPE=text NAME=name SIZE=30 maxlength=80 style="background-color: #999999; font-weight:bold;  color: #ffffff; border-style: solid; border-bottom:2px solid #666666;  border-right:2px solid #666666; border-top:2px solid #666666; border-left:2px solid #666666"><BR>
       Email:<BR>
       	<INPUT TYPE=text NAME=email SIZE=30 maxlength=80 style="background-color: #999999; font-weight:bold;  color: #ffffff; border-style: solid; border-bottom:2px solid #666666;  border-right:2px solid #666666; border-top:2px solid #666666; border-left:2px solid #666666"><BR>
       Notes:
    
    <BR>
    	<TEXTAREA NAME=comments maxlength="256" ROWS=10 COLS=30 WRAP=virtual style="background-color: #999999; font-weight:bold;  color: #ffffff; border-style: solid; border-bottom:2px solid #666666;  border-right:2px solid #666666; border-top:2px solid #666666; border-left:2px solid #666666"></TEXTAREA>
    <BR><BR>
      </FONT>
    <table>
       <tr>
    	<td><font size="3" class="s4" face="Arial, Helvetica">Check here if you've used HTML tags in any field:</FONT></td>
    	<td><INPUT TYPE=checkbox NAME=html value="1" onFocus="if(this.blur)this.blur()" style="background-color: #999999; font-weight:bold;  color: #ffffff; border-style: solid; border-bottom:2px solid #666666;  border-right:2px solid #666666; border-top:2px solid #666666; border-left:2px solid #666666"><BR></td>
       </tr>
    </table>
    
    
    </div>
       <BR>
       <div><font size="2" class="s3" face="Arial, Helvetica"><INPUT TYPE=submit NAME=gb VALUE="Add Message" onFocus="if(this.blur)this.blur()" style="background-color: #999999; font-weight:bold;  color: #ffffff; border-style: solid; border-bottom:2px solid #666666;  border-right:2px solid #666666; border-top:2px solid #666666; border-left:2px solid #666666">&nbsp;&nbsp;Please press only once: this may take a moment (or two).</FONT>
    </FORM></div>
    
    <BR><BR><a href="javascript:launch()">Emoticon List</a> 
    
    
    <br><br>
    <a href="index.php" onFocus="if(this.blur)this.blur()"><img src="view.gif" width="120" height="42" border="0" alt="Sign"></a>
    </font>
    	   
    <? include ("footer.inc") ?>


  2. #2
    The Hiding One lynlimz's Avatar
    Join Date
    Jul 2000
    Location
    Singapore
    Posts
    2,103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When you submit the form, parsing the values of it, use this function to check if the email structure is valid. doesn't really check if the emails works though. if you want osmething like that, try www.phpbuilder.com - code section.

    PHP Code:
    function checkEmail($value)
    {
      if(
    eregi("^[a-z0-9\._-]+@+[a-z0-9\._-]+\.+[a-z]{2,3}$"$value)) { return true; }
      else { return 
    false; }

    "Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
    -- Albert Einstein

  3. #3
    SitePoint Enthusiast Jack@dwd.com's Avatar
    Join Date
    Dec 2000
    Location
    UK
    Posts
    60
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php
    /*
     * email_validation.php
     *
     * @(#) $Header: /cvsroot/PHPlibrary/email_validation.php,v 1.9 2000/09/25 03:28:15 mlemos Exp $
     *
     */

    class email_validation_class
    {
     var 
    $email_regular_expression="^([-!#\$%&'*+./0-9=?A-Z^_`a-z{|}~ ])+@([-!#\$%&'*+/0-9=?A-Z^_`a-z{|}~ ]+\\.)+[a-zA-Z]{2,4}\$";
     var 
    $timeout=0;
     var 
    $localhost="";
     var 
    $localuser="";
     var 
    $debug=0;

     Function 
    OutputDebug($message)
     {
      echo 
    $message,"\n";
     }

     Function 
    GetLine($connection)
     {
      for(
    $line="";;)
      {
       if(
    feof($connection))
        return(
    0);
       
    $line.=fgets($connection,100);
       
    $length=strlen($line);
       if(
    $length>=2
       
    && substr($line,$length-2,2)=="\r\n")
       {
        
    $line=substr($line,0,$length-2);
        if(
    $this->debug)
         
    $this->OutputDebug("< $line");
        return(
    $line);
       }
      }
     }

     Function 
    PutLine($connection,$line)
     {
      if(
    $this->debug)
       
    $this->OutputDebug("> $line");
      return(
    fputs($connection,"$line\r\n"));
     }

     Function 
    ValidateEmailAddress($email)
     {
      return(
    eregi($this->email_regular_expression,$email)!=0);
     }

     Function 
    ValidateEmailHost($email,$hosts=0)
     {
      if(!
    $this->ValidateEmailAddress($email))
       return(
    0);
      
    $user=strtok($email,"@");
      
    $domain=strtok("");
      if(@
    GetMXRR($domain,&$hosts,&$weights))
      {
       
    $mxhosts=array();
       for(
    $host=0;$host<count($hosts);$host++)
        
    $mxhosts[$weights[$host]]=$hosts[$host];
       
    KSort($mxhosts);
       for(
    Reset($mxhosts),$host=0;$host<count($mxhosts);Next($mxhosts),$host++)
        
    $hosts[$host]=$mxhosts[Key($mxhosts)];
      }
      else
      {
       
    $hosts=array();
       if(
    strcmp(@gethostbyname($domain),$domain)!=0)
        
    $hosts[]=$domain;
      }
      return(
    count($hosts)!=0);
     }

     Function 
    VerifyResultLines($connection,$code)
     {
      while((
    $line=$this->GetLine($connection)))
      {
       if(!
    strcmp(strtok($line," "),$code))
        return(
    1);
       if(
    strcmp(strtok($line,"-"),$code))
        return(
    0);
      }
      return(-
    1);
     }

     Function 
    ValidateEmailBox($email)
     {
      if(!
    $this->ValidateEmailHost($email,&$hosts))
       return(
    0);
      if(!
    strcmp($localhost=$this->localhost,"")
      && !
    strcmp($localhost=getenv("SERVER_NAME"),"")
      && !
    strcmp($localhost=getenv("HOST"),""))
         
    $localhost="localhost";
      if(!
    strcmp($localuser=$this->localuser,"")
      && !
    strcmp($localuser=getenv("USERNAME"),"")
      && !
    strcmp($localuser=getenv("USER"),""))
         
    $localuser="root";
      for(
    $host=0;$host<count($hosts);$host++)
      {
       if(
    $this->debug)
        
    $this->OutputDebug("Connecting to host \"".$hosts[$host]."\"...");
       if((
    $connection=($this->timeout fsockopen($hosts[$host],25,&$errno,&$error,$this->timeout) : fsockopen($hosts[$host],25))))
       {
        if(
    $this->debug)
         
    $this->OutputDebug("Connected.");
        if(
    $this->VerifyResultLines($connection,"220")>0
        
    && $this->PutLine($connection,"HELO $localhost")
        && 
    $this->VerifyResultLines($connection,"250")>0
        
    && $this->PutLine($connection,"MAIL FROM: <$localuser@$localhost>")
        && 
    $this->VerifyResultLines($connection,"250")>0
        
    && $this->PutLine($connection,"RCPT TO: <$email>")
        && (
    $result=$this->VerifyResultLines($connection,"250"))>=0)
        {
         if(
    $this->debug)
          
    $this->OutputDebug("This host states that the address is ".($result "" "not ")."valid.");
         
    fclose($connection);
         if(
    $this->debug)
          
    $this->OutputDebug("Disconnected.");
         return(
    $result);
        }
        if(
    $this->debug)
         
    $this->OutputDebug("Unable to validate the address with this host.");
        
    fclose($connection);
        if(
    $this->debug)
         
    $this->OutputDebug("Disconnected.");
       }
       else
       {
        if(
    $this->debug)
         
    $this->OutputDebug("Failed.");
       }
       
      }
      return(-
    1);
     }
    };

    ?>
    The above is a class (not written by me, its fully GPLed tho, with a wee modification by me on line 59 so it works on a Win32 server) which should help you do a more advanced form of email authentication. The first function, ValidateEmailAddress, does what the person above has suggested. The next function ValidateEmailHost checks that the bit after the @ sign (eg. hotmail.com) is a registered domain name that points to a server. Then ValidateEmailBox actually makes a connection to the server and checks that the individual mailbox exists.

    Personally I would go for the second method (which calls the first within it btw) as the 3rd method won't work for web based email accounts such as hotmail which don't have a POP server.

    Heres an example of how to use it:

    PHP Code:
    <?php
    $validator 
    = new email_validation_class;

    if(!
    $validator->ValidateEmailBox($email))
    {
        echo(
    "Not a valid email address!");
        exit();
    }
    else
    {
        echo(
    "Woo yeah, Email valid");
    }
    ?>
    Enjoy
    Last edited by Jack@dwd.com; Aug 13, 2001 at 10:31.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •