Senin, 09 Februari 2009

Verify a User's Email Address Using PHP-2
By Joe Marini
March 18th 2003

Reader Rating: 8.1

WThe code above takes a string of the form "username@emaildomain.com" and checks to see if the domain is real. First, the code calls the split() function to split the email string into "username" and "emaildomain.com", as we're only interested in the domain.

Once we've got the domain, the code calls checkdnsrr, with the domain string and "MX" as the arguments. The second argument tells checkdnsrr what type of DNS record to look for. As we're interested only in whether the given domain can handle email, we use the "MX" argument, which means "look for the Mail Exchange record."

If the checkdnsrr function returns TRUE, then we know we've got a valid email domain (but not necessarily a valid user name). If the function returns FALSE, then the email domain given is invalid.

Gotcha! - checkdnsrr Doesn't Do Windows (Yet)
There's one small problem, however, if you're using PHP on a Windows server. The checkdnsrr function is not implemented on the Windows platform, so if you're going to deploy this code on a Windows-based machine, you'll need to do some extra work yourself.

The way to get around this problem is to write your own version of checkdnsrr. We'll call our version myCheckDNSRR, the code for which is as follows:

function myCheckDNSRR($hostName, $recType = '')
{
if(!empty($hostName)) {
if( $recType == '' ) $recType = "MX";
exec("nslookup -type=$recType $hostName", $result);
// check each line to find the one that starts with the host
// name. If it exists then the function succeeded.
foreach ($result as $line) {
if(eregi("^$hostName",$line)) {
return true;
}
}
// otherwise there was no mail handler for the domain
return false;
}
return false;
}

Our version of the checkdnsrr function works by taking advantage of a system call that's available in Windows called nslookup, which performs essentially the same function. To call the nslookup function, our code uses PHP's exec function, which executes a system command. It returns the result of the command as an array of strings in the $result parameter.

When the nslookup function successfully finds an entry for the given domain, the output will look something like this:

Server: o1-sjc-ns1.o1.com
Address: 66.81.7.158
joemarini.com MX preference = 0, mail exchanger = smtp.joemarini.com

To determine whether a mail handler for the domain exists, the function loops through each line of the output in search of the line that starts with the given host name. If such a line is found, then the function returns TRUE, otherwise it returns FALSE.

Conclusion
While there's no foolproof way to make sure a user isn't giving you a completely bogus email address, you can at least help cut down on the problem by making sure that email addresses your site is given at least correspond to a real domain. Using PHP's checkdnsrr function, you can look up the registration record for a given domain and see if it's a real domain before saving away a user's email address.

Download the code for this article. http://www.sitepoint.com/examples/emailverify/verifyemail.zip



---------------------------------


Related :

UsingCrystalReports6
VerifyUserEmailAddressPHP-1
VerifyUserEmailAddressPHP-2
viewinformationvb2005
WatermarkImagesFlyPHP-1
WatermarkImagesFlyPHP-2
WhatAretheIssues
What-isMySQL
WhatSQLServerExpress
writefileinvb2005
WritingFileDialogBox
SimpleDatabasevb6
SQLServer2005fromVisualBasic6
SQLServer2005withPHP
SQLServerSecurityGuidelines
TransferringFiles
UsingCrystalReports6