PHP encoding and decoding of URL parameters

Source: Internet
Author: User
Tags urlencode
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.