Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: What does this code do - getting blank emails

  1. #1
    Join Date
    May 2009
    Location
    Greensboro, GA
    Posts
    163
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default What does this code do - getting blank emails

    I have many forms that use the php code below (found on the web somewhere). In my html code, all the fields have validation so users CANNOT send emails with blank fields. I think I'm betting these emails with blank fields from bots looking at the .php files and somehow sending blank emails.

    What does the "preg_match" code do. Is there a better way to do this. Is there a way to stop the blank emails. What does the "header" line do.
    Thanks for ANY help.

    PHP Code:
    if (preg_match(' /[\r\n,;\'"]/ '$_POST['email'])) {
      exit(
    'Invalid Email Address');
    }
    else {
    mail($to,$subject,$message,$headers);
    mail($to2,$subject,$message2,$headers);
    header("Location: http://www.lotatennis.com");


  2. #2
    Join Date
    Nov 2008
    Posts
    58
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Default

    Client side validation is not sufficient.

    Server side validation is required.
    A basic PHP validation goes like this
    PHP Code:
    if(empty($_POST['email'])))
    {
       exit(
    'Email is required');

    preg_match in your code is searching for certain characters(\n\r) in the input, in an attempt to prevent email injection.
    The header() function redirects to the home page

    The following pages might be helpful:
    PHP Form to email

    Server side PHP form validation

    PHP form processing

  3. #3
    Join Date
    May 2009
    Location
    Greensboro, GA
    Posts
    163
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default

    So you think I should replace the "preg_match" line with:
    if(empty($_POST['email'])))

    BTW, that line has 2 left parentheses and 3 right ones.

  4. #4
    Join Date
    Apr 2009
    Location
    Cognac, France
    Posts
    400
    Thanks
    2
    Thanked 57 Times in 57 Posts

    Default

    No you need to keep the preg_match line to reduce the risk of email injection.

    There should only be 2 right parentheses on the 'empty'.

    Both of these are server side validation, the 'empty' is checking that there is something in the email field although it doen't particularly care what.

    You could add this to your code to check that it is a valid email address:
    PHP Code:
    $email=trim($_POST['email']);
    if (!
    preg_match('/\b[A-Z0-9._%+-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,4}\b/im'$email')) {
       exit(' 
    Valid Email address is required'); 


  5. #5
    Join Date
    May 2009
    Location
    Greensboro, GA
    Posts
    163
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default

    Sorry amnesiac I'm confused. Should I use your "preg_match" code instead of the one I originally had.
    Does that take care of empty email addresses also.
    Is there 1 or more ' missing.

  6. #6
    Join Date
    Apr 2009
    Location
    Cognac, France
    Posts
    400
    Thanks
    2
    Thanked 57 Times in 57 Posts

    Default

    The preg_match that I added validates that $email is in a valid email format, it is not a replacement for the original.

    The original preg_match is there to reduce the risk of email injection, if this is not a term you know than look it up with your search engine.

    This is what the validation should look like in your PHP:

    PHP Code:
    $email=trim($_POST['email']);

    if (
    preg_match(' /[\r\n,;\'"]/ '$email)) {
      exit(
    'Invalid Email Address');

    } else if (!
    preg_match('/\b[A-Z0-9._%+-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,4}\b/im'$email)) {
       exit(
    ' Valid Email address format required'); 

    } else {

    mail($to,$subject,$message,$headers);
    mail($to2,$subject,$message2,$headers);
    header("Location: http://www.lotatennis.com");

    There is no need to do the empty() test, an empty value in $email will not pass the email format test.

    Sorry about the problem with the ', this code should now have the correct number of them

  7. #7
    Join Date
    Sep 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    merci

  8. #8
    Join Date
    May 2009
    Location
    Greensboro, GA
    Posts
    163
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default

    Sorry guys. It didn't work. I uploaded it and filled out a form. I hit submit and got the
    "Valid Email address format required" message. The email was my correct email.
    Last edited by mcolton; 09-28-2009 at 11:16 AM.

  9. #9
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    A suggestion/question: is it better to use a preg_match instead of the php email filter?

    PHP Code:
    $validEmail filter_var($emailFILTER_VALIDATE_EMAIL

  10. #10
    Join Date
    Apr 2009
    Location
    Cognac, France
    Posts
    400
    Thanks
    2
    Thanked 57 Times in 57 Posts

    Default

    mcolton - what was the email address that you used, I use this routine myself and it definitely works.

    I can send you some code to test the routine if you want

    traq - I use preg_match out of habit and also because the PHP Filter_Validate_Email used to be vulnerable to email injection, although I believe that is now sorted. Old habits died hard!

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
  •