Sophie

Sophie

distrib > Mageia > 4 > x86_64 > by-pkgid > f800694edefe91adea2624f711a41a2d > files > 8971

php-manual-en-5.5.7-1.mga4.noarch.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Variables From External Sources</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="language.variables.variable.html">Variable variables</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="language.constants.html">Constants</a></div>
 <div class="up"><a href="language.variables.html">Variables</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><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 many ways to access this information, for example:
    </p>

    <p class="para">
     <div class="example" id="example-108">
      <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">
     Depending on your particular setup and personal preferences, there 
     are many ways to access data from your HTML forms.  Some examples are:
    </p>

    
    <p class="para">
     <div class="example" id="example-109">
      <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&nbsp;<br /></span><span style="color: #FF8000">//&nbsp;Available&nbsp;since&nbsp;PHP&nbsp;4.1.0<br /><br />&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">$_POST</span><span style="color: #007700">[</span><span style="color: #DD0000">'username'</span><span style="color: #007700">];<br />&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$_REQUEST</span><span style="color: #007700">[</span><span style="color: #DD0000">'username'</span><span style="color: #007700">];<br /><br />&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">import_request_variables</span><span style="color: #007700">(</span><span style="color: #DD0000">'p'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'p_'</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$p_username</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;As&nbsp;of&nbsp;PHP&nbsp;5.0.0,&nbsp;these&nbsp;long&nbsp;predefined&nbsp;variables&nbsp;can&nbsp;be<br />//&nbsp;disabled&nbsp;with&nbsp;the&nbsp;register_long_arrays&nbsp;directive.<br /><br />&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">$HTTP_POST_VARS</span><span style="color: #007700">[</span><span style="color: #DD0000">'username'</span><span style="color: #007700">];<br /><br /></span><span style="color: #FF8000">//&nbsp;Available&nbsp;if&nbsp;the&nbsp;PHP&nbsp;directive&nbsp;register_globals&nbsp;=&nbsp;on.&nbsp;As&nbsp;of&nbsp;<br />//&nbsp;PHP&nbsp;4.2.0&nbsp;the&nbsp;default&nbsp;value&nbsp;of&nbsp;register_globals&nbsp;=&nbsp;off.<br />//&nbsp;Using/relying&nbsp;on&nbsp;this&nbsp;method&nbsp;is&nbsp;not&nbsp;preferred.<br /><br />&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">$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 you&#039;ll use the appropriate
     GET predefined variable instead. GET also applies to the
     <em>QUERY_STRING</em> (the information after the &#039;?&#039; in a URL).  So,
     for example, <em>http://www.example.com/test.php?id=3</em>
     contains GET data which is accessible with <var class="varname"><var class="varname"><a href="reserved.variables.get.html" class="classname">$_GET['id']</a></var></var>.
     See also <var class="varname"><var class="varname"><a href="reserved.variables.request.html" class="classname">$_REQUEST</a></var></var> and 
      <span class="function"><a href="function.import-request-variables.html" class="function">import_request_variables()</a></span>.
    </p>

    <blockquote class="note"><p><strong class="note">Note</strong>: 
     <p class="para">
      <a href="language.variables.superglobals.html" class="link">Superglobal arrays</a>, 
      like <var class="varname"><var class="varname"><a href="reserved.variables.post.html" class="classname">$_POST</a></var></var> and <var class="varname"><var class="varname"><a href="reserved.variables.get.html" class="classname">$_GET</a></var></var>, became 
      available in PHP 4.1.0
     </p>
    </p></blockquote>

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

    <p class="para">
     As shown, before PHP 4.2.0 the default value for <a href="ini.core.html#ini.register-globals" class="link">register_globals</a>
     was <em class="emphasis">on</em>.  The PHP 
     community is encouraging all to not rely on this directive 
     as it&#039;s preferred to assume it&#039;s <em class="emphasis">off</em> and code 
     accordingly.
    </p>

    <blockquote class="note"><p><strong class="note">Note</strong>: 
     <p class="para">
      The <a href="info.configuration.html#ini.magic-quotes-gpc" class="link">magic_quotes_gpc</a> 
      configuration directive affects Get, Post and Cookie values.  If 
      turned on, value (It&#039;s &quot;PHP!&quot;) will automagically become (It\&#039;s \&quot;PHP!\&quot;).
      It was deemed that this was needed for escaping for DB insertion circa a
      decade ago and is a legacy feature now that should be disabled. See also
       <span class="function"><a href="function.addslashes.html" class="function">addslashes()</a></span>,  <span class="function"><a href="function.stripslashes.html" class="function">stripslashes()</a></span> and 
      <a href="sybase.configuration.html#ini.magic-quotes-sybase" class="link">magic_quotes_sybase</a>.
     </p>
    </p></blockquote>
    
    <p class="simpara">
     PHP also understands arrays in the context of form variables 
     (see the <a href="faq.html.html" class="link">related faq</a>).  You may, 
     for example, group related variables together, or use this 
     feature 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-110">
      <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&nbsp;(</span><span style="color: #0000BB">$_POST</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'&lt;pre&gt;'</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</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">,&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">));<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</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&nbsp;action=""&nbsp;method="post"&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;Name:&nbsp;&nbsp;&lt;input&nbsp;type="text"&nbsp;name="personal[name]"&nbsp;/&gt;&lt;br&nbsp;/&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;Email:&nbsp;&lt;input&nbsp;type="text"&nbsp;name="personal[email]"&nbsp;/&gt;&lt;br&nbsp;/&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;Beer:&nbsp;&lt;br&nbsp;/&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&lt;select&nbsp;multiple&nbsp;name="beer[]"&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;option&nbsp;value="warthog"&gt;Warthog&lt;/option&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;option&nbsp;value="guinness"&gt;Guinness&lt;/option&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;option&nbsp;value="stuttgarter"&gt;Stuttgarter&nbsp;Schwabenbräu&lt;/option&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&lt;/select&gt;&lt;br&nbsp;/&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&lt;input&nbsp;type="submit"&nbsp;value="submit&nbsp;me!"&nbsp;/&gt;<br />&lt;/form&gt;</span>
</code></div>
      </div>

     </div>
    </p>

    <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"><var class="varname">sub_x</var></var> and <var class="varname"><var class="varname">sub_y</var></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="http://www.faqs.org/rfcs/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.  You can set cookies using
     the  <span class="function"><a href="function.setcookie.html" 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.html" class="function">header()</a></span> function.  Cookie data 
     is then available in the appropriate cookie data arrays, such 
     as <var class="varname"><var class="varname"><a href="reserved.variables.cookies.html" class="classname">$_COOKIE</a></var></var>, <var class="varname"><var class="varname">$HTTP_COOKIE_VARS</var></var> 
     as well as in <var class="varname"><var class="varname"><a href="reserved.variables.request.html" class="classname">$_REQUEST</a></var></var>.  See the 
      <span class="function"><a href="function.setcookie.html" class="function">setcookie()</a></span> manual page for more details and 
     examples.
    </p>

    <p class="simpara">
     If you wish to assign multiple values to a single cookie variable, you 
     may assign it 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 />&nbsp;&nbsp;setcookie</span><span style="color: #007700">(</span><span style="color: #DD0000">"MyCookie[foo]"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'Testing&nbsp;1'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">time</span><span style="color: #007700">()+</span><span style="color: #0000BB">3600</span><span style="color: #007700">);<br />&nbsp;&nbsp;</span><span style="color: #0000BB">setcookie</span><span style="color: #007700">(</span><span style="color: #DD0000">"MyCookie[bar]"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'Testing&nbsp;2'</span><span style="color: #007700">,&nbsp;</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"><var class="varname">MyCookie</var></var> will now 
     be a single array in your script.  If you want to set just one cookie 
     with multiple values, consider using  <span class="function"><a href="function.serialize.html" class="function">serialize()</a></span> or 
      <span class="function"><a href="function.explode.html" 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 your browser unless the path or domain is different.  So,
     for a shopping cart application you may want to keep a counter
     and pass this along.  i.e.
    </p>

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

     Now, what the parser sees is a variable named
     <var class="varname"><var class="varname">$varname</var></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.html" class="function">gettype()</a></span>,  <span class="function"><a href="function.is-array.html" class="function">is_array()</a></span>,
      <span class="function"><a href="function.is-float.html" class="function">is_float()</a></span>,  <span class="function"><a href="function.is-int.html" class="function">is_int()</a></span>,
      <span class="function"><a href="function.is-object.html" class="function">is_object()</a></span>, and
      <span class="function"><a href="function.is-string.html" class="function">is_string()</a></span>.  See also the chapter on 
     <a href="language.types.html" class="link">Types</a>.
    </p>
   </div>

  </div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="language.variables.variable.html">Variable variables</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="language.constants.html">Constants</a></div>
 <div class="up"><a href="language.variables.html">Variables</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>