Click to See Complete Forum and Search --> : email validation error


NickG21
12-27-2006, 01:46 PM
hey everyone, below is my code for an e-mail validation script i am working on. i am very new to php and have developed this through the use of other threads on forums and tutorials i have found online. if anyone could tell me what is wrong about it i would appreciate it. there are no errors being generated for the php syntax but the code doesn't access the DNS and display whether the e-mail address is really valid or not. thank you
nick

<?php
function checkEmail($email)
{
if (!preg_match("/^( [a-zA-Z0-9] )+( [a-zA-Z0-9\._-] )*@( [a-zA-Z0-9_-] )+([a-zA-Z0-9\._-] +)+$/" , $email)) {
return false;
}
list($username,$domain) = split('@',$email);
$mxhosts = array();
if(!getmcxrr($domain, $mxhosts)) {

if(!fsockopen($domain, 25, $errno, $errstr, 30))
{
return false;
}else{

foreach($mxhosts as $host) {
if (fsockopen($host,25,$errno,$errstr,30)) {
return true;
}
}
return false;
}
}
if(!fsockopen($domain,25,$errno,$errstr,30)) {
return false;
}else{
return true;
}
}

if (isset($_POST['submit'])) {
$email = trim($_POST['email']);
if(!checkEmail($email)) {
echo 'Invalid email address!';
}
else {
echo 'Email address is valid';
}
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<form method="post" action="emailcheck.php">
<table class="qForm">
<tr>
<td><a>Email:</a></td>
<td><input type="text" size="28" name=email value-<? echo"$email"?>"><br/></td>
</tr>
<tr>
<input type="hidden" value="1"
<td><input type="submit" name="submit" value="submit"></td>
</tr>
</table>
</form>
</body>
</html>

NogDog
12-29-2006, 09:49 PM
Do you have a custom function defined somewhere called getmcxrr()? You call it in this line...
if(!getmcxrr($domain, $mxhosts)) {...but the PHP on-line reference only shows a getmxrr() function (http://www.php.net/getmxrr).

NogDog
12-29-2006, 09:52 PM
PS: What is going to happen if a user enters a (perfectly valid) email address such as:
myname@servername.subdomain.domain.com...?

(Such questions are why, as far as I'm concerned, the only dependable method of email validation that won't get false negatives is the sending of a confirmation email with a link the user must click to validate it.)