Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8679125
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:53:35+00:00 2026-06-12T20:53:35+00:00

I am trying to prevent mail header injection by looping through all the _POST

  • 0

I am trying to prevent mail header injection by looping through all the _POST data but the variables are not passing my validation. I have a few “date” fields that include slashes and some of the fields might be blank. Not sure if that would have anything to do with it. Can anyone see the problem with my logic? I keep getting my “fail” message. Thanks.

if(isset($_POST['submit'])) {

    $boolValidateOK = 1;

    function safe( $name ) {
       return( str_ireplace(array( "\r", "\n", "%0a", "%0d", "Content-Type:", "bcc:","to:","cc:" ), "", $name ) );
    }

    foreach($_POST as $value){
        if(!safe($value)){
            $boolValidateOK = 0;
        }else{
            $boolValidateOK = 1;
        }
    }

    if($boolValidateOK == 1){

        $headers  = 'MIME-Version: 1.0' . "\r\n";
        $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
        $to...etc
        $subject...etc
        $message...etc

        mail($to, $subject, $message, $headers);

        $success_message = "win";
    }else{
        $error_message = "fail";
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T20:53:37+00:00Added an answer on June 12, 2026 at 8:53 pm

    What you’re doing is unnecessary. Note PHP’s mail() function signature:

    bool mail ( string $to , string $subject , string $message 
                  [, string $additional_headers [, string $additional_parameters ]] )
    

    The recipient is the second argument. The headers are all in the fourth argument. The contents of $message or $subject are not going to magically “spill over” to the other arguments.

    Since your $headers doesn’t depend on user input, it doesn’t really matter if the user has entered. No such “injection” is possible. All you’re doing is butchering the message.

    PS: This code is wrong:

    foreach($_POST as $value){
        if(!safe($value)){
            $boolValidateOK = 0;
        }else{
            $boolValidateOK = 1;
        }
    }
    
    if($boolValidateOK == 1){
    

    Think about it. Let’s say all your post values are not “safe,” but the last one iterated is. It will then overwrite $boolValidateOK to 1, and that’s the value it will retain by the time the loop has ended.

    Also, PHP does have a boolean type. If you have a bool, set them to true or false instead of 1 and 0. It’s clearer.


    Regardless, I would recommend using something like PHPMailer. The native mail() function is very awkward to use.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to prevent any SQL injection in all my queries and would
I have a flat file of e-mail header data that I'm trying to parse
Trying to prevent access to a specific file, not any files with a certain
I'm trying to prevent certain keys from being entered into an input box, but
I'm trying to prevent A HREF from actually opening the link, but to execute
I have been trying to send out html emails through a PHP mail script
I am trying to prevent from having to escape apostrophes in my string variables
, i'm trying to prevent SQL Injection , is my function enough when I'm
i am trying to prevent all elements within a div from being selected. this
Possible Duplicate: Best way to prevent SQL Injection in PHP I'm trying to insert

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.