Form testing

Hi all


Can anybody tell me to test for validity of an email address?


I mean, can one detect if an address is bogus without actually sending mail to it?


For starters, I need to see if it's in the right format ie user@some-server.somewhereelse.com.us  

And if the address isn't valid, I want to

Redirect to the referer with posted 'frm_Fail_Email=true'

so that I can retrieve this with $post("frm_Fail_Email')
LVL 2
x_terminat_or_3Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

minichickenCommented:
- check an email address validity
- @access public
- @param string $address : email address to check
- @return true if email adress is ok

function ValidEmail($address)
{
      if( ereg( ".*<(.+)>", $address, $regs ) ) {
            $address = $regs[1];
      }
       if(ereg( "^[^@  ]+@([a-zA-Z0-9\-]+\.)+([a-zA-Z0-9\-]{2}|net|com|gov|mil|org|edu|int)\$",$address) )
             return true;
       else
             return false;
}
0
minichickenCommented:
So in your example:

**********************************************************

function ValidEmail($address)
{
     if( ereg( ".*<(.+)>", $address, $regs ) ) {
          $address = $regs[1];
     }
      if(ereg( "^[^@  ]+@([a-zA-Z0-9\-]+\.)+([a-zA-Z0-9\-]{2}|net|com|gov|mil|org|edu|int)\$",$address) )
           return true;
      else
           return false;
}

if (ValidEmail("user@some-server.somewhereelse.com.us  "))
{
       //if VALID do something
}
else
{
      //else INVALID do something else
}
0
sigmaconCommented:
// assuming email address is in $email
if (!ereg('^[A-Za-z0-9_\.\-]+\@([A-Za-z0-9][A-Za-z0-9\-]*\.)+[a-zA-Z]{2,3}$', $email)) {
   // do your redirect here
}
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

x_terminat_or_3Author Commented:
'k sigmacon, I think I'll go for your example as testing for extension (net|com...) won't work because new extensions are created every day.


What about the redirecting?

If invalid email, I want to redirect to the referer page, with the name/value pair 'frm_Fail_Email=true' in the postdata
0
sigmaconCommented:
// the value will be in $_GET, not in $_POST !!!

if (!ereg('^[A-Za-z0-9_\.\-]+\@([A-Za-z0-9][A-Za-z0-9\-]*\.)+[a-zA-Z]{2,3}$', $email)) {
   header('Location: form.php?frm_Fail_Email=true');
}
0
sigmaconCommented:
I have to update: You SHOULD NOT continue execution of that file after sending the header and you want the referrer. If this code happens later down the page, you might want to use output buffering (ob_start()) to prevent headers from being sent too early.

if (!ereg('^[A-Za-z0-9_\.\-]+\@([A-Za-z0-9][A-Za-z0-9\-]*\.)+[a-zA-Z]{2,3}$', $email)) {
   header('Location: ' . (isset($_SERVER['HTTP_REFERER'])? $_SERVER['HTTP_REFERER'] : 'form.php') . '?frm_Fail_Email=true');
   die();
}
0
x_terminat_or_3Author Commented:
Sig

I see you added a test to see if the referer is set.  I what case are they empty, asides from when the page's url is typed in the addressbar?
0
basiclifeCommented:
I hate posting links but http://www.zend.com/zend/spotlight/ev12apr.php also includes code for connecting to the mail server and verifying the email account is valid
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
minichickenCommented:
Hi x_terminat_or_3

I would like to know if my function worked for you?
If you would like to do a redirect with a variable, then like sigmacon said you can only use $_GET and not $_POST
So it will be something like this:

***********************************************

function ValidEmail($address)
{
     if( ereg( ".*<(.+)>", $address, $regs ) ) {
          $address = $regs[1];
     }
      if(ereg( "^[^@  ]+@([a-zA-Z0-9\-]+\.)+([a-zA-Z0-9\-]{2}|net|com|gov|mil|org|edu|int)\$",$address) )
           return true;
      else
           return false;
}

if (ValidEmail("user@some-server.somewhereelse.com.us  "))
{
       header('Location: valid_page.php?frm_Fail_Email=true'); //redirect to page stating the email is true (valid)
       exit;
}
else
{
      header('Location: valid_page.php?frm_Fail_Email=false'); //redirect to page stating the email is false (invalid)
      exit;
}

NOTE: remember to use "exit;" after redirecting using header to exit script or else it will execute anything after it, which may cause an error

regards-
0
x_terminat_or_3Author Commented:
Hi chicken

I didn't test your example because I don't believe that it would be wise to test for .net,.com, .gov,.mil,... because those are not constants!

But don't worry, I appreciate your input and will reward (as usual) anybody who a/is part of the solution b/thought me something I didn't know already.


x_terminat_or_3 (Ramses)
0
x_terminat_or_3Author Commented:
basiclife  that's good!
0
sigmaconCommented:
x_terminat_or_3

As pointed out in the comments to the link given by basiclife, this form of verification is not very reliable. I have actually written a similar email verification a while ago, and could have posted it here, but that's was not exactly what your question seemed to be. Anyway: Since I have done this type of 'online' email address verification, I can tell you that the hassle and time involved is not worth it. When people fake email addresses, they usually use well-know providers, such as AOL, Yahoo or Hotmail/MSN. The verification for Hotmail works actually pretty well, but for AOL not at all and for Yahoo it depends on which server you're talking to. Also, finding the mail exchangers, connecting to them and performing the communication can easily take 3, 4 or even more than 10 seconds, even if you're in a very fast datacenter (on a very fast connection). I have tried it - this method is unusable for form field email address validation, the wait time and potential failure rate scares too many users away. Just my 2 cents.
0
x_terminat_or_3Author Commented:
Hi sigma

Your 2cents are noted AND appreciated.  

If you send a form, one can accept a 10sec delay, and if the user has no malintention, then it will work like a charm!

0
basiclifeCommented:
Glad I could help 8-D
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.