Home > Article > Backend Development > PHP encodes and decodes URL parameters
1. Encode the parameters passed by the URL
Using the URL to pass parameter data means adding appropriate parameters after the URL address. URL entities handle these parameters. The method of use is as follows:
Obviously, this method will expose the parameters and has a low safety factor. Therefore, this chapter will describe a URL encoding method for this problem, encoding the parameters passed by the URL.
URL encoding is a format used by browsers to package form input data. It is an encoding rule for passing parameters in the address bar. If there are spaces in the parameters, an error will occur when passing the parameters using URL. After URL encoding, the spaces will be converted into %20. In this way, errors will not occur. Encoding Chinese is also a case of nursery rhymes. The most important point is that it plays a hidden role in the parameters passed.
Recommended Manual: php complete self-study manual
URL encoding the query string in PHP , can be achieved through the urlencode() function. The syntax of this function is as follows:
urlencode(string)
urlencode() function implements the character The string is URL encoded.
In the following example, the urlencode() function is used to encode the parameter value passed by the URL. The displayed string is the
URL encoded string. The implemented code is as follows Display:
<?php $url = urlencode('PHP中文网'); //把 PHP中文网 进行编码 echo "index.php?id=".$url; ?>
Enter the running address in the browser, press Enter, and get the running result as shown below:
index.php?id=PHP%E4%B8 %AD%E6%96%87%E7%BD%91
Explanation:
For the server, there is no difference between the strings before and after encoding, and the server can automatically recognize it. . The main purpose here is to explain how to use URL encoding. In practical applications, some non-confidential parameters do not need to be encoded, and readers can selectively use them according to the actual situation.
2. Decode the parameters passed by the URL
You can directly use the $_GET[] method to obtain the parameters passed by the URL. For URL-encrypted query strings, the obtained string needs to be decoded through the urldecode() function. The syntax of this function is as follows:
urldecode(string)
urldecode() function can encode the URL-encoded string string.
In the above example, the urlencode() function encodes "PHP Chinese website" and turns it into "PHP%E4%B8%AD%E6%96%87%E7%BD%91" .
The example here uses the urlencode() function to decode the obtained encoding and output the decoded result. The implemented code is as follows:
<?php $url = urldecode("PHP%E4%B8%AD%E6%96%87%E7%BD%91"); // 把编码还原成 PHP中文网 echo $url; ?>
The running result is as follows:
PHP Chinese Network
It can be clearly seen here The urldecode() function restores the string encoded by the urlencode() function.
Recommended related articles:
1.How does PHP use the urlencode() function to perform url encoding? (Code example)
2.How does PHP encode and decode Chinese characters in Url? (Pictures + Videos)
Related video recommendations:
1. Dugu Jiujian (4)_PHP video tutorial
The above is the detailed content of PHP encodes and decodes URL parameters. For more information, please follow other related articles on the PHP Chinese website!