Solved

Form testing

Posted on 2004-10-22
173 Views
Last Modified: 2008-03-06
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')
0
Question by:x_terminat_or_3
    14 Comments
     
    LVL 12

    Expert Comment

    by:minichicken
    - 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
     
    LVL 12

    Expert Comment

    by:minichicken
    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
     
    LVL 8

    Expert Comment

    by:sigmacon
    // 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
     
    LVL 2

    Author Comment

    by:x_terminat_or_3
    '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
     
    LVL 8

    Expert Comment

    by:sigmacon
    // 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
     
    LVL 8

    Assisted Solution

    by:sigmacon
    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
     
    LVL 2

    Author Comment

    by:x_terminat_or_3
    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
     
    LVL 5

    Accepted Solution

    by:
    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
     
    LVL 12

    Assisted Solution

    by:minichicken
    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
     
    LVL 2

    Author Comment

    by:x_terminat_or_3
    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
     
    LVL 2

    Author Comment

    by:x_terminat_or_3
    basiclife  that's good!
    0
     
    LVL 8

    Expert Comment

    by:sigmacon
    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
     
    LVL 2

    Author Comment

    by:x_terminat_or_3
    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
     
    LVL 5

    Expert Comment

    by:basiclife
    Glad I could help 8-D
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    As you all know PHP got one problem, before using other files classes or functions you need to include or use require function. But now in PHP 5 there's solution for this, it's called __autoload() function. You can read more about function in PHP ma…
    Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

    877 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now