Forum Moderators: coopster
function checkEmail($email)
{
$email = mysql_real_escape_string($email);
$query_emailcheck = "SELECT email FROM hfs_users WHERE email = '$email'";
$result_emailcheck = @mysql_query($query_emailcheck);
$num_sameemail = @mysql_num_rows($result_emailcheck);
if ($num_sameemail> 0) {
$errors[] = "This email is being used by another user.";
}if(eregi("^[a-zA-Z0-9_]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$]", $email))
{
list($Username, $Domain) = split("@",$email);
if(getmxrr($Domain, $MXHost))
{
$errors[] = "Email address is invalid.";
}
else
{
if(@fsockopen($Domain, 25, $errno, $errstr, 30))
{
$errors[] = "Email address is invalid.";
}
else
{
return FALSE;
}}
}}
The same thing goes for fsockopen(). You should be checking for a "false" return.
You also only return false, you never show where you return true. So something like this should work:
function checkEmail($email)
{
$email = mysql_real_escape_string($email);
$query = "SELECT `email` FROM `hfs_users` WHERE `email` = '$email'";
$result = @mysql_query($query);
$errors = array();if(mysql_num_rows($result) > 0)
{
$errors[] = 'This email is being used by another user.';
}
else
{
if(!eregi("^[a-z0-9_]+@[a-z0-9-]+\.[a-z0-9.-]+$", $email))
{
list($username, $domain) = explode('@', $email);
if(!getmxrr($domain, $MXHosts))
{
$errors[] = 'Email address is invalid.';
}
else
{
if(!@fsockopen($domain, 25, $errno, $errstr, 30))
{
$errors[] = 'Email address is invalid.';
}
}
}
}if(!empty($errors))
return $errors;return true;
}
I'd also suggest you look at the eregi page in the php manual and look through the user notes. There are much better email eregi patterns to use ;)
heres what i have to as my function..
function checkEmail($email) {
$email = mysql_real_escape_string($email);
$query = "SELECT `email` FROM `hfs_users` WHERE `email` = '$email'";
$result = @mysql_query($query);
$errors = array();
if(mysql_num_rows($result) > 0)
{
$errors[] = 'This email is being used by another user.';
}
else { if(!eregi("^[a-z0-9_]+@[a-z0-9-]+\.[a-z0-9.-]+$", $email))
{
list($username, $domain) = explode('@', $email);
if(!getmxrr($domain, $MXHosts))
{
$errors[] = 'Email address is invalid.';
}
else
{
if(!@fsockopen($domain, 25, $errno, $errstr, 30)) { $errors[] = 'Email address is invalid.';
}
}
}
} if(!empty($errors)) return $errors; return true;
}
-____________________________-
also here are the contents of my email testing file...
<?php
include("mysql_connect.php");
include("functions.php");
$urlemail = $_GET['email'];
if(checkEmail($urlemail) == FALSE)
{
echo "E-mail entered is not valid.";
}
else
{
echo "E-mail entered is valid.";
}
?>
[edited by: GamingLoft at 11:22 pm (utc) on Feb. 2, 2008]
1.) Use "===" to see if the left operand is strictly equal to the right
2.) Use "!==" to see if the left operand is strictly not equal to the right
So essentially what we're asking is if the result of checkEmail() is not boolean true (and only boolean true) then there are errors. If it is boolean true, then the email address is okay.
It's not false anymore because we don't return false, we return whatever error(s) there are. You can change the function to just return false; however, it's not always what you want since there are multiple fail points (MX records, openning a connection, etc.).
For example, if say I give my email as user@domain123.com and you see that there are mx records there. When you go to open a connection on port 25, it fails. This does not necessarily mean that it's a bad webserver. I could have limited it only to SSL (443) or maybe I'm using TLS or just using a different port. I wouldn't fail just on having no connection to a server.
here is the email function from functions.php
function checkEmail($email) {
$email = mysql_real_escape_string($email);
$query = "SELECT `email` FROM `hfs_members` WHERE `email` = '$email'";
$result = @mysql_query($query);
$errors = array();
if(mysql_num_rows($result) > 0)
{
$errors[] = 'This email is being used by another user.';
}
else { if(!eregi("^[a-z0-9_]+@[a-z0-9-]+\.[a-z0-9.-]+$", $email))
{
list($username, $domain) = explode('@', $email);
if(!getmxrr($domain, $MXHosts))
{
$errors[] = 'Email address is invalid.';
}
else
{
if(!@fsockopen($domain, 25, $errno, $errstr, 30)) { $errors[] = 'Email address is invalid.';
}
}
}
} if(!empty($errors)) return $errors; return true;
}
and here is the source of my email checker...
<?php
include("mysql_connect.php");
include("functions.php");
$urlemail = $_GET['email'];
if(checkEmail($email)!== true)
{
echo "E-mail entered is valid.";
}
else
{
echo "E-mail entered is NAAAAAAT valid.";
}
?>
[edited by: GamingLoft at 10:14 pm (utc) on Feb. 4, 2008]
function checkEmail($email) {
$email = mysql_real_escape_string($email);
$query = "SELECT `email` FROM `hfs_members` WHERE `email` = '$email'";
$result = @mysql_query($query);
$errors = array(); if(mysql_num_rows($result) > 0)
{
$errors[] = 'This email is being used by another user.';
}
else
{
if(!eregi("^[a-z0-9_]+@[a-z0-9-]+\.[a-z0-9.-]+$", $email))
{
list($username, $domain) = explode('@', $email);
if(!getmxrr($domain, $MXHosts))
{
$errors[] = 'Email address is invalid.';
}
else
{
if(!@fsockopen($domain, 25, $errno, $errstr, 30))
{
$errors[] = 'Email address is invalid.';
}
}
}
}
if(!empty($errors))
return $errors;
return true;
}
To use this function, your code should be:
if(($rslt = checkEmail($email)) === true)
{
echo 'The email is valid';
}
else
{
echo 'The email is invalid. Error returned:<br />' . implode('<br />', $rslt);
}
after testing the code you had given me i found it didn't work the following email
"bobosycjs@sjdhasjdhsadsa.ceas"
came up valid... i did more testing and found the reason behind this is that the fsock checker isnt working
any email that passes the ereg test is valid, i need help with fixing this fsock i looked at the script but i cant think of anything here is my function...
function checkEmail($email) {
$email = mysql_real_escape_string($email);
$query = "SELECT `email` FROM `hfs_users` WHERE `email` = '$email'";
$result = @mysql_query($query);
$errors = array();
if(mysql_num_rows($result) > 0)
{
$errors[] = 'This email is being used by another user.';
}
else
{
if(!eregi("^[a-z0-9_]+@[a-z0-9-]+\.[a-z0-9.-]+$", $email)) {
list($username, $domain) = explode('@', $email);
if(!getmxrr($domain, $MXHosts))
{
$errors[] = 'Email address is invalid.';
}
else
{
if(!@fsockopen($domain, 25, $errno, $errstr, 30))
{
$errors[] = 'Email address is invalid.';
} } } } if(!empty($errors)) return $errors; return true; }
i get this echoed/error when the email SHOULD come out valid
----------------------
Warning: implode() [function.implode]: Bad arguments. in /home/EXAMPLE/public_html/site/emaila.php on line 11
The email is invalid. Error returned:
----------------------
(emaila is my tester file here is the contents of emaila
<?php
include("mysql_connect.php");
include("functions.php");
$urlemail = $_GET['email'];
if(($rslt = checkEmail($urlemail)) === true)
{
echo 'The email is valid';
}
else
{
echo 'The email is invalid. Error returned:<br />' . implode('<br />', $rslt);
}
?>
i get this echoed/error when the email SHOULD come out valid
----------------------
Warning: implode() [function.implode]: Bad arguments. in /home/EXAMPLE/public_html/site/emaila.php on line 11
The email is invalid. Error returned:
----------------------
(emaila is my tester file here is the contents of emaila
<?php
include("mysql_connect.php");
include("functions.php");
$urlemail = $_GET['email'];
if(($rslt = checkEmail($urlemail)) === true)
{
echo 'The email is valid';
}
else
{
echo 'The email is invalid. Error returned:<br />' . implode('<br />', $rslt);
}
?>
and here is my function...
function checkEmail($email) {
$email = mysql_real_escape_string($email);
$query = "SELECT `email` FROM `hfs_users` WHERE `email` = '$email'";
$result = @mysql_query($query);
$errors = array();
if(mysql_num_rows($result) > 0)
{
$errors[] = 'This email is being used by another user.';
}
else
{
if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) {
$errors[]="$email is not properly formatted";
}
}
if(!empty($errors)){ return $errors; return true; }}
anyone know why this error is occuring? or see anything wrong with my coding?
[edited by: GamingLoft at 1:07 am (utc) on Feb. 5, 2008]
Heh, thanks a lot bpat you've been amazingly helpful throughout this thread!
///////////
Actually i have another problem, where i am trying to "imitate this" with my username check function...
here is my username check function..
function checkUsername($username)
{
if (eregi('^[[:alnum:]\.\?\!\-\_\'\-]{4,12}$', stripslashes(trim($username)) )) {//must be 4-12 letters/chars long .?! - _ ' all aloud + nums and letters
$user = mysql_real_escape_string($username);
$query_usernamecheck = "SELECT username FROM hfs_users WHERE username = '$username'";
$result = @mysql_query($query_usernamecheck);
$num_samename = @mysql_num_rows($result_usernamecheck);
if ($num_samename> 0) { //checks if anyone is using this nam
$errors[] = "This name is not availible.";//error message if someones using name
}
else {
$user = mysql_real_escape_string($username);
$query_blockedname = "SELECT username FROM hfs_blockedname WHERE username = '$username'";
$result_blockedname = @mysql_query($query_blockedname);
$num_blockedname = @mysql_num_rows($result_usernamecheck);
if ($num_blockedname> 0) {
$errors[] = "The username you entered is not aloud.";//error message if name is put in unacceptable list
}
else{
$check= eregi("slime¦kille[rd]¦puke¦vomit¦suck¦spastic¦spaz¦(you )¦( you )¦(u )¦( u )¦( of?f$)¦( of?f )¦[asz]{3,}", $intext);
if ($check) {
$errors[] = "Usernames are not aloud to contain foul language.";
} else { return FALSE;
}}}}
else {
$errors[] = "You're username must contain only letters, numbers and the following characters: .?! - _";
}
if(!empty($errors)){ return $errors; }
return true;
}
here is my name checking file...
<?php
include("mysql_connect.php");
include("functions.php");
$urlname = $_GET['name'];
if(($rslt = checkUsername($urlname)) === true)
{
echo 'Its all good.';
}
else
{
echo implode('<br />', $rslt);
}
?>
so its pretty much the same as the email function but i get this error when running my name checker...
"
Warning: implode() [function.implode]: Bad arguments. in /home/EXAMPLE/public_html/EXAMPLE/namea.php on line 11"
know why this is happening AGAIN?!?!
[edited by: GamingLoft at 10:06 pm (utc) on Feb. 5, 2008]
[edited by: jatar_k at 10:15 pm (utc) on Feb. 5, 2008]
[edit reason] removed some words [/edit]