Lost Password?

  #1 (permalink)  
Old 12-15-2006, 08:10 PM
John's Avatar   
John John is offline
Co-Administrator
 
Join Date: Jul 2006
Age: 19
Posts: 2,736
Last Blog:
Passwords
Rep Power: 20
John has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud of
Send a message via AIM to John
Default PHP:Tutorial - Email Verification

The first thing we are going to do is create a new php file and starte a new function that accepts an email parameter.

PHP Code:
<?php
function EmailValidation($email) { 
    
}
?>
Next thing we want to do is remove any unnecessary characters from the email address to prevent any melicious attacks. We do that by using the htmlspecialchars(), stripslashes(), and strip_tags() functions.

PHP Code:
<?php
function EmailValidation($email) { 
    
$email htmlspecialchars(stripslashes(strip_tags($email))); //parse unnecessary characters to prevent exploits
    
}
?>
Next we are going to use regex in conjunction with the eregi (which is the same thing as the ereg function except eregi ignores case) to verify that the email address is in proper format. For example: name@domain.extention

PHP Code:
<?php
function EmailValidation($email) { 
    
$email htmlspecialchars(stripslashes(strip_tags($email))); //parse unnecessary characters to prevent exploits
    
    
if ( eregi '[a-z||0-9]@[a-z||0-9].[a-z]'$email ) ) { //checks to make sure the email address is in a valid format
    
        
}
}
?>
Now we are going to explode the email address at the "@" sign so the parts of the email address are seperated into an array. That way we can use the domain name to make a connection to the server to test if the doman name is valid. To connect to the server we are going to use the fsockopen() function, and if the connection is established we are going to return true.

PHP Code:
<?php
function EmailValidation($email) { 
    
$email htmlspecialchars(stripslashes(strip_tags($email))); //parse unnecessary characters to prevent exploits
    
    
if ( eregi '[a-z||0-9]@[a-z||0-9].[a-z]'$email ) ) { //checks to make sure the email address is in a valid format
    
$domain explode"@"$email ); //get the domain name
        
        
if ( @fsockopen ($domain[1],80,$errno,$errstr,3)) {
            
//if the connection can be established, the email address is probabley valid
            
return true;
        }
}
?>
Now all we need to do is write the code for the even that the email address is not in a valid format or the connection cannot be established. We will set the return types to be false in these cases.

PHP Code:
<?php
function EmailValidation($email) { 
    
$email htmlspecialchars(stripslashes(strip_tags($email))); //parse unnecessary characters to prevent exploits
    
    
if ( eregi '[a-z||0-9]@[a-z||0-9].[a-z]'$email ) ) { //checks to make sure the email address is in a valid format
    
$domain explode"@"$email ); //get the domain name
        
        
if ( @fsockopen ($domain[1],80,$errno,$errstr,3)) {
            
//if the connection can be established, the email address is probabley valid
            
return true;
            
/*
            
            GENERATE A VERIFICATION EMAIL
            
            */
            
        
} else {
            return 
false//if a connection cannot be established return false
        
}
    
    } else {
        return 
false//if email address is an invalid format return false
    
}
}
?>
Now that we have a function to verify the email address all you need to do is make a simple form like this

PHP Code:
<?php
function EmailForm(){
    if(empty(
$_POST['email'])){
        echo 
"<form action=".$_SERVER['PHP_SELF']." method='post'>
        <table border='0'>
        <tr>
        <td>Email</td>
        <td><input name='email' type='text' id='email' /></td>
        </tr>
        <tr>
        <td>&nbsp;</td>
        <td><input type='submit' name='Submit' value='Validate' /></td>
        </tr>
        </table>
        </form>"
;    
    } elseif(isset(
$_POST['email'])) {
    
        if(
EmailValidation($_POST['email'])) {
            echo 
"An email has been sent to you. Please follow the instructions to activate your account.";
        } else {
            echo 
"Your email address appears to be invalid. Please try again.";
        }
    
    } else {
        
        echo 
"An error has occured, please contact the administrator.";
    
    }
}
?>
Now add these two functions to the same file and call the EmailForm function and your good to go.

PHP Code:
<?php

function EmailValidation($email) { 
    
$email htmlspecialchars(stripslashes(strip_tags($email))); //parse unnecessary characters to prevent exploits
    
    
if ( eregi '[a-z||0-9]@[a-z||0-9].[a-z]'$email ) ) { //checks to make sure the email address is in a valid format
    
$domain explode"@"$email ); //get the domain name
        
        
if ( @fsockopen ($domain[1],80,$errno,$errstr,3)) {
            
//if the connection can be established, the email address is probabley valid
            
return true;
            
/*
            
            GENERATE A VERIFICATION EMAIL
            
            */
            
        
} else {
            return 
false//if a connection cannot be established return false
        
}
    
    } else {
        return 
false//if email address is an invalid format return false
    
}
}

function 
EmailForm(){
    if(empty(
$_POST['email'])){
        echo 
"<form action=".$_SERVER['PHP_SELF']." method='post'>
        <table border='0'>
        <tr>
        <td>Email</td>
        <td><input name='email' type='text' id='email' /></td>
        </tr>
        <tr>
        <td>&nbsp;</td>
        <td><input type='submit' name='Submit' value='Validate' /></td>
        </tr>
        </table>
        </form>"
;    
    } elseif(isset(
$_POST['email'])) {
    
        if(
EmailValidation($_POST['email'])) {
            echo 
"An email has been sent to you. Please follow the instructions to activate your account.";
        } else {
            echo 
"Your email address appears to be invalid. Please try again.";
        }
    
    } else {
        
        echo 
"An error has occured, please contact the administrator.";
    
    }
}

EmailForm();

?>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 12-15-2006, 08:57 PM
Jordan's Avatar   
Jordan Jordan is offline
Administrator
 
Join Date: Nov 2005
Location: Hendersonville, NC
Age: 26
Posts: 5,973
Last Blog:
SAP, ERP and EDI
Rep Power: 20
Jordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud of
Send a message via ICQ to Jordan Send a message via AIM to Jordan Send a message via MSN to Jordan
Default

Very nice tutorial! Thank you!
__________________
CodeCall Blog | CodeCall Wiki | Shareware Site | Linux Forum | Write a Blog
Don't hesitate to ask any questions that you have! Check out our ASCII Calculator!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 12-30-2006, 02:25 AM
xtraze xtraze is offline
Programming God
 
Join Date: Dec 2006
Location: Sri lanka
Posts: 921
Rep Power: 0
xtraze is on a distinguished road
Send a message via MSN to xtraze Send a message via Skype™ to xtraze
Default

wow, not a simple for I say, but I may just copy/paste and I will Edit thing to suit my needs.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 09-19-2007, 12:19 PM
matthewk matthewk is offline
Newbie
 
Join Date: Sep 2007
Posts: 2
Rep Power: 0
matthewk is on a distinguished road
Default

I think preg_match is quicker from what I've read. Also, what exploits could occur which require the necessity of htmlspecialchars?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Email form v2 Jaan PHP Tutorials 7 07-25-2008 12:44 PM
Dynamic Email insertion in HTML Gibster HTML Programming 5 07-17-2007 04:22 PM
WARNING: New e-gold phishing email andrew Business and Legal 3 04-27-2007 11:54 AM
Email Verification Lop PHP Forum 7 12-22-2006 10:32 PM
Catch All Email Account Nightracer Computer Software/OS 4 11-17-2006 05:56 PM


All times are GMT -5. The time now is 01:18 PM.

Contest Stats

John ........ 223.00000
dargueta ........ 168.00000
Xav ........ 164.00000
LogicKills ........ 20.00000
sam ........ 20.00000
gaylo565 ........ 18.00000
|pH| ........ 15.00000
WingedPanther ........ 15.00000
Johnnyboy ........ 3.00000
navghost ........ 1.00000

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 67%

Ads