1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
|
<HTML
><HEAD
><TITLE
>Variables from outside PHP</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.57"><LINK
REL="HOME"
TITLE="PHP Manual"
HREF="manual.html"><LINK
REL="UP"
TITLE="Variables"
HREF="language.variables.html"><LINK
REL="PREVIOUS"
TITLE="Variable variables"
HREF="language.variables.variable.html"><LINK
REL="NEXT"
TITLE="Constants"
HREF="language.constants.html"><META
NAME="HTTP_EQUIV"
CONTENT="text/html; charset=ISO-8859-1"></HEAD
><BODY
CLASS="sect1"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
><DIV
CLASS="NAVHEADER"
><TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>PHP Manual</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="language.variables.variable.html"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
>Chapter 7. Variables</TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="language.constants.html"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="sect1"
><H1
CLASS="sect1"
><A
NAME="language.variables.external"
>Variables from outside PHP</A
></H1
><DIV
CLASS="sect2"
><H2
CLASS="sect2"
><A
NAME="language.variables.external.form"
>HTML Forms (GET and POST)</A
></H2
><P
> When a form is submitted to a PHP script, any variables from that
form will be automatically made available to the script by
PHP. If the <A
HREF="configuration.html#ini.track-vars"
>track_vars</A
>
configuration option is turned on, then these variables will be
located in the associative arrays
<TT
CLASS="varname"
>$HTTP_POST_VARS</TT
>,
<TT
CLASS="varname"
>$HTTP_GET_VARS</TT
>, and/or
<TT
CLASS="varname"
>$HTTP_POST_FILES</TT
>, according to the
source of the variable in question.
</P
><P
> For more information on these variables, please read <A
HREF="language.variables.predefined.html"
>Predefined
variables</A
>.
</P
><P
> <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN2830"
></A
><P
><B
>Example 7-1. Simple form variable</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="programlisting"
> <form action="foo.php" method="post">
Name: <input type="text" name="username"><br>
<input type="submit">
</form>
</PRE
></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
</P
><P
> When the above form is submitted, the value from the text input
will be available in
<TT
CLASS="varname"
>$HTTP_POST_VARS['username']</TT
>. If the <A
HREF="configuration.html#ini.register-globals"
>register_globals</A
>
configuration directive is turned on, then the variable will also
be available as <TT
CLASS="varname"
>$username</TT
> in the global scope.
</P
><P
> PHP also understands arrays in the context of form variables. You
may, for example, group related variables together, or use this
feature to retrieve values from a multiple select
input:
</P
><P
> <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN2839"
></A
><P
><B
>Example 7-2. More complex form variables</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="programlisting"
> <form action="array.php" method="post">
Name: <input type="text" name="personal[name]"><br>
Email: <input type="text" name="personal[email]"><br>
Beer: <br>
<select multiple name="beer[]">
<option value="warthog">Warthog
<option value="guinness">Guinness
<option value="stuttgarter">Stuttgarter Schwabenbru
</select>
<input type="submit">
</form>
</PRE
></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
</P
><P
> In PHP 3, the array form variable usage is limited to
single-dimensional arrays. In PHP 4, no such restriction applies.
</P
><DIV
CLASS="sect3"
><H3
CLASS="sect3"
><A
NAME="AEN2843"
>IMAGE SUBMIT variable names</A
></H3
><P
> 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"
><A
NAME="AEN2846"
></A
><P
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="programlisting"
> <input type=image src="image.gif" name="sub">
</PRE
></TD
></TR
></TABLE
><P
></P
></DIV
><P
> 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
CLASS="sect2"
><H2
CLASS="sect2"
><A
NAME="language.variables.external.cookies"
>HTTP Cookies</A
></H2
><P
> PHP transparently supports HTTP cookies as defined by <A
HREF="http://www.netscape.com/newsref/std/cookie_spec.html"
TARGET="_top"
>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 <B
CLASS="function"
>SetCookie()</B
> 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 <B
CLASS="function"
>Header()</B
> function. Any cookies
sent to you from the client will automatically be turned into a
PHP variable just like GET and POST method data.</P
><P
> If you wish to assign multiple values to a single cookie, just
add <I
CLASS="emphasis"
>[]</I
> to the cookie name. For
example:
</P
><DIV
CLASS="informalexample"
><A
NAME="AEN2857"
></A
><P
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="programlisting"
> SetCookie ("MyCookie[]", "Testing", time()+3600);
</PRE
></TD
></TR
></TABLE
><P
></P
></DIV
><P
> 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
><TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN2860"
></A
><P
><B
>Example 7-3. SetCookie Example</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="programlisting"
> $Count++;
SetCookie ("Count", $Count, time()+3600);
SetCookie ("Cart[$Count]", $item, time()+3600);
</PRE
></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
></DIV
><DIV
CLASS="sect2"
><H2
CLASS="sect2"
><A
NAME="language.variables.external.environment"
>Environment variables</A
></H2
><P
> PHP automatically makes environment variables available as normal
PHP variables.
<DIV
CLASS="informalexample"
><A
NAME="AEN2866"
></A
><P
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="programlisting"
> echo $HOME; /* Shows the HOME environment variable, if set. */
</PRE
></TD
></TR
></TABLE
><P
></P
></DIV
>
</P
><P
> Since information coming in via GET, POST and Cookie mechanisms
also automatically create PHP variables, it is sometimes best to
explicitly read a variable from the environment in order to make
sure that you are getting the right version. The
<A
HREF="function.getenv.html"
><B
CLASS="function"
>getenv()</B
></A
> function can be used for this. You
can also set an environment variable with the
<A
HREF="function.putenv.html"
><B
CLASS="function"
>putenv()</B
></A
> function.
</P
></DIV
><DIV
CLASS="sect2"
><H2
CLASS="sect2"
><A
NAME="AEN2871"
>Dots in incoming variable names</A
></H2
><P
> 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:
<TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="programlisting"
> $varname.ext; /* invalid variable name */
</PRE
></TD
></TR
></TABLE
>
Now, what the parser sees is a variable named
<TT
CLASS="varname"
>$varname</TT
>, 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
> 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"
><H2
CLASS="sect2"
><A
NAME="AEN2877"
>Determining variable types</A
></H2
><P
> 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. They are
<A
HREF="function.gettype.html"
><B
CLASS="function"
>gettype()</B
></A
>, <A
HREF="function.is-long.html"
><B
CLASS="function"
>is_long()</B
></A
>,
<A
HREF="function.is-double.html"
><B
CLASS="function"
>is_double()</B
></A
>, <A
HREF="function.is-string.html"
><B
CLASS="function"
>is_string()</B
></A
>,
<A
HREF="function.is-array.html"
><B
CLASS="function"
>is_array()</B
></A
>, and
<A
HREF="function.is-object.html"
><B
CLASS="function"
>is_object()</B
></A
>.
</P
></DIV
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="language.variables.variable.html"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="manual.html"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="language.constants.html"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Variable variables</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="language.variables.html"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Constants</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>
|