6 ways to transfer values ​​between php pages

PHP中文网
Release: 2023-02-28 22:30:02
Original
4585 people have browsed it

6 ways to pass values ​​between PHP pages:

1. How to get the passed value after PHP4

Generally, the most common ways to pass values ​​in pages are POST, GET and COOKIE , so I will mainly introduce these types below. PHP4 and later will use $_POST, $_GET and other arrays to obtain web page values. In PHP3.0 and below, arrays such as $HTTP_POST_VARS and $HTTP_GET_VARS are used. The specific codes are as follows

echo $_POST['dopost'];
?>

< form action="weste_net.php" method="post" name=&#39;form1&#39; id="form1">
< input type="text" name="dopost" value="hello weste.net!" />
< input type="submit" name="weste" value="提交" />
< /form>
Copy after login

2. The old version of PHP3 used to get the value transfer method

as mentioned above , you can use the $HTTP_POST_VARS, $HTTP_GET_VARS, $HTTP_COOKIE_VARS arrays to read. In the old version of PHP, you can also use methods such as $dopost to directly obtain the passed value. This method is relatively simple. Among the following methods of obtaining the passed value of a web page, icech actually focuses on how to use $dopost. To get the value passed by the web page.

3. Modify the configuration of the PHP.ini file

Another question is why are PHP3 and PHP4 and later incompatible? In fact, a very important reason is that after PHP4, register_globals in the PHP.ini file is set to Off by default. Therefore, the previous method of using $dopost to directly obtain the value of the web page cannot be used.

Easy way to check the value of register_globals

echo &#39;register_globals = &#39; . ini_get(&#39;register_globals&#39;);
?>
Copy after login

If register_globals=Off, display register_globals = or register_globals = 0

If register_globals=On, display register_globals = 1

Manually modify register_globals to On, if in the submitted form Contains a variable named "dopost", then you can use $dopost to get the variable in PHP. But if you are using a virtual host, it is still difficult to modify the php.ini file. Here is just one method.

4. Use the import_request_variables function

The import_request_variables function is to import GET, POST, and Cookie variables into the global scope. This function is useful if you disable register_globals but want to use some global variables. .

Syntax:

bool import_request_variables(string $types[, string $prefix])
Copy after login

The first parameter can be g, p or c, which is the first letter of the GET, POST, and COOKIE variables, and the meaning is very clear;

The second parameter is the variable prefix after import, which can be Write as you like;

Simple example:

import_request_variables("p","p_");
echo $p_dopost;
?>
Copy after login

Refer to method 1 for the HTML part, and get the value of the dopost input box variable.

5. Use the extract function

PHP extract() function to import variables from the array into the current symbol table. For each element in the array, the key 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 there is an element with the same name in the array.

Grammar:

extract(array,extract_rules,prefix)
Copy after login

Usage:

if(!empty($_POST)) extract($_POST);
echo $dopost;
?>
Copy after login

This method is very simple, right? Very practical too.

6. Methods used in dedecms

In dede, the value of register_globals is judged in advance. If it is Off, call the following program. If it is On, then you can use it directly. The specific code is as follows, which is placed in the common.inc.php file.

//检查和注册外部提交的变量
foreach($_REQUEST as $_k=>$_v)
{
if( strlen($_k)>0 && eregi(&#39;^(cfg_|GLOBALS)&#39;,$_k) && !isset($_COOKIE[$_k]) )
{
exit(&#39;Request var not allow!&#39;);
}
}
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(&#39;_GET&#39;,&#39;_POST&#39;,&#39;_COOKIE&#39;) as $_request)
{
foreach($$_request as $_k => $_v) ${$_k} = _RunMagicQuotes($_v);
}
Copy after login

Put the above code into a public php page. You only need to use it when getting the value passed by the web page later.

The above is the content of the 6 methods of transferring values ​​between PHP pages. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template