Is this mail() function safe from header injection?

前端 未结 3 1108
北荒
北荒 2020-12-10 04:31

I\'m building a simple contact form for a website. It does not connect to a database, it just sends the email. Will this code prevent spammers from using header injections?

3条回答
  •  借酒劲吻你
    2020-12-10 05:18

    IMHO, your code is not secure, as you miss \r and \n characters. filter_var() only kills those, if FILTER_SANITIZE_STRING is used in conjunction with FILTER_FLAG_STRIP_LOW, which will also filter out any characters below ASCII 32:

    $message= filter_var($_POST['Message'], 
                         FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW);
    

    Also, FILTER_VALIDATE_MAIL will return a true or false, which you also do not account for. I recommend to check out this excellent source for filter_var(), as the main PHP manual is very short on information.


    Update: As Alnitak pointed out, through the \n\n in the code, this actually doesn't matter.

提交回复
热议问题