Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
E-mail verification script
Old 02-16-2009, 03:42 PM E-mail verification script
GhettoFish's Avatar
Average Talker

Posts: 24
Name: Martin Söderberg
Trades: 0
Hey fellas.

I have made this small little script to verify an E-mail that is used in a Mail form on my page. It seems like this works fine as it is now but I was wondering if you could see anything that could be made better and more efficient?

PHP Code:
function verify($email){
    
    
// Check so the e-mail uses allowed chars and correct structure.
    // If match not made it sends user back to form.
    
if (!preg_match('/^[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+)*\@[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+)+$/i'$email)){
//        return false;
        // Return incorrect e-mail to main site.
        
echo 'Failed due to invalid E-mailadress';
        exit;
    }
    
    
// Grab the domain from the e-mail adress.
    
$domain strstr($email'@');
    
$domain substr($domain1); // Shifts selection by one so not @ is added with the domain.
    
    // Checks that the domain is resolved to an IP. 
    // If hostname isn't resolved to and IP user is sent back to form.
    
if (!ip2long(gethostbyname($domain))){
//        return false;
        // Return incorrect e-mail to main site.
        
echo 'Failed due to invalid domain.';
        exit;
    }
    
    echo 
'Valid E-Mail!';
    
}
if (isset(
$_POST['submit'])) {
    
verify($_POST['email']);

I thought that it would be better to resolve the domain to an IP instead of pinging the domain due to that not all servers allow for pinging.


I will also add the basic HTML for testing.
HTML Code:
<html>
<head>
<title>Validate Email</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>

<form name="myform" method="post" action="test.php">

<input type="text" name="email"/>
<input type="submit" name="submit" value="Submit"/>

</form>

</body>
</html>
Would be awesome if you found anything that could be improved, which i must admit is quite likely that you do
GhettoFish is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 02-18-2009, 07:06 AM Re: E-mail verification script
Novice Talker

Posts: 5
Trades: 0
Well, you can compare your verification script with the one I found here:
http://www.phpeasystep.com/workshopview.php?id=24
I'v been looking for a verification script for "ages", and the tutorial helped me out .
__________________

Please login or register to view this content. Registration is FREE
Ariyes1 is offline
Reply With Quote
View Public Profile
 
Old 02-18-2009, 07:08 PM Re: E-mail verification script
GhettoFish's Avatar
Average Talker

Posts: 24
Name: Martin Söderberg
Trades: 0
The problem with that script is that it only checks if the e-mail has been sent to an adress, not the actual adress it self.

What I wanted to do was to check the inputted e-mail address for bad letters, bad domain etc. The script does that as it is quite fine but would be great to get some more feedback on parts you can make better.

I have made a better version which is a bit cleaned up and it looks like this.
PHP Code:
function verify($email){
    
    
// If no e-mail address is inputted to begin with the user will get an error message.
    
if(!isset($email)){
        
// Return incorrect e-mail to main site.
        
return -1;
    }
    
    
// Check so the e-mail uses allowed chars and correct structure.
    // If match not made it sends user back to form.
    
if (!preg_match('/^[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+)*\@[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+)+$/i'$email)){
        
// Return incorrect e-mail to main site.
        
return -2;
    }
    
    
// Grab the domain from the e-mail adress.
    
$domain strstr($email'@');
    
$domain substr($domain1); // Shifts selection by one so not @ is added with the domain.
    
    // Checks that the domain is resolved to an IP. 
    // If hostname isn't resolved to and IP user is sent back to form.
    
if (!ip2long(gethostbyname($domain))){
        
// Return incorrect e-mail to main site.
        
return -3;
    }
    
    
// It passed all the tests, hurrah for us!
    
return 0;

As I see it this is the best to do it. I have seen scripts that checks the domain through its DNS to see if it has Mail Exchange registerd on it. But when i read more aobut it not all DNS servers and domain/IPs has this flag so it might turn down a valid e-mail due to that they cannot find a MX-flag on the IP.
GhettoFish is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to E-mail verification script
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.18124 seconds with 12 queries