SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    a fresh, new start... dujmovicv's Avatar
    Join Date
    Aug 2006
    Location
    Earth
    Posts
    559
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Validate e-mails

    Greetings,

    I have to validate ALL e-mail addresses from a database and I have this script :
    PHP Code:
        $query "SELECT * FROM recipients ORDER by id ASC";
        
    $result mysql_query($query) or die('Error : ' mysql_error());

    while(
    $row mysql_fetch_array($result)) {

    $email $row['recipient_email'];

        function 
    check_email_address($email) {
        
    // First, we check that there's one @ symbol, and that the lengths are right
        
    if (!ereg("^[^@]{1,64}@[^@]{1,255}$"$email)) {
        
    // Email invalid because wrong number of characters in one section, or wrong number of @ symbols.
        
    return false;
        }
        
    // Split it into sections to make life easier
        
    $email_array explode("@"$email);
        
    $local_array explode("."$email_array[0]);
       for (
    $i 0$i sizeof($local_array); $i++) {
       if (!
    ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$"$local_array[$i])) {
       return 
    false;
       }
       }
       if (!
    ereg("^\[?[0-9\.]+\]?$"$email_array[1])) { // Check if domain is IP. If not, it should be valid domain name
       
    $domain_array explode("."$email_array[1]);
       if (
    sizeof($domain_array) < 2) {
       return 
    false// Not enough parts to domain
       
    }
       for (
    $i 0$i sizeof($domain_array); $i++) {
       if (!
    ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$"$domain_array[$i])) {
       return 
    false;
       }
       }
       }
       return 
    true;
       }
       
           if (
    check_email_address($email)) {
        echo 
    $email ' is a valid email address.';
        } else {
        echo 
    $email ' is NOT a valid email address.';
        }

    it validates the FIRST e-mail address from the database and then returns error :
    Code:
    Fatal error: Cannot redeclare check_email_address() (previously declared in /var/www/responder_scripts/check_emails.php:12) in /var/www/responder_scripts/check_emails.php on line 12
    Can someone help me why this script doesn't 'move' on the second, third, etc. emails from the database?

    Thank you very much!

    Full time ADMIN - art community
    Part time coder - dsign

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Don't define the check_email_address function within a loop. You define functions once and call them many times.

    You actually don't ever use the function within the loop anyway.

  3. #3
    a fresh, new start... dujmovicv's Avatar
    Join Date
    Aug 2006
    Location
    Earth
    Posts
    559
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you Dan!!! That was a stupid mistake of mine....
    Have a nice day!

    Full time ADMIN - art community
    Part time coder - dsign

  4. #4
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Take a look at filter_var(), it should help you validate the email address.

    PHP Code:
    <?php
    $rResult 
    mysql_query('SELECT recipient_email FROM recipients ORDER by id ASC');
    while (
    $aRecord mysql_fetch_assoc($rResult))
    {
        
    $bIsValidEmail = (Boolean)filter_var($aRecord['recipient_email'], FILTER_VALIDATE_EMAIL);
        if(
    $bIsValidEmail)
        {
            echo 
    $aRecord['recipient_email'], ' is a VALID email';
        }
        else
        {
            echo 
    $aRecord['recipient_email'], ' is an INVALID email';
        }
    }
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  5. #5
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    The main issue with using filter_var is that it's not available at most hosts. Even if your host is cutting edge enough to have it, the code won't be portable (for the time being).

    As far as I can tell, the most popular operating systems among the major Linux hosting providers are RHEL (RedHat Enterprise Linux) and CentOS, which is equivalent without the branding and associated fees. RedHat does not support PHP 5.2 yet (it's not on their package repositories, etc); as an enterprise OS, stability is favored over bleeding edge, and they're still only supporting 5.1.x.

  6. #6
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Great info Dan, thanks. Luckily a lot of my validation is abstracted away, so I might alter it just to be safe... at least on my distributed code.

    Hopefully, it won't be a huge job!

    Cheers.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •