Name:
"/> Name:
">

Home  >  Article  >  Backend Development  >  Variables from outside php

Variables from outside php

伊谢尔伦
伊谢尔伦Original
2016-11-24 13:43:26921browse

HTML Forms (GET and POST)

When a form is submitted to a PHP script, the information in the form is automatically available in the script. There are many ways to access this information, for example:

Example #1 A simple HTML form

<form action="foo.php" method="POST">
Name:  <input type="text" name="username"><br />
Email: <input type="text" name="email"><br />
<input type="submit" name="submit" value="Submit me!" />
</form>

There are many ways to access data in an HTML form, depending on your specific setup and personal preference. For example:

Example #2 Accessing data from a simple POST HTML form

<?php
// 自 PHP 4.1.0 起可用echo $_POST[&#39;username&#39;];echo $_REQUEST[&#39;username&#39;];
import_request_variables(&#39;p&#39;, &#39;p_&#39;);echo $p_username;
// 自 PHP 5.0.0 起,这些长格式的预定义变量可用 register_long_arrays 指令关闭。
echo $HTTP_POST_VARS[&#39;username&#39;];
// 如果 PHP 指令 register_globals = on 时可用。不过自PHP 4.2.0 起默认值为 register_globals = off。// 不提倡使用/依赖此种方法。
echo $username;
?>

Using a GET form is similar, but with the appropriate GET predefined variables. GET also works with QUERY_STRING (the information after the "?" in the URL). So, for example, http://www.example.com/test.php?id=3 contains GET data that can be accessed with $_GET['id']. See $_REQUEST and import_request_variables().

Note:

Superglobal arrays such as $_POST and $_GET are available since PHP 4.1.0.

Note:

Dots and spaces in variable names are converted to underscores. For example, 66df9299ffe54b5001d938858b4213b8 becomes $_REQUEST["a_b"].

As shown above, the default value of register_globals before PHP 4.2.0 is on. The PHP community encourages everyone not to rely on this directive and recommends coding assuming it is off.

Note:

magic_quotes_gpc configuration directive affects the values ​​of Get, Post and Cookie. If on, the value (It's "PHP!") is automatically converted to (It's "PHP!"). Over a decade ago database inserts required such escaping, which is now obsolete and should be turned off.

PHP also understands arrays in the context of form variables. For example, you can group related variables, or use this feature to get values ​​from a multi-select input box. For example, POST a form to yourself and display data on submission:

Example #3 More complex form variables

<?php
if (isset($_POST[&#39;action&#39;]) && $_POST[&#39;action&#39;] == &#39;submitted&#39;) {
   echo &#39;<pre class="brush:php;toolbar:false">&#39;;
   print_r($_POST);
   echo &#39;<a href="&#39;. $_SERVER[&#39;PHP_SELF&#39;] .&#39;">Please try again</a>&#39;;
   echo &#39;
'; } else { ?>
Name:
Email:
Beer:

IMAGE SUBMIT variable name

When submitting a form, you can use an image instead of the standard submit button, Use markup like this:

6c1a602b0aa6d408023c4dd21a62a130

When the user clicks somewhere in the image, the corresponding form will be sent to the server and add two variables sub_x and sub_y. They contain the coordinates of the image the user clicked on. Experienced users may notice that the actual variable name sent by the browser contains a dot instead of an underscore (i.e. sub.x and sub.y), but PHP automatically converts the dot to an underscore.

HTTP Cookies

PHP transparently supports » HTTP cookies as defined in RFC 6265. Cookies are a mechanism that stores data on a remote browser and can track or identify users who visit again. Cookies can be set using the setcookie() function. Cookies are part of the HTTP headers, so the SetCookie function must be called before any output is sent to the browser. The same restriction applies to the header() function. Cookie data will be available in the corresponding cookie data array, such as $_COOKIE, $HTTP_COOKIE_VARS and $_REQUEST.

If you want to assign multiple values ​​​​to a cookie variable, you must assign it to an array. For example:

<?php
 setcookie("MyCookie[foo]", &#39;Testing 1&#39;, time()+3600);
 setcookie("MyCookie[bar]", &#39;Testing 2&#39;, time()+3600);
?>

This will create two separate cookies, even though MyCookie is a single array in the script. If you want to set multiple values ​​in just one cookie, consider using serialize() or explode() on the values ​​first.

Note that a cookie in the browser will replace the previous cookie with the same name, unless the path or domain is different. So for a shopping cart program you can keep a counter and pass it along, for example:

Example #4 An example of setcookie()

<?php
if (isset($_COOKIE[&#39;count&#39;])) {
   $count = $_COOKIE[&#39;count&#39;] + 1;
} else {
   $count = 1;
}
setcookie(&#39;count&#39;, $count, time()+3600);
setcookie("Cart[$count]", $item, time()+3600);
?>

The dot in the variable name

Normally, PHP will not change the variable name passed to the script. However, it should be noted that the period (period) is not a legal character in PHP variable names. As for the reason, take a look:

<?php
$varname.ext;  /* 非法变量名 */
?>

At this point, the parser sees a variable named $varname, followed by a string concatenation operator, followed by a bare string (that is, a string without quotes, and Does not match any known keyname or reserved word) 'ext'. Obviously this is not the desired result.

For this reason, be aware that PHP will automatically replace dots in variable names with underscores.

Determining Variable Types

Because PHP determines the variable type and converts it when needed (usually), it is not obvious what type a given variable is at a given moment. PHP includes several functions that can determine the type of variables, such as: gettype(), is_array(), is_float(), is_int(), is_object() and is_string().


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn