1. Encode the parameters passed by the URL
Using a URL to pass parameter data is to add the appropriate parameters after the URL address. These parameters are processed by the URL entity. It is used in the following format:
It is obvious that this method will expose the parameters to a lower safety factor. Therefore, this chapter describes a URL encoding for the issue, encoding the parameters passed by the URL.
URL encoding is a format that a browser uses to package form input data, which is an encoding rule for passing parameters using the address bar. If there is a space in the argument, an error occurs when the parameter is passed with the URL, and the space is converted to%20 after the URL is encoded. So the error won't happen. Encoding Chinese is also the case of nursery rhymes, the main point is to pass the parameters played a hidden role.
In PHP, the query string URL encoding, can be implemented through the UrlEncode () function, the syntax of the function is as follows:
UrlEncode (String)
The UrlEncode () function implements URL encoding of string strings.
In the following instance, the UrlEncode () function is applied to encode the parameter values passed by the URL, and the string displayed is
The URL-encoded string, which implements the code as follows:
<?php $url = UrlEncode (' topic.alibabacloud.com '); Encode the topic.alibabacloud.com echo "index.php?id=". $url;? >
Enter the run address in the browser and press ENTER to get the result as shown below:
index.php?id=php%e4%b8%ad%e6%96%87%e7%bd%91
Description
For the server, there is no difference between the string before and after the encoding, and the server can recognize it automatically. The main purpose here is to explain how URL encoding is used. In practical applications, some non-confidential parameters do not need to be encoded, the reader can be selected according to the actual situation of use.
2. Decoding the parameters passed by the URL
Use the $_get[] method directly for URL-passed parameters to get. For URL-encrypted query strings, the acquired string needs to be decoded through the UrlDecode () function. The syntax for this function is as follows:
UrlDecode (String)
The UrlDecode () function encodes a URL-encoded string string.
In the example above, the UrlEncode () function encodes "topic.alibabacloud.com" 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 code that implements it is as follows:
<?php $url = UrlDecode ("php%e4%b8%ad%e6%96%87%e7%bd%91"); Restore the code to topic.alibabacloud.com echo $url;? >
The results of the run are as follows:
Topic.alibabacloud.com
It is clear here that the UrlDecode () function restores the string encoded by the UrlEncode () function.