Home > Article > Backend Development > what are variables from outside php
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['username']; echo $_REQUEST['username']; import_request_variables('p', 'p_'); echo $p_username;// 自 PHP 5.0.0 起,这些长格式的预定义变量 // 可用 register_long_arrays 指令关闭。 echo $HTTP_POST_VARS['username'];// 如果 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. See addslashes() , stripslashes() and magic_quotes_sybase.
PHP also understands arrays in the context of form variables (see related FAQ). 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 the data when submitted:
Example #3 More complex form variables
<?php if (isset( $_POST [ 'action' ]) && $_POST [ 'action' ] == 'submitted' ) { echo '<pre class="brush:php;toolbar:false">' ; print_r ( $_POST ); echo '<a href="' . $_SERVER [ 'PHP_SELF' ] . '">Please try again</a>' ; echo '' ; } else { ?>
IMAGE SUBMIT variable name
When the form is submitted , you can use an image instead of the standard submit button, with a tag like this:
<input type="image" src="image.gif" name="sub" />
When the user clicks somewhere in the image, the corresponding form will be sent to the server, and 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 restrictions apply to the header() function. Cookie data will be available in the corresponding cookie data array, such as $_COOKIE , $HTTP_COOKIE_VARS and $_REQUEST . See the setcookie() man page for more details and examples.
If you want to assign multiple values to a cookie variable, you must assign it to an array. For example:
<?php setcookie ( "MyCookie[foo]" , 'Testing 1' , time ()+ 3600 ); setcookie ( "MyCookie[bar]" , 'Testing 2' , 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 will replace the previous cookie with the same name in the browser, unless the path or domain is different. Therefore, the shopping cart program can retain a counter and pass it together, for example:
Example #4 An example of setcookie()
<?php if (isset( $_COOKIE [ 'count' ])) { $count = $_COOKIE [ 'count' ] + 1 ; } else { $count = 1 ; } setcookie ( 'count' , $count , time ()+ 3600 ); setcookie ( "Cart[ $count ]" , $item , time ()+ 3600 ); ?>
变量名中的点
通常,PHP 不会改变传递给脚本中的变量名。然而应该注意到点(句号)不是 PHP 变量名中的合法字符。至于原因,看看:
<?php $varname . ext ; ?>
这时,解析器看到是一个名为 $varname 的变量,后面跟着一个字符串连接运算符,后面跟着一个裸字符串(即没有加引号的字符串,且不匹配任何已知的健名或保留字)'ext'。很明显这不是想要的结果。
出于此原因,要注意 PHP 将会自动将变量名中的点替换成下划线。
确定变量类型
因为 PHP 会判断变量类型并在需要时进行转换(通常情况下),因此在某一时刻给定的变量是何种类型并不明显。PHP 包括几个函数可以判断变量的类型,例如: gettype() , is_array() , is_float() , is_int() , is_object()和 is_string()
The above is the detailed content of what are variables from outside php. For more information, please follow other related articles on the PHP Chinese website!