Php encodes and decodes URL parameters

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

Urlencode (string)

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

Note:

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:

Urldecode (string)

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!

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.