contact form - validate email

This is where you share YOUR scripts with others

Moderators: egami, macek, gesf

Post Reply
marchello
New php-forum User
New php-forum User
Posts: 3
Joined: Mon Oct 30, 2017 6:10 am

Mon Apr 09, 2018 2:17 am

Hi together,
Got contact form that works fine in general, I just tried to add email validation to reject emails like user@example (no domain after [at] symbol). These are lines I tried to use:

Code: Select all

&& filter_var($_REQUEST['email'], FILTER_VALIDATE_EMAIL)

Code: Select all

        if (!filter_var($_REQUEST['email'], FILTER_VALIDATE_EMAIL)) {
            $desc = $desc.'Email is not valid.';
        }
Actually it really disallows to send message, but it does not warn user in any way. The only difference is that in successful case it displays "thanks.php", but in unsuccessful case it just displays main page.
So.. please, be so kind, check code below and advise me how to display error to user.
Thanks ahead.

Code: Select all

<?php

function getUserIP()
{
    $client  = @$_SERVER['HTTP_CLIENT_IP'];
    $forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
    $remote  = $_SERVER['REMOTE_ADDR'];

    if(filter_var($client, FILTER_VALIDATE_IP))
    {
        $ip = $client;
    }
    elseif(filter_var($forward, FILTER_VALIDATE_IP))
    {
        $ip = $forward;
    }
    else
    {
        $ip = $remote;
    }

    return $ip;
}

        $user_ip = getUserIP();
        $user_agent = $_SERVER['HTTP_USER_AGENT'];
        $ref = @$_SERVER[HTTP_REFERER];
        $nl = nl2br("   |||||   ");

        $headerFields = array(
            "MIME-Version: 1.0",
            "Content-Type: text/html;charset=utf-8"
        );

        if (isset($_REQUEST['fullname']) && ($_REQUEST['fullname'] != '')
          && isset($_REQUEST['email']) && ($_REQUEST['email'] != '')
          && filter_var($_REQUEST['email'], FILTER_VALIDATE_EMAIL)
          && isset($_REQUEST['message']) && ($_REQUEST['message'] != '')

          && mail( "user@example.com",'example.com contact form', $user_ip.' '.$nl.' reference: '.$ref.' '.$nl.' '.$user_agent.' '.$nl.' '.$_REQUEST['fullname'].' email: '.$_REQUEST['email'].' mail: '.$_REQUEST['message'], implode("\r\n", $headerFields))) {

                echo '{"result": "sent", "desc": "Message was sent succesfully."}';

                // redirect

                if(! isset($_REQUEST['ajax'])){
                        $host  = $_SERVER['HTTP_HOST'];
                        $uri   = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
                        $extra = 'thanks.php';
                        header("Location: http://$host$uri/$extra");
                }


        } else {
          $desc = '';
          if (!isset($_REQUEST['fullname']) or ($_REQUEST['fullname'] == '')) {
          $desc = $desc.'Name field is empty.';
          }
          if (!isset($_REQUEST['email']) or ($_REQUEST['email'] == '')) {
          $desc = $desc.'Email field is empty.';
          }
        if (!filter_var($_REQUEST['email'], FILTER_VALIDATE_EMAIL)) {
            $desc = $desc.'Email is not valid.';
        }
       if (!isset($_REQUEST['message']) or ($_REQUEST['message'] == '')) {
          $desc = $desc.'Site field is empty.';
      }
          echo '{"result": "failed", "desc": "'.$desc.'"}';

                if(! isset($_REQUEST['ajax'])){
                        header("Location: $_SERVER[HTTP_REFERER]");
                }
  }
?>

marchello
New php-forum User
New php-forum User
Posts: 3
Joined: Mon Oct 30, 2017 6:10 am

Mon Apr 09, 2018 5:45 am

Seems I figured out how to solve it.

Code: Select all

if (!filter_var($_REQUEST['email'], FILTER_VALIDATE_EMAIL)) {                                                                                                                                                                                                          
             $desc = $desc.'Email is not valid.';                                                                                                                                                                                                                               
             $host  = $_SERVER['HTTP_HOST'];                                                                                                                                                                                                                                    
             $uri   = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');                                                                                                                                                                                                              
             $extra = 'error.php';                                                                                                                                                                                                                                              
             header("Location: http://$host$uri/$extra");                                                                                                                                                                                                                       
         } else
No clue how to echo the error to user, but I just redirect to the error page when entered email is not valid.
Will play with additional checks for email validity when time permits.

Post Reply