pratik_learner
07-13-2005, 11:35 AM
I have written the following class that returns 0 is the e-mail is valid or a number from 1-5 depending on the type of invalidation. Try using some network functions to check if the domain or the email exists but it is not reliable because it is subject to the uptime of the server. This silly cause could send a potential visitor ( or a customer) from signing up ( or purchasing ) for a service.
The better thing to do is to send the an autoresponder and check for the confirmation. The reason for this is someone might signup ( intentionally or otherwise) under someone else's email ID and the rightful entity to whom the ID belongs may accuse you of sending spam.
<?PHP
/*
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
class ValidateEmail
{
function InvChrDM($em)
{
$flag = true;
for( $i = 0; $i < strlen($em); $i++)
{
$cLet = substr($em, $i, 1);
if( ctype_alnum($cLet) or $cLet == '-' or $cLet == '.' )
{
$flag = TRUE;
}
else
{
$flag = FALSE;
break;
}
}
return $flag;
}
function InvChrEM($em)
{
$flag = true;
for( $i = 0; $i < strlen($em); $i++)
{
$cLet = substr($em, $i, 1);
if( ctype_alnum($cLet) or $cLet == '_' or $cLet == '-' or $cLet == '.' )
{
$flag = TRUE;
}
else
{
$flag = FALSE;
break;
}
}
return $flag;
}
/* Checks for blank fields that contain only whitespace or nothing*/
function ChkBlnk($em)
{
/*Checks for ONLY whitespace fields
ctype_graph checks for printable characters
ctype_space chcks for whitespace like space tab etc.*/
if(!ctype_graph($em) and ctype_space($em))
{
return false;
}
/*Checks for blank fields*/
else if($em == "")
{
return false;
}
else
{
return true;
}
}
/* The following function checks for the @ character in the*/
function ChkEm($em)
{
if(strpbrk($em, '@'))
{
if(substr_count($em, '@') > 1)
{
return false;
}
else
{
return true;
}
}
else
{
return false;
}
}
/*Checks for validity of domain name*/
function ChkDom($em)
{
/* Three character domain names */
$dm1 = array(
'.ac', '.ad', '.ae', '.af', '.ag', '.ai', '.al', '.am',
'.an', '.ao', '.aq', '.ar', '.as', '.at', '.au', '.aw',
'.az', '.ba', '.bb', '.bd', '.be', '.bf', '.bg', '.bh',
'.bi', '.bj', '.bm', '.bn', '.bo', '.br', '.bs', '.bt',
'.bv', '.bw', '.by', '.bz', '.ca', '.cc', '.cd', '.cf',
'.cg', '.ch', '.ci', '.ck', '.cl', '.cm', '.cn', '.co',
'.cr', '.cu', '.cv', '.cx', '.cy', '.cz', '.de', '.dj',
'.dk', '.dm', '.do', '.dz', '.ec', '.ee', '.eg', '.eh',
'.er', '.es', '.et', '.fi', '.fj', '.fk', '.fm', '.fo',
'.fr', '.ga', '.gd', '.ge', '.gf', '.gg', '.gh', '.gi',
'.gl', '.gm', '.gn', '.gp', '.gq', '.gr', '.gs', '.gt',
'.gu', '.gw', '.gy', '.hk', '.hm', '.hn', '.hr', '.ht',
'.hu', '.id', '.ie', '.il', '.im', '.in', '.io', '.iq',
'.ir', '.is', '.it', '.je', '.jm', '.jo', '.jp', '.ke',
'.kg', '.kh', '.ki', '.km', '.kn', '.kp', '.kr', '.kw',
'.ky', '.kz', '.la', '.lb', '.lc', '.li', '.lk', '.lr',
'.ls', '.lt', '.lu', '.lv', '.ly', '.ma', '.mc', '.md',
'.mg', '.mh', '.mk', '.ml', '.mm', '.mn', '.mo', '.mp',
'.mq', '.mr', '.ms', '.mt', '.mu', '.mv', '.mw', '.mx',
'.my', '.mz', '.na', '.nc', '.ne', '.nf', '.ng', '.ni',
'.nl', '.no', '.np', '.nr', '.nu', '.nz', '.om', '.pa',
'.pe', '.pf', '.pg', '.ph', '.pk', '.pl', '.pm', '.pn',
'.pr', '.ps', '.pt', '.pw', '.py', '.qa', '.re', '.ro',
'.ru', '.rw', '.sa', '.sb', '.sc', '.sd', '.se', '.sg',
'.sh', '.si', '.sj', '.sk', '.sl', '.sm', '.sn', '.so',
'.sr', '.st', '.sv', '.sy', '.sz', '.tc', '.td', '.tf',
'.tg', '.th', '.tj', '.tk', '.tm', '.tn', '.to', '.tp',
'.tr', '.tt', '.tv', '.tw', '.tz', '.ua', '.ug', '.uk',
'.um', '.us', '.uy', '.uz', '.va', '.vc', '.ve', '.vg',
'.vi', '.vn', '.vu', '.wf', '.ws', '.ye', '.yt', '.yu',
'.za', '.zm', '.zw'
);
/* Four character domain names */
$dm2 = array(
'.biz','.com','.edu','.gov','.int','.mil','.net', 'org',
'.pro'
);
/*Five character domain names */
$dm3 = array('aero','.coop','.info','.name');
/* Solitary seven-letter domain*/
$dm7 = '.museum';
/*Finds string length and substrings */
$len = strlen($em);
$sub3 = substr($em,$len-3, $len);
$sub4 = substr($em,$len-4, $len);
$sub5 = substr($em,$len-5, $len);
$sub7 = substr($em,$len-7, $len);
/*Searches if the any of substrings match the listed domain
suffixes*/
$flag = false;
/* Searches if the three letter substring is valid*/
if( $flag != true )
{
for($i = 0; $i < count($dm1); $i++)
{
if($sub3 == $dm1[$i])
{
return true;
$flag = true;
break;
}
}
}
/* Searches if the four letter substring is valid*/
if( $flag != true )
{
for($j = 0; $j < count($dm2); $j++)
{
if($sub4 == $dm2[$j])
{
return true;
$flag = true;
break;
}
}
}
/* Searches if the five letter substring is valid*/
if( $flag != true )
{
for($k = 0; $k < count($dm3); $k++)
{
if( $sub5 == $dm3[$k])
{
return true;
$flag = true;
break;
}
}
}
/* Searches for seven letter .museum domain*/
if( $flag != true )
{
for($l = 0; $l < count($dm7); $l++)
{
if( $sub7 == $dm7)
{
return true;
$flag = true;
break;
}
}
}
/* Return false if none of substrings match the listed domain names*/
if( $flag == false)
{
return false;
}
}
/*Returns the string that comes after the @ character*/
function SpltEmID($em,$what)
{
$dmn = strpbrk($em, '@');
$dmn = explode('@',$dmn);
if($what == 'domain')
{
return $dmn[0];
}
else if($what == 'email')
{
return $dmn[1];
}
else
{
return FALSE;
}
}
};
function IsValid($eml)
{
$dm = ValidateEmail::SpltEmID($eml,'email');
$em = ValidateEmail::SpltEmID($eml,'domain');
$tmp1 = ValidateEmail::ChkBlnk($eml);
$tmp2 = ValidateEmail::ChkEm($eml);
$tmp3 = ValidateEmail::ChkDom($dm);
$tmp4 = ValidateEmail::InvChrDM($dm);
$tmp5 = ValidateEmail::InvChrEM($em);
while(1)
{
if(!$tmp1)
{
return '1';
break;
}
elseif(!$tmp2)
{
return '2';
break;
}
elseif(!$tmp3)
{
return '3';
break;
}
elseif(!$tmp4)
{
return '4';
break;
}
elseif(!$tmp5)
{
return '5';
break;
}
else
{
return '0';
break;
}
}
/*The function returns the following integers for invalid
error types
* 1 if blank field
* 2 if @ char not found
* 3 if email on invalid TLD
* 4 if invalid characters are found in domain name
* 5 if invalid characters are found in email
* 0 if the email is valid
NOTE : the values returned are in strings and are
*NOT* Boolean
*/
}
/*Zip Files Classes*/
/* http://www.phpclasses.org/browse/package/945.html */
/* Send HTML pages */
/* http://www.phpguru.org/static/mime.mail.html */
/* http://phpmailer.sourceforge.net */
/* SQL Courses */
/* http://www.sqlcourse.com/ */
/* http://www.w3schools.com/sql/ */
/* http://www.oreillynet.com/pub/ct/19 */
?>