📄 language.variables.external.html
字号:
</p> <div class="informalexample"> <div class="example-contents"><div class="cdata"><pre><input type="image" src="image.gif" name="sub" /></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, sub_x and sub_y. 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 id="language.variables.external.cookies" class="sect2"> <h3 class="title">HTTP Cookies</h3> <p class="simpara"> PHP transparently supports HTTP cookies as defined by <a href="http://cgi.netscape.com/newsref/std/cookie_spec.html" class="link external">» Netscape's Spec</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 <a href="function.setcookie.html" class="function">setcookie()</a> 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 <a href="function.header.html" class="function">header()</a> function. Cookie data is then available in the appropriate cookie data arrays, such as <var class="varname"><a href="reserved.variables.cookies.html" class="classname">$_COOKIE</a></var>, <var class="varname">$HTTP_COOKIE_VARS</var> as well as in <var class="varname"><a href="reserved.variables.request.html" class="classname">$_REQUEST</a></var>. See the <a href="function.setcookie.html" class="function">setcookie()</a> 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"><?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">?></span></span></code></div> </div> </div> <p class="simpara"> That will create two separate cookies although MyCookie will now be a single array in your script. If you want to set just one cookie with multiple values, consider using <a href="function.serialize.html" class="function">serialize()</a> or <a href="function.explode.html" class="function">explode()</a> 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"> <p><b>Example #4 A <a href="function.setcookie.html" class="function">setcookie()</a> example</b></p> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?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[$count]"</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">?></span></span></code></div> </div> </div> </div> <div id="language.variables.external.dot-in-names" class="sect2"> <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"><?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">?></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't match any known key or reserved words) 'ext'. Obviously, this doesn'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 id="language.variables.determining-type-of" class="sect2"> <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: <a href="function.gettype.html" class="function">gettype()</a>, <a href="function.is-array.html" class="function">is_array()</a>, <a href="function.is-float.html" class="function">is_float()</a>, <a href="function.is-int.html" class="function">is_int()</a>, <a href="function.is-object.html" class="function">is_object()</a>, and <a href="function.is-string.html" class="function">is_string()</a>. See also the chapter on <a href="language.types.html" class="link">Types</a>. </p> </div> </div><hr /><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="language.variables.html">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>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -