IT Community - Software Programming, Web Development and Technical Support

PHP:Tutorial - Email Verification

This is a discussion on PHP:Tutorial - Email Verification within the PHP Programming forums, part of the Web Development category; The first thing we are going to do is create a new php file and starte a new function that ...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Web Development > PHP Programming

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 02-24-2007, 12:45 AM
pranky pranky is offline
D-Web Programmer
 
Join Date: Feb 2007
Posts: 51
pranky is on a distinguished road
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.
Code:
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.
Code:
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
Code:
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.
Code:
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.
Code:
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
Code:
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.
Code:
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();

?>
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 10-04-2008, 10:54 PM
jackjack jackjack is offline
D-Web Trainee
 
Join Date: Sep 2008
Posts: 12
jackjack is on a distinguished road
Default Re: PHP:Tutorial - Email Verification

PHP is a computer scripting language. Originally designed for producing dynamic web pages, it has evolved to include a command line interface capability and can be used in standalone graphical applications.

jack

____________________________________

fear of flying cowboy boots
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 10-05-2008, 11:19 PM
sakari sakari is offline
D-Web Trainee
 
Join Date: Sep 2008
Posts: 36
sakari is on a distinguished road
Default Re: PHP:Tutorial - Email Verification

hi

nice read, thanks for the tut.
__________________
free templates
web design company
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
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

vB 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
Formal verification method Shanthi Software Testing 0 01-14-2008 04:31 AM
What is the verification testing Strategies? sundarraja Software Testing 2 01-14-2008 03:23 AM
Email Verification in ASP.NET oxygen C# Programming 1 09-07-2007 08:44 AM
What is verification? validation? devarajan.v Software Testing 2 07-17-2007 07:56 AM
Verification & Validation vadivelanvaidyanathan Quality Engineering and Methodologies 0 04-18-2007 04:06 AM


All times are GMT -7. The time now is 03:58 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0