Question

I can get it to validate my email address but it isn't sanitizing it, for example when I do a simple test and input an email such as joeschm//oe@yahoo.com into the email address input field, I receive an email that says the email address is from joeschm//oe@yahoo.com.

Here is the relevant code:

//form validation vars
$formok = true;
$errors = array();  

//form data
$email = $_POST['email'];

//validate email address is not empty
if(empty($email)){
    $formok = false;
    $errors[] = "You have not entered an email address";
//validate email address is valid
}elseif(!filter_var($email, FILTER_VALIDATE_EMAIL)){
    $formok = false;
    $errors[] = "You have not entered a valid email address";
    //if email address input field is not empty and the email is valid, sanitize the email
}else{
    filter_var($email, FILTER_SANITIZE_EMAIL);
    $formok = true;
}
Was it helpful?

Solution

$email = filter_var($email, FILTER_SANITIZE_EMAIL);

You need to capture the return value of filter_var.

OTHER TIPS

$email = mysqli_real_escape_string($_POST['email']);

Try using that instead of your current code. It will not replace any mishaps the user puts into the email input but it will make sure it is safe to post to your database for saving.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top