English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

The method of parsing query strings into variables using parse_str in PHP

This article instance tells the method of using parse_str to parse query strings into variables in PHP. Shared for everyone's reference, as follows:

The parse_str() function can parse strings into variables, which means it has realized a conversion mechanism between strings and variables. In the process of data transmission between the client and the server, data is transmitted in the form of strings, such as GET requests, and then on the server side, through $_GET/$_POST, etc., global variables to realize the conversion between string and variables, such as: https://www.oldtoolbag.com/?index.php?var1=1&var2=2, After the request, the server can use $_GET['var1'] to obtain var1=1&var2=2This segment of string is converted into variables. The parse_str() function can achieve similar functionality, by using the parse_str() function to parse the value of $_SERVER['QUERY_STRING'], you can directly realize the conversion between string and variables, such as $var1.

One, function prototype

void parse_str ( string str [, array &arr] )

Two, version compatibility

PHP 3, PHP 4, PHP 5

Three, basic usage and examples of functions

1. Parse the string into a variable

<?php
parse_str("var1=jb51&var2=parse_str");
echo $var1.$var2;
?>

2. Parse the string and store the variables in the array

<?php
parse_str("var1=jb51&var2=parse_str",$array);
print_r($array);
?>

Output:

Array ( [var1] => jb51 [var2] => parse_str )

Note: This stores variables in an array in PHP 4.0.3 . Increase

3. The parsed string contains spaces

<?php
parse_str("v ar1=jb51&var 2=parse_str",$array);
print_r($array);
?>

Output:

Array ( [v_ar1] => jb51 [var_2] => parse_str )

Note: Directly convert spaces to underscores _

Four, precautions

1. If the array parameter is not set, the variables set by this function will overwrite the variables with the same name.

2. The magic_quotes_gpc setting in .php.ini affects the output of this function. If enabled, variables will be converted by addslashes() before parsing by parse_str().

3. The parse_str() function has a vulnerability in processing parameters, which can be exploited by attackers to enable register_globals and further exploit vulnerabilities in other PHP scripts. If parse_str() is called with only one parameter, the function will treat the string provided as a request string transmitted through a URL and parse it accordingly. However, external attackers can send many request variables during the call to parse_str() to trigger the termination of memory_limit request. If the request is closed during the call to parse_str(), the register_globals tag will remain open for the rest of the lifecycle of the relevant webserver process.

Readers who are interested in more PHP-related content can check the special topics on this site: 'Summary of Usage of PHP String (string)', 'PHP Data Structures and Algorithms Tutorial', 'Summary of PHP Program Design Algorithms', 'Summary of PHP Sorting Algorithms', 'Summary of Common PHP Traversal Algorithms and Techniques', 'Summary of PHP Mathematical Operation Techniques', 'Complete PHP Array (Array) Operation Techniques', and 'Summary of Common Database Operation Techniques in PHP'.

I hope this article is helpful to everyone in PHP program design.

Statement: The content of this article is from the network, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume any relevant legal liability. If you find any copyright-infringing content, please send an email to: notice#oldtoolbag.com (When reporting via email, please replace # with @) to report any infringement, and provide relevant evidence. Once verified, this site will immediately delete the infringing content.

You May Also Like