[Phpwm] Simple PHP Contact Form

Alex Mace alex at hollytree.co.uk
Wed Oct 14 09:45:52 UTC 2009


I personally would always use the filter functions for validation  
nowadays. To get the hostname I'd explode the email address on @ and  
pass the resultant array into array_pop to return the last element...

Alex

On 14 Oct 2009, at 10:40, Pete Graham wrote:

> I'd only found out about the filter_var function recently. I curious
> if general consensus is to now use this for validation instead of
> regular expressions. Below is the function that I use for email
> validation:
>
> function _email($email) {
> 	if (preg_match ( '/^\w[-.\w]*@(\w[-._\w]*\.[a-zA-Z]{2,}.*)$/',
> $email, $matches )) {
> 		if (function_exists ( 'checkdnsrr' )) {
> 			if (checkdnsrr ( $matches [1] . '.', 'MX' ))
> 			return true; // If a 'mail exchange' record exists
> 			if (checkdnsrr ( $matches [1] . '.', 'A' ))
> 			return true; // Mail servers can fall back on 'A' records
> 		}
> 		else {
> 			return true; // For Windows
> 		}
> 	}
> 	return false;
> }
>
> Would I be better off swapping the regular expression for this:
> filter_var($email, FILTER_VALIDATE_EMAIL)
> If so should what would then be the best method to get the host name
> for the dns check, or is the dns check overkill?
>
> Thanks,
> Pete
>
> 2009/10/14 Alex Mace <alex at hollytree.co.uk>:
>> I think the point was there is no point writing and maintaining a
>> regex for email address validation when PHP's built in filter
>> functions have an email address checker will validate email addresses
>> correctly against the spec.
>>
>> Alex
>>
>> On 14 Oct 2009, at 09:35, <phil at infolinkelectronics.co.uk> <phil at infolinkelectronics.co.uk
>>  > wrote:
>>
>>>
>>>
>>> Regards,
>>>
>>> Phil Beynon
>>> Sales & Operations Director
>>>
>>> ** Infolink Electronic Systems Ltd.
>>> ** S/W Birmingham's Longest Established ISV & VAR
>>> ** http://www.infolinkelectronics.co.uk
>>> ** Professional Web 2.0 Design & Advanced Hosting Platforms
>>> ** http://www.coeus-computica.co.uk
>>> ** Affordable Knowledgebase Solutions
>>> ** Contact: Sales at infolinkelectronics.co.uk
>>> ** Tel / Fax 0121 441 3558
>>>
>>>
>>>>> if (!eregi ("^([a-z0-9_]|\\-|\\.)+@(([a-z0-9_]|\\-)+\\.)+[a-z]
>>>>> {2,4}$",
>>>>> $emaila)) error_alert("Invalid Email Address");
>>>>
>>>> My understanding is that there are a million and one regexps to
>>>> validate email addresses and it'd not the right one unless it is
>>>> very,
>>>> very long.
>>>>
>>>> Yours will fail on eg adresses which look like foo.o'reilly at ....
>>>> (valid) or foo+bar at ... (valid).
>>>
>>> Apostrophes in teh first part are also likely to break other things
>>> as they
>>> can be seen as a delimiter. Many ISPs thus limit their use.
>>>
>>>> And it allows an _ in a domain name which isn't possible. I think
>>>> there are also tlds longer than 4 characters.
>>>
>>> Yup there can be trade offs, it will fail on say .museum addresses,
>>> however
>>> the only real way to do that is look up the tld against a definitive
>>> list
>>> It has to be said when I wrote the code originally I doubt many > 3
>>> char
>>> TLDs actually did exist! :-)
>>>
>>>> And ereg is deprecated, as has already been pointed out. Use
>>>> preg_match
>>>
>>> I only actually got that pointed out to me last night by Alex :-)
>>>
>>>> You'd have made the regexp easier to write in ' rather than " -
>>>> avoiding \\ escaping blah blah too i think.
>>>
>>> Yes but if I had made it perfect as per RFC 2822 it would have
>>> looked like;
>>>
>>> (?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]
>>> +)*|"(?:[\x01
>>> -\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c
>>> \x0e-\x7f]
>>> )*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-
>>> z0-9-]*[a-z0-9])
>>> ?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|
>>> 2[0-4][0-9]|
>>> [01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f
>>> \x21-\x5a\
>>> x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])
>>>
>>> So yes mine is a lot shorter and is a compromise in some respects -
>>> but it
>>> pretty much works in real world environments!
>>>
>>>> Sorry if I sound bitchy,
>>>
>>> Dont worry about it.
>>>
>>> Phil
>>> <Phil Beynon.vcf>_______________________________________________
>>> Phpwm mailing list
>>> Website : http://www.phpwm.org
>>> Twitter : http://www.twitter.com/phpwm
>>> Facebook: http://www.facebook.com/group.php?gid=2361609907
>>>
>>> Post to list: Phpwm at mailman.lug.org.uk
>>> Archive etc : https://mailman.lug.org.uk/mailman/listinfo/phpwm
>>
>>
>> _______________________________________________
>> Phpwm mailing list
>> Website : http://www.phpwm.org
>> Twitter : http://www.twitter.com/phpwm
>> Facebook: http://www.facebook.com/group.php?gid=2361609907
>>
>> Post to list: Phpwm at mailman.lug.org.uk
>> Archive etc : https://mailman.lug.org.uk/mailman/listinfo/phpwm
>>
>
> _______________________________________________
> Phpwm mailing list
> Website : http://www.phpwm.org
> Twitter : http://www.twitter.com/phpwm
> Facebook: http://www.facebook.com/group.php?gid=2361609907
>
> Post to list: Phpwm at mailman.lug.org.uk
> Archive etc : https://mailman.lug.org.uk/mailman/listinfo/phpwm




More information about the Phpwm mailing list