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:
[php]&& filter_var($_REQUEST[‘email’], FILTER_VALIDATE_EMAIL)[/php]
[php] if (!filter_var($_REQUEST[‘email’], FILTER_VALIDATE_EMAIL)) {
$desc = $desc.‘Email is not valid.’;
}[/php]
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.
[php]<?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( "[email protected]",'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]");
}
}
?>[/php]