<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/language.variables.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'en',
  ),
  'this' => 
  array (
    0 => 'language.variables.external.php',
    1 => 'Variables From External Sources',
    2 => 'Variables From External Sources',
  ),
  'up' => 
  array (
    0 => 'language.variables.php',
    1 => 'Variables',
  ),
  'prev' => 
  array (
    0 => 'language.variables.variable.php',
    1 => 'Variable variables',
  ),
  'next' => 
  array (
    0 => 'language.constants.php',
    1 => 'Constants',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'language/variables.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="language.variables.external" class="sect1">
   <h2 class="title">Variables From External Sources</h2>
   
   <div class="sect2" id="language.variables.external.form">
    <h3 class="title">HTML Forms (GET and POST)</h3>

    <p class="simpara">
     When a form is submitted to a PHP script, the information from 
     that form is automatically made available to the script. There 
     are few ways to access this information, for example:
    </p>

    <p class="para">
     <div class="example" id="example-1">
      <p><strong>Example #1 A simple HTML form</strong></p>
      <div class="example-contents">
<div class="htmlcode"><pre class="htmlcode">&lt;form action=&quot;foo.php&quot; method=&quot;post&quot;&gt;
    Name:  &lt;input type=&quot;text&quot; name=&quot;username&quot; /&gt;&lt;br /&gt;
    Email: &lt;input type=&quot;text&quot; name=&quot;email&quot; /&gt;&lt;br /&gt;
    &lt;input type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;Submit me!&quot; /&gt;
&lt;/form&gt;</pre>
</div>
      </div>

     </div>
    </p>

    <p class="para">
     There are only two ways to access data from HTML forms.
     Currently available methods are listed below:
    </p>

    <p class="para">
     <div class="example" id="example-2">
      <p><strong>Example #2 Accessing data from a simple POST HTML form</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: #007700">echo </span><span style="color: #0000BB">$_POST</span><span style="color: #007700">[</span><span style="color: #DD0000">'username'</span><span style="color: #007700">];<br />echo </span><span style="color: #0000BB">$_REQUEST</span><span style="color: #007700">[</span><span style="color: #DD0000">'username'</span><span style="color: #007700">];<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
      </div>

     </div>
    </p>

    <p class="para">
     Using a GET form is similar except the appropriate
     GET predefined variable can be used instead. GET also applies to the
     <code class="literal">QUERY_STRING</code> (the information after the &#039;?&#039; in a URL). So,
     for example, <code class="literal">http://www.example.com/test.php?id=3</code>
     contains GET data which is accessible with <var class="varname"><a href="reserved.variables.get.php" class="classname">$_GET['id']</a></var>.
     See also <var class="varname"><a href="reserved.variables.request.php" class="classname">$_REQUEST</a></var>.
    </p>

    <blockquote class="note"><p><strong class="note">Note</strong>: 
     <p class="para">
      Dots and spaces in variable names are converted to underscores. For
      example <code class="literal">&lt;input name=&quot;a.b&quot; /&gt;</code> becomes
      <code class="literal">$_REQUEST[&quot;a_b&quot;]</code>.
     </p>
    </p></blockquote>

    <p class="simpara">
     PHP also understands arrays in the context of form variables 
     (see the <a href="faq.html.php" class="link">related faq</a>).
     For example, related variables may be grouped together, or this
     feature may be used to retrieve values from a multiple select input. For
     example, let&#039;s post a form to itself and upon submission display 
     the data:
    </p>

    <p class="para">
     <div class="example" id="example-3">
      <p><strong>Example #3 More complex form variables</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: #007700">if (</span><span style="color: #0000BB">$_POST</span><span style="color: #007700">) {<br />    echo </span><span style="color: #DD0000">'&lt;pre&gt;'</span><span style="color: #007700">;<br />    echo </span><span style="color: #0000BB">htmlspecialchars</span><span style="color: #007700">(</span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$_POST</span><span style="color: #007700">, </span><span style="color: #0000BB">true</span><span style="color: #007700">));<br />    echo </span><span style="color: #DD0000">'&lt;/pre&gt;'</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;<br /></span>&lt;form action="" method="post"&gt;<br />    Name:  &lt;input type="text" name="personal[name]" /&gt;&lt;br /&gt;<br />    Email: &lt;input type="text" name="personal[email]" /&gt;&lt;br /&gt;<br />    Beer: &lt;br /&gt;<br />    &lt;select multiple name="beer[]"&gt;<br />        &lt;option value="warthog"&gt;Warthog&lt;/option&gt;<br />        &lt;option value="guinness"&gt;Guinness&lt;/option&gt;<br />        &lt;option value="stuttgarter"&gt;Stuttgarter Schwabenbräu&lt;/option&gt;<br />    &lt;/select&gt;&lt;br /&gt;<br />    &lt;input type="submit" value="submit me!" /&gt;<br />&lt;/form&gt;</span></code></div>
      </div>

     </div>
    </p>

    <blockquote class="note"><p><strong class="note">Note</strong>: 
     <span class="simpara">
      If an external variable name begins with a valid array syntax, trailing characters
      are silently ignored. For example, <code class="literal">&lt;input name=&quot;foo[bar]baz&quot;&gt;</code>
      becomes <code class="literal">$_REQUEST[&#039;foo&#039;][&#039;bar&#039;]</code>.
     </span>
    </p></blockquote>
 
    <div class="sect3" id="language.variables.external.form.submit">
     <h4 class="title">IMAGE SUBMIT variable names</h4>

     <p class="simpara">
      When submitting a form, it is possible to use an image instead
      of the standard submit button with a tag like:
     </p>

     <div class="informalexample">
      <div class="example-contents">
<div class="htmlcode"><pre class="htmlcode">&lt;input type=&quot;image&quot; src=&quot;image.gif&quot; name=&quot;sub&quot; /&gt;</pre>
</div>
      </div>

     </div>

     <p class="simpara">
      When the user clicks somewhere on the image, the accompanying
      form will be transmitted to the server with two additional
      variables, <var class="varname">sub_x</var> and <var class="varname">sub_y</var>.
      These contain the coordinates of the
      user click within the image. The experienced may note that the
      actual variable names sent by the browser contains a period
      rather than an underscore, but PHP converts the period to an
      underscore automatically.
     </p>
    </div>

   </div>

   <div class="sect2" id="language.variables.external.cookies">
    <h3 class="title">HTTP Cookies</h3>

    <p class="simpara">
     PHP transparently supports HTTP cookies as defined by <a href="https://datatracker.ietf.org/doc/html/rfc6265" class="link external">&raquo;&nbsp;RFC 6265</a>. Cookies are a
     mechanism for storing data in the remote browser and thus
     tracking or identifying return users. It is possible to set cookies using
     the <span class="function"><a href="function.setcookie.php" class="function">setcookie()</a></span> function. Cookies are part of
     the HTTP header, so the SetCookie function must be called before
     any output is sent to the browser. This is the same restriction
     as for the <span class="function"><a href="function.header.php" class="function">header()</a></span> function. Cookie data
     is then available in the appropriate cookie data arrays, such
     as <var class="varname"><a href="reserved.variables.cookies.php" class="classname">$_COOKIE</a></var> as well as in <var class="varname"><a href="reserved.variables.request.php" class="classname">$_REQUEST</a></var>.
     See the <span class="function"><a href="function.setcookie.php" class="function">setcookie()</a></span> manual page for more details and 
     examples.
    </p>

    <blockquote class="note"><p><strong class="note">Note</strong>: 
     <span class="simpara">
      As of PHP 7.2.34, 7.3.23 and 7.4.11, respectively, the <em>names</em>
      of incoming cookies are no longer url-decoded for security reasons.
     </span>
    </p></blockquote>

    <p class="simpara">
     If multiple values should be assigned to a single cookie variable,
     they can be assigned as an array. For example:
    </p>

    <div class="informalexample">
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />  setcookie</span><span style="color: #007700">(</span><span style="color: #DD0000">"MyCookie[foo]"</span><span style="color: #007700">, </span><span style="color: #DD0000">'Testing 1'</span><span style="color: #007700">, </span><span style="color: #0000BB">time</span><span style="color: #007700">()+</span><span style="color: #0000BB">3600</span><span style="color: #007700">);<br />  </span><span style="color: #0000BB">setcookie</span><span style="color: #007700">(</span><span style="color: #DD0000">"MyCookie[bar]"</span><span style="color: #007700">, </span><span style="color: #DD0000">'Testing 2'</span><span style="color: #007700">, </span><span style="color: #0000BB">time</span><span style="color: #007700">()+</span><span style="color: #0000BB">3600</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
     </div>

    </div>
    
    <p class="simpara">
     That will create two separate cookies although <var class="varname">MyCookie</var> will now 
     be a single array in the script. If just one cookie should be set
     with multiple values, consider using <span class="function"><a href="function.serialize.php" class="function">serialize()</a></span> or
     <span class="function"><a href="function.explode.php" class="function">explode()</a></span> on the value first.
    </p>

    <p class="simpara">
     Note that a cookie will replace a previous cookie by the same
     name in the browser unless the path or domain is different. So,
     for a shopping cart application a counter may be kept,
     and passed along. I.e.
    </p>

    <div class="example" id="example-4">
     <p><strong>Example #4 A <span class="function"><a href="function.setcookie.php" class="function">setcookie()</a></span> example</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: #007700">if (isset(</span><span style="color: #0000BB">$_COOKIE</span><span style="color: #007700">[</span><span style="color: #DD0000">'count'</span><span style="color: #007700">])) {<br />    </span><span style="color: #0000BB">$count </span><span style="color: #007700">= </span><span style="color: #0000BB">$_COOKIE</span><span style="color: #007700">[</span><span style="color: #DD0000">'count'</span><span style="color: #007700">] + </span><span style="color: #0000BB">1</span><span style="color: #007700">;<br />} else {<br />    </span><span style="color: #0000BB">$count </span><span style="color: #007700">= </span><span style="color: #0000BB">1</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">setcookie</span><span style="color: #007700">(</span><span style="color: #DD0000">'count'</span><span style="color: #007700">, </span><span style="color: #0000BB">$count</span><span style="color: #007700">, </span><span style="color: #0000BB">time</span><span style="color: #007700">()+</span><span style="color: #0000BB">3600</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">setcookie</span><span style="color: #007700">(</span><span style="color: #DD0000">"Cart[</span><span style="color: #0000BB">$count</span><span style="color: #DD0000">]"</span><span style="color: #007700">, </span><span style="color: #0000BB">$item</span><span style="color: #007700">, </span><span style="color: #0000BB">time</span><span style="color: #007700">()+</span><span style="color: #0000BB">3600</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
     </div>

    </div>

   </div>

   <div class="sect2" id="language.variables.external.dot-in-names">
    <h3 class="title">Dots in incoming variable names</h3>

    <p class="para">
     Typically, PHP does not alter the names of variables when they
     are passed into a script. However, it should be noted that the
     dot (period, full stop) is not a valid character in a PHP
     variable name. For the reason, look at it:
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$varname</span><span style="color: #007700">.</span><span style="color: #0000BB">ext</span><span style="color: #007700">;  </span><span style="color: #FF8000">/* invalid variable name */<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
     </div>

     Now, what the parser sees is a variable named
     <var class="varname">$varname</var>, followed by the string concatenation
     operator, followed by the barestring (i.e. unquoted string which
     doesn&#039;t match any known key or reserved words) &#039;ext&#039;. Obviously,
     this doesn&#039;t have the intended result.
    </p>

    <p class="para">
     For this reason, it is important to note that PHP will
     automatically replace any dots in incoming variable names with
     underscores.
    </p>

   </div>

   <div class="sect2" id="language.variables.determining-type-of">
    <h3 class="title">Determining variable types</h3>

    <p class="para">
     Because PHP determines the types of variables and converts them
     (generally) as needed, it is not always obvious what type a given
     variable is at any one time. PHP includes several functions
     which find out what type a variable is, such as:
     <span class="function"><a href="function.gettype.php" class="function">gettype()</a></span>, <span class="function"><a href="function.is-array.php" class="function">is_array()</a></span>,
     <span class="function"><a href="function.is-float.php" class="function">is_float()</a></span>, <span class="function"><a href="function.is-int.php" class="function">is_int()</a></span>,
     <span class="function"><a href="function.is-object.php" class="function">is_object()</a></span>, and
     <span class="function"><a href="function.is-string.php" class="function">is_string()</a></span>. See also the chapter on
     <a href="language.types.php" class="link">Types</a>.
    </p>
    <p class="para">
     HTTP being a text protocol, most, if not all, content that comes in
     <a href="language.variables.superglobals.php" class="link">Superglobal arrays</a>, 
     like <var class="varname"><a href="reserved.variables.post.php" class="classname">$_POST</a></var> and <var class="varname"><a href="reserved.variables.get.php" class="classname">$_GET</a></var> will remain
     as strings. PHP will not try to convert values to a specific type.
     In the example below, <var class="varname"><a href="reserved.variables.get.php" class="classname">$_GET["var1"]</a></var> will contain the
     string &quot;null&quot; and <var class="varname"><a href="reserved.variables.get.php" class="classname">$_GET["var2"]</a></var>, the string &quot;123&quot;.
     <div class="example-contents">
<div class="cdata"><pre>
/index.php?var1=null&amp;var2=123
</pre></div>
      </div>

    </p>
   </div>

   <div class="sect2" id="language.variables.external.changelog">
    <h3 class="title">Changelog</h3>

    <p class="para">
     <table class="doctable informaltable">
      
       <thead>
        <tr>
         <th>Version</th>
         <th>Description</th>
        </tr>

       </thead>

       <tbody class="tbody">
        <tr>
         <td>7.2.34, 7.3.23, 7.4.11</td>
         <td>
          The <em>names</em> of incoming cookies are no longer url-decoded
          for security reasons.
         </td>
        </tr>

       </tbody>
      
     </table>

    </p>
   </div>

  </div><?php manual_footer($setup); ?>