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

  • Home
  • SEARCH
  • 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 7001335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:47:24+00:00 2026-05-27T20:47:24+00:00

Scenario : My PHP script requires 10 POST strings to work. The value of

  • 0

Scenario: My PHP script requires 10 POST strings to work. The value of all of them needs to be escaped with htmlspecialchars(). So the first lines of the script look like this:

$var1 = htmlspecialchars($_POST['var1']);
$var2 = htmlspecialchars($_POST['var2']);
// And more. You get the point.

This is some code that could simplify it:

foreach($_POST as $key => $value){
    $$key = htmlspecialchars($_POST[$value]);
}

I’m unsure about the $$ with user input. I guess somebody could send many POST requests I don’t need and block the server with that. Is this realistic?

The foreach code would be at the very top of my script. So it won’t be able to overwrite any other variables.

  • 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-05-27T20:47:25+00:00Added an answer on May 27, 2026 at 8:47 pm

    Rather than just blindly handling everything in $_POST (although just passing them through htmlspecialchars() is pretty harmless), you can use a whitelist of keys that are acceptable:

    // An array of $_POST keys that are acceptable
    $whitelist = array('var1','var2','var3');
    
    foreach($_POST as $key => $value) {
       // Only handle $_POST keys you expect to receive...
       if (in_array($key, $whitelist)) {
          $$key = htmlspecialchars($_POST[$value]);
        }
    }
    

    This evades the possibility of a malicious user submitting hundreds of values to POST and consuming extra system resources.

    Update

    Commenters are correct. It is better to iterate through the whitelist than $_POST:

    // Iterate over $whitelist and check for corresponding keys in $_POST
    $missing_keys = array();
    foreach($whitelist as $key) {
       if (isset($_POST[$key])) {
         $$key = htmlspecialchars($_POST[$key]);
       }
       else $missing_keys[] = $key;
    }
    echo "Missing keys: " . implode(",", $missing_keys);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Scenario: I have a php page in which I call a python script. Python
Scenario: I have got a client, php script and a MySQL database. I am
Let's say I have the following scenario: <form action=/something.php method=GET>Click me</div> <script type=text/javascript><!-- $('form').submit(function(e)
Hi I need some help with the following scenario in php. I have a
Scenario : I have a PHP Forum and a Asp.Net site hosted on Linux
Scenario :- I have to call MYSQL stored procedure from PHP and do some
I am putting my php /mysql website up and this is my scenario The
I have the following scenario jQuery('#content').fadeOut('slow',function(){ jQuery('#content').load(detail.php?product=+imgID+&category=+cat); jQuery('#content').fadeIn('fast'); }); My problem however is that
I have the following scenario. I have a index.php page with the following JQuery
Scenario: A stored procedure receives from code a DateTime with, let's say DateTime.Now value,

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.