<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/security.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'uk',
  ),
  'this' => 
  array (
    0 => 'security.variables.php',
    1 => 'User Submitted Data',
    2 => 'User Submitted Data',
  ),
  'up' => 
  array (
    0 => 'security.php',
    1 => 'Безпека',
  ),
  'prev' => 
  array (
    0 => 'security.errors.php',
    1 => 'Error Reporting',
  ),
  'next' => 
  array (
    0 => 'security.hiding.php',
    1 => 'Hiding PHP',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'security/variables.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="security.variables" class="chapter">
 <h1 class="title">User Submitted Data</h1>

 <p class="para">
  The greatest weakness in many <abbr title="PHP: Hypertext Preprocessor">PHP</abbr> programs is not inherent in the
  language itself, but merely an issue of code not being written with
  security in mind. For this reason, you should always take the time
  to consider the implications of a given piece of code, to ascertain
  the possible damage if an unexpected variable is submitted to it.
  <div class="example" id="example-1">
   <p><strong>Приклад #1 Dangerous Variable Usage</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">// remove a file from the user's home directory... or maybe<br />// somebody else's?<br /></span><span style="color: #0000BB">unlink </span><span style="color: #007700">(</span><span style="color: #0000BB">$evil_var</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Write logging of their access... or maybe an /etc/passwd entry?<br /></span><span style="color: #0000BB">fwrite </span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">, </span><span style="color: #0000BB">$evil_var</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Execute something trivial.. or rm -rf *?<br /></span><span style="color: #0000BB">system </span><span style="color: #007700">(</span><span style="color: #0000BB">$evil_var</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">exec </span><span style="color: #007700">(</span><span style="color: #0000BB">$evil_var</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

  </div>
 </p>
 <p class="para">
  You should always carefully examine your code to make sure that any
  variables being submitted from a web browser are being properly
  checked, and ask yourself the following questions:
  <ul class="itemizedlist">
   <li class="listitem">
    <span class="simpara">
     Will this script only affect the intended files?
    </span>
   </li>
   <li class="listitem">
    <span class="simpara">
     Can unusual or undesirable data be acted upon?
    </span>
   </li>
   <li class="listitem">
   <span class="simpara">
     Can this script be used in unintended ways?
    </span>
   </li>
   <li class="listitem">
    <span class="simpara">
     Can this be used in conjunction with other scripts in a negative
     manner?
    </span>
   </li>
   <li class="listitem">
    <span class="simpara">
     Will any transactions be adequately logged?
    </span>
   </li>
  </ul>
 </p>
 <p class="para">
  By adequately asking these questions while writing the script,
  rather than later, you prevent an unfortunate re-write when you
  need to increase your security. By starting out with this mindset,
  you won&#039;t guarantee the security of your system, but you can help
  improve it.
 </p>
 <p class="para">
  Improve security by disabling convenience settings that obscure input
  data&#039;s origin, validity, or integrity. Implicit variable creation and
  unchecked input can lead to vulnerabilities like injection attacks and
  data manipulation.
 </p>
 <p class="para">
  Features like <code class="literal">register_globals</code> and
  <code class="literal">magic_quotes</code> (both removed in PHP 5.4.0) once contributed
  to these risks by automatically creating variables from user input and
  escaping data inconsistently. While no longer in PHP, similar risks persist
  if input handling is mismanaged.
 </p>
 <p class="para">
  Enable <a href="function.error-reporting.php" class="link">error_reporting(E_ALL)</a> to
  help detect uninitialized variables and validate input. Use strict types
  (<a href="language.types.declarations.php#language.types.declarations.strict" class="link">declare(strict_types=1)</a>,
  introduced in PHP 7) to enforce type safety, prevent unintended type conversions,
  and improving overall security.
 </p>
</div>
<?php manual_footer($setup); ?>