View Full Version : email validation in php
ff123
05-13-2009, 02:39 PM
hey im trying to put email validation in php, i tried searching and the code i found does not work it shows 500 internal error here is my code
<?php
$to = "";
$subject = "subject";
$mail = $_POST['mail'];
if ($mail = $_POST['mail']) {
$body = "E-Mail: $mail";
echo "success";
mail($to, $subject, $body, "From: $mail");
} else {
echo "error";
}
?>
and here is the code im trying to add
http://www.go4expert.com/forums/showthread.php?t=1128
forum_amnesiac
05-13-2009, 03:14 PM
you can use this code to validate an email address
$result = preg_split('/\b[A-Z0-9._%+-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,4}\b/i', $email);
If $result is TRUE then proceed if FALSE then error
ff123
05-13-2009, 04:11 PM
and where should i use this $result?
forum_amnesiac
05-13-2009, 04:46 PM
This is where to use it, note the comment saying it can replace your 'if' statement.
<?php
$to = "";
$subject = "subject";
$mail = $_POST['mail'];
$result = preg_split('/\b[A-Z0-9._%+-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,4}\b/i', $mail);
if ($result){
if ($mail = $_POST['mail']) { /*Why this line it will always be true, you can replace it with the 'if' above*/
$body = "E-Mail: $mail";
echo "success";
mail($to, $subject, $body, "From: $mail");
} else {
echo "error";
}
?>
If you are submitting the email from an HTML form then personally I prefer to do any validation at the input stage.
It is fairly easy to use javascript to field validate a form
forum_amnesiac
05-13-2009, 05:18 PM
This test is probably better, it stops consecutive '.'
$result = preg_split('/[a-z0-9!#$%&\'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&\'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/i', $mail);
techietim
05-13-2009, 07:13 PM
When possible, you should use the filter extension over regex.
http://php.net/filter
ff123
05-14-2009, 03:40 AM
This is where to use it, note the comment saying it can replace your 'if' statement.
<?php
$to = "";
$subject = "subject";
$mail = $_POST['mail'];
$result = preg_split('/\b[A-Z0-9._%+-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,4}\b/i', $mail);
if ($result){
if ($mail = $_POST['mail']) { /*Why this line it will always be true, you can replace it with the 'if' above*/
$body = "E-Mail: $mail";
echo "success";
mail($to, $subject, $body, "From: $mail");
} else {
echo "error";
}
?>
If you are submitting the email from an HTML form then personally I prefer to do any validation at the input stage.
It is fairly easy to use javascript to field validate a form
thanks alot... well i was using javascript validate thingy, but i wanted with php.. thanks anyway
ff123
05-14-2009, 04:57 AM
hey again.. one more thing im using this for my contact, where should i put that email validation thing so it can even validate in here..
if ( $message = $message && $name = $name && $fone = $fone && $mail = $mail ){
$body = "From: $name\n E-Mail: $mail\n Phone: $fone\n Message:\n $message";
} else {
echo ""
}
forum_amnesiac
05-14-2009, 07:19 AM
Try this
$result = preg_split('/[a-z0-9!#$%&\'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&\'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/i', $mail);
if ($result){
if ( $message = $message && $name = $name && $fone = $fone && $mail = $mail ){
$body = "From: $name\n E-Mail: $mail\n Phone: $fone\n Message:\n $message";
} else {
echo ""
}
} else {
echo ""
}
It may easily be possible to write this neater but it should work
ff123
05-14-2009, 08:51 AM
thats cool mate.. it works but please tell me what is the main difference between
preg_match("/^[\.A-z0-9_\-]+[@][A-z0-9_\-]+([.][A-z0-9_\-]+)+[A-z]{1,4}$/", $mail
and
$result = preg_split('/[a-z0-9!#$%&\'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&\'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/i', $mail);
forum_amnesiac
05-14-2009, 08:59 AM
I posted earlier that this code stops someone from entering an email address with more than one consecutive full-stop, ie fred.blogs@him.com is OK, fred..blogs@him.com would fail
ff123
05-14-2009, 09:28 AM
that cool..
thank you!
Edit: and does anyone know how to get sender i.p address?
forum_amnesiac
05-14-2009, 10:04 AM
It is possible to get the IP address using this code.
$ip = $_SERVER['HTTP_CLIENT_IP'];
You do need to be careful however, in some countries storing a persons IP address or using it for identification purposes, is illegal.
I think Germany is one such country.
ff123
05-14-2009, 11:41 AM
It is possible to get the IP address using this code.
$ip = $_SERVER['HTTP_CLIENT_IP'];
You do need to be careful however, in some countries storing a persons IP address or using it for identification purposes, is illegal.
I think Germany is one such country.
hmmm its not getting the ip..
forum_amnesiac
05-14-2009, 01:29 PM
This link shows a bit more about this, it shows a function and how to use it.
http://andrewgatenby.com/getting-a-visitors-ip-address
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.