Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    Regular Coder ArcticFox's Avatar
    Join Date
    Jan 2004
    Location
    Vostok Station, AQ
    Posts
    602
    Thanks
    35
    Thanked 3 Times in 3 Posts

    Question Email Validation always False

    Can someone take a look at this and give me a heads-up as to why this doesn't work?

    PHP Code:
    <?php


    // get posted data into local variables
    $EmailFrom Trim(stripslashes($_POST['EmailFrom'])); 
    $EmailTo "address@domain.com";
    $Subject "BSZ Contact Form";
    $Name Trim(stripslashes($_POST['Name'])); 
    $Street1 Trim(stripslashes($_POST['Street1'])); 
    $Street2 Trim(stripslashes($_POST['Street2'])); 
    $City Trim(stripslashes($_POST['City'])); 
    $State Trim(stripslashes($_POST['State'])); 
    $PostCode Trim(stripslashes($_POST['PostCode'])); 
    $Tel Trim(stripslashes($_POST['Tel'])); 
    $Comments Trim(stripslashes($_POST['Comments'])); 


    // TESTING validation


        
    function checkEmail($EmailFrom) {
         
    // checks proper syntax
         
    if(preg_match("/^( [a-zA-Z0-9] )+( [a-zA-Z0-9\._-] )*@( [a-zA-Z0-9_-] )+( [a-zA-Z0-9\._-] +)+$/" $EmailFrom)) {
          
    // gets domain name
          
    list($username,$domain)=split('@',$EmailFrom);
          
    // checks for if MX records in the DNS
          
    if(!checkdnsrr($domain'MX')) {
           return 
    false;
          }
          
    // attempts a socket connection to mail server
          
    if(!fsockopen($domain,25,$errno,$errstr,30)) {
           return 
    false;
          }
          return 
    true;
         }
         return 
    false;
        }




        
        
    // end TESTING validation


    // prepare email body text
    $Body "";
    $Body .= "Comments: ";
    $Body .= $Comments;
    $Body .= "\n";
    $Body .= "Name: ";
    $Body .= $Name;
    $Body .= "\n";
    $Body .= "Tel: ";
    $Body .= $Tel;
    $Body .= "\n";





        
    $EmailFrom trim($_POST['email']);  
        if(!
    checkEmail($EmailFrom)) { 
        print 
    "<meta http-equiv=\"refresh\" content=\"0;URL=oops.php\">";
        }
        else {
         print 
    "<meta http-equiv=\"refresh\" content=\"0;URL=thanks.php\">";
        }
    ?>

    No matter what I put in my email form, this doesn't like it. Is it a windows/linux thing?
    Last edited by ArcticFox; 01-30-2007 at 12:43 AM.
    <div> - putting your mind in a box since 1997

  • #2
    New Coder
    Join Date
    Jul 2006
    Posts
    44
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Do you have the right $_POST key? I noticed the top $EmailFrom came from $_POST['EmailFrom'] while the bottom $EmailFrom came from $_POST['email'].

    Good luck!

  • #3
    Regular Coder ArcticFox's Avatar
    Join Date
    Jan 2004
    Location
    Vostok Station, AQ
    Posts
    602
    Thanks
    35
    Thanked 3 Times in 3 Posts
    LOL. I'm not sure what that means - it was two scripts that I tried to combine then remove parts I didn't need.

    Everything that was $email was changed to $EmailFrom. Are you suggesting I change email to EmailFrom as well, 'cause that didn't work.

    The form
    Code:
    <div style="text-align:justify; font-size:14px;">
    <script language="Javascript">
    <!--//
    when = new Date
    
    if (when.getHours() < 9) {
        document.write("Good morning! What can I do for you?")
    }
    else if (when.getHours() < 17) {
        document.write("Good afternoon! What can I do for you today?")
    }
    else {
        document.write("Good evening! What can I do for you tonight?")
    }
    //-->
    </script>
    <form method="POST" action="submitted.php" class="button">
    
    
    <p>Comments:<span style="color:red">*</span> <br>
    <textarea name="Comments" cols=38 rows=5></TEXTAREA>
    
    
    <p>Your Email Address:<span style="color:red">*</span><br>
    <input type="text" name="EmailFrom">
    <p>Your Name:<br>
    <input type="text" name="Name">
    <p>Phone Number:<br>
    <input type="text" name="Tel">
    <br><br>
    
    <br><small style="color:red">Fields marked (*) are required</small>
    <p><input type="submit" name="submit" value="  Send It !  ">
    </form>
    <p>
    </div>
    Last edited by ArcticFox; 01-30-2007 at 12:43 AM.
    <div> - putting your mind in a box since 1997

  • #4
    New Coder
    Join Date
    Jul 2006
    Posts
    44
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yes. Change this line
    Code:
    $EmailFrom = trim($_POST['email']);
    to this
    Code:
    $EmailFrom = trim($_POST['EmailFrom']);

  • #5
    Regular Coder ArcticFox's Avatar
    Join Date
    Jan 2004
    Location
    Vostok Station, AQ
    Posts
    602
    Thanks
    35
    Thanked 3 Times in 3 Posts
    Nope. Still doing the same thing.
    <div> - putting your mind in a box since 1997

  • #6
    New Coder
    Join Date
    Jul 2006
    Posts
    44
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Are you getting any errors? Do you have URL we can take a look at?

  • #7
    Regular Coder ArcticFox's Avatar
    Join Date
    Jan 2004
    Location
    Vostok Station, AQ
    Posts
    602
    Thanks
    35
    Thanked 3 Times in 3 Posts
    No errors, it just goes directly to the oops.php page.

    All the pages are in .php - changed them to .txt so you can take a look:

    http://www.basestationzero.com/contact.txt
    http://www.basestationzero.com/submitted.txt
    http://www.basestationzero.com/oops.txt
    http://www.basestationzero.com/thanks.txt
    <div> - putting your mind in a box since 1997

  • #8
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,048
    Thanks
    2
    Thanked 317 Times in 309 Posts
    Your checkEmail function contains multiple tests that all return false upon failure. There is no way to know which one failed. The proper way would be to return unique values for each test so that you can take appropriate action in the program and print out a meaningful error message.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #9
    New Coder
    Join Date
    Jul 2006
    Posts
    44
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Try using this this function in place of your checkEmail function (found here):
    PHP Code:
    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;


  • #10
    Regular Coder ArcticFox's Avatar
    Join Date
    Jan 2004
    Location
    Vostok Station, AQ
    Posts
    602
    Thanks
    35
    Thanked 3 Times in 3 Posts

    Question

    I looked at that, but didn't think I wanted to use it. After looking through it again, LOL, I've decided to combine that and the one I can't get to work... which I almost got to work now.


    I get this error only sometimes, seems like at random.
    Warning: fsockopen() [function.fsockopen]: unable to connect to yahoo.com:25 in /sites/basestationzero.com/htdocs/submitted.php on line 68

    Located:
    Code:
       function checkEmail($EmailFrom) {
         	
        	
        	
         // checks proper syntax
         if (check_email_address($EmailFrom)) {
           // gets domain name
           list($username,$domain)=split('@',$EmailFrom);
           // checks for if MX records in the DNS
             if(!checkdnsrr($domain, 'MX')) {
              echo "28<br>";ob_flush();flush();return false;
             }
            // attempts a socket connection to mail server
            if(!fsockopen($domain,25,$errno,$errstr,30)) {
             echo "32<br>";ob_flush();flush();return false;
            }
           echo "34<br>";ob_flush();flush();return true;
           }
         echo "36<br>";ob_flush();flush();return false;
         }

    I'm guessing that sometimes port 25 is not accessible?
    <div> - putting your mind in a box since 1997

  • #11
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,048
    Thanks
    2
    Thanked 317 Times in 309 Posts
    The receiving mail server(s) would be indicated by the MX records for a domain. There is no guarantee that any domain will have a mail server listening on port 25 at the same IP address as the domain.

    For example, yahoo has the following MX records (receiving mail servers) -

    1 g.mx.mail.yahoo.com. [TTL=7200] IP=206.190.53.191 [TTL=1800] [US]
    1 a.mx.mail.yahoo.com. [TTL=7200] IP=209.191.118.103 [TTL=1800] [US]
    1 b.mx.mail.yahoo.com. [TTL=7200] IP=66.196.97.250 [TTL=1800] [US]
    1 c.mx.mail.yahoo.com. [TTL=7200] IP=216.39.53.3 [TTL=1800] [US]
    1 d.mx.mail.yahoo.com. [TTL=7200] IP=216.39.53.2 [TTL=1800] [US]
    1 e.mx.mail.yahoo.com. [TTL=7200] IP=216.39.53.1 [TTL=1800] [US]
    1 f.mx.mail.yahoo.com. [TTL=7200] IP=68.142.202.247 [TTL=1800] [US]

    For your code to work properly, you would need to attempt an fsockopen at each of these address in turn, until you get a successful connection to at least one.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #12
    Regular Coder ArcticFox's Avatar
    Join Date
    Jan 2004
    Location
    Vostok Station, AQ
    Posts
    602
    Thanks
    35
    Thanked 3 Times in 3 Posts
    Is that possible to do? Seems like a lot of work. Maybe I should just stick with what I've put up so far.

    Is there a way for me to keep the php from throwing an error when it hits that point - like instead of an error, it could send a message along with the original posted email telling me it couldn't connect...
    <div> - putting your mind in a box since 1997


  •  

    Posting Permissions

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