When you use a URL to pass parameter data, add the appropriate parameter after the URL address. The URL entity processes these parameters.
1. encode the parameters passed by the URL
When you use a URL to pass parameter data, add the appropriate parameter after the URL address. The URL entity processes these parameters. The format is as follows:
Obviously, this method exposes parameters with a low security factor. Therefore, this chapter describes a URL encoding method to encode the parameters passed by the URL.
URL encoding is a format used by a browser to package input data in a form. it is an encoding rule for passing parameters in the address bar. If there is a space in the parameter, an error will occur when the parameter is passed through the URL. After URL encoding is used, the space is converted to % 20. this will not happen. It is also a rumor that Chinese characters are encoded. The most important thing is to hide the transmitted parameters.
URL encoding of the query string in PHP can be implemented through the urlencode () function. the syntax of this function is as follows:
The urlencode () function implements URL encoding for the string.
In the following example, the urlencode () function is used to encode the parameter values passed by the URL. the displayed string is
The implementation code of the URL-encoded string is as follows:
Enter the running address in the browser and press enter to obtain the running result as follows:
Index. php? Id = PHP % E4 % B8 % AD % E6 % 96% 87% E7 % BD % 91
For the server, there is no difference between the strings before and after encoding, and the server can automatically identify the strings. This document describes how to use URL encoding. In practical applications, non-confidential parameters do not need to be encoded. you can choose to use them based on your actual situation.
2. decode the parameters passed by the URL
You can directly use the $ _ GET  method to obtain the URL parameters. The URL-encrypted query string must be decoded using the urldecode () function. The syntax of this function is as follows:
The urldecode () function can encode the string after URL encoding.
In the above example, the urlencode () function encodes "PHP Chinese network""PHP % E4 % B8 % AD % E6 % 96% 87% E7 % BD % 91".
In this example, the urlencode () function is used to decode the obtained encoding and output the decoded result. The implementation code is as follows:
The running result is as follows:
PHP Chinese network
Here we can clearly see that the urldecode () function restores the encoded string of the urlencode () function.
The above is the detailed content for php to encode and decode URL parameters. For more information, see other related articles in the first PHP community!