: This article mainly introduces six methods for transferring values between php pages. For more information about PHP tutorials, see.
There are 6 methods for transferring values between php pages:
1. method for obtaining the pass-through value after PHP4
Generally, POST, GET, and COOKIE are common values in the page, so I will mainly introduce these types below. PHP4 uses arrays such as $ _ POST and $ _ GET to obtain the webpage data transfer value. PHP3.0 and later versions use arrays such as $ HTTP_POST_VARS and $ HTTP_GET_VARS. the specific code is as follows:
Echo $ _ POST ['dopost'];?> <Form action = "weste_net.php" method = "post" name = 'form1 'id = "form1"> <input type = "text" name = "dopost" value = "hello weste.net! "/> <Input type =" submit "name =" weste "value =" submit "/> </form>
2. methods for obtaining the pass-through value before the earlier version of PHP3
As mentioned above, you can use the arrays $ HTTP_POST_VARS, $ HTTP_GET_VARS, and $ HTTP_COOKIE_VARS to read data. In the old version of PHP, you can also use the $ dopost method to directly obtain the pass-through value. this method is relatively simple. in the following methods to get the value of a webpage, in fact, icech focuses on how to use $ dopost to obtain the value of a webpage.
3. modify the configuration of the PHP. ini file.
Another question is, why is PHP3 incompatible with PHP4 and later? In fact, a very important reason is that after PHP4, the register_globals in the PHP. ini file is set to Off by default. Therefore, you cannot use the $ dopost method to directly obtain the webpage value.
A simple way to view the value of register_globals
echo 'register_globals = ' . ini_get('register_globals');?>
If register_globals = Off, register_globals = or register_globals = 0
If register_globals = On, display register_globals = 1
Manually change register_globals to On. if the submitted form contains a variable named "dopost", you can use $ dopost in PHP to obtain the variable. However, if you are using a VM, it is still difficult to modify the php. ini file. Here is just a method.
4. use the import_request_variables function
The import_request_variables function is used to import GET, POST, and Cookie variables to the global scope. if you disable register_globals but want to use some global variables, this function is very useful.
Syntax:
bool import_request_variables(string $types[, string $prefix])
The first parameter can be g, p, or c, that is, the first letter of the GET, POST, or COOKIE variable, with a clear meaning;
The second parameter is the prefix of the imported variable and can be written at will;
Simple example:
import_request_variables("p","p_");echo $p_dopost;?>
For the HTML part, refer to Method 1 and obtain the variable in the dopost input box.
5. use the extract function
The PHP extract () function imports variables from the array to the current symbol table. For each element in the array, the key name is used for the variable name, and the key value is used for the variable value. The second parameter type is used to specify how the extract () function treats such conflicts when a variable already exists and an element with the same name exists in the array.
Syntax:
extract(array,extract_rules,prefix)
Usage:
if(!empty($_POST)) extract($_POST);echo $dopost;?>
Is this method simple? It is also very practical.
6. methods used in dedecms
In dede, determine in advance
The value of register_globals. if it is Off, the following program is called. if it is On, it can be used directly. The specific code is as follows, which is stored in the common. inc. php file.
// Check and register the externally submitted variable foreach ($ _ REQUEST as $ _ k = >$ _ v) {if (strlen ($ _ k)> 0 & eregi ('^ (cfg _ | GLOBALS)', $ _ k )&&! Isset ($ _ COOKIE [$ _ k]) {exit ('request var not allow! ') ;}} Function _ RunMagicQuotes (& $ svar) {if (! Get_magic_quotes_gpc () {if (is_array ($ svar) {foreach ($ svar as $ _ k => $ _ v) $ svar [$ _ k] = _ RunMagicQuotes ($ _ v);} else {$ svar = addslashes ($ svar);} return $ svar ;} foreach (Array ('_ get',' _ post', '_ COOKIE') as $ _ request) {foreach ($ _ request as $ _ k = >$ _ v) $ {$ _ k} = _ RunMagicQuotes ($ _ v );}
Put the above code in a public php page, and you only need to use it to obtain the value of the webpage.
The above is the content of the six methods for transferring values between php pages. For more information, see PHP Chinese network (www.php1.cn )!