If a user enters an email address (and it validates), I think I am correct that it, in common with all user entered data, should be sanitized before outputting it in HTML in case the email address contains malicious code. I am confused as to whether using filter_var()
with the FILTER_SANITIZE_EMAIL
flag is considered a good way of doing this or whether that function is intended for some other purpose.
I would have thought that if an email validates but actually changes when sanitized with the above approach there would be a problem that now the email would be different when displayed than the valid email address that was entered by the user. If someone tried to use the displayed version to send an email it would presumably not be sent to the intended person.
In view of this why not just use htmlentities($email)
to display the validated email addresses which I think will display it as it was entered but safely as key characters like <
have been encoded with safe html entities?
If it were this simple I imagine filter_var
and FILTER_SANITIZE_EMAIL
would not be used so I would like to know if I am misunderstanding the situation or have missed some aspect which I should know about.
There is a related question, although about URLs, which is relevant and interesting but which does not address the issue about sanitization changing the actual value of the entered data. It is also very old and thinking on the subject may have changed.