


Analyze problems caused by special characters in PHP URLs (+,\,=)
Recommended: "PHP Video Tutorial"
Problems caused by special characters in the URL in PHP (,,=)
Foreword, In the process of working on a certain channel, I discovered a signature verification error. However, at that time, the signature verification performance was inconsistent in the two places with the same set of processing methods. I thought this was because the request methods in the two places were different. One was the get method and the other was naturally the post method. Of course, the problem must be get.
GET and POST
GET request methods, since the parameters are placed in the URL, may be subject to some policy issues on the browser side when passing them. Urlencode the parameters. Therefore, when you get the parameters on the server side, they may not be the original data. Therefore, when requesting data through GET, if no processing is done, there may be problems in verifying the signature. The possibility here is that this special character is not included after base64 processing, and a blank string is obtained without any processing after the GET method.
The POST request method puts the parameters in the request body. During the http transfer process, there will be no processing of the parameters due to some strategic issues of the browser. Therefore, there will be no problem when performing parameter signature verification through POST requests, and signature verification can be performed smoothly. However, we have no way to ask the channel provider to turn the get request into a post request, so we can only find a way ourselves.
urlencode and urldecode
urlencode: (PHP 4, PHP 5, PHP 7) urlencode — 编码 URL 字符串 string urlencode ( string $str )
This function facilitates encoding a string and using it in the request part of the URL, and it also facilitates passing variables to Next page.
return
Returns a string. All non-alphanumeric characters in this string except -_. will be replaced with a percent sign (%) followed by two hexadecimal digits, and spaces are encoded. is a plus sign ( ). This encoding is the same as the WWW form POST data, and the same media type encoding as application/x-www-form-urlencoded
urldecode: (PHP 4, PHP 5, PHP 7)
urldecode — Decode an encoded URL string
string urldecode ( string $str )
Decode any %## in the given encoded string. The plus sign (' ') is decoded into a space character.
Return the decoded string.
It seems that we have seen the light of day, the "perfect way" to deal with this string that will turn into spaces. That is, urlencode the signature string to encrypt it. Then, happily verify, fxxk, false. If you still don’t pass, then give yourself a slap in the face. After base64 encryption, the padding string = will appear, which is very painful. So I thought of a temporary solution.
urlencode(substr($str,0,strlen($sign)-2)).substr($sign,strlen($sign)-2)
At that time, considering that there were at most two == in base64, urlencode processing was not performed on the last two. This can basically be handled, but there may be a problem, that is, it will not work if the last two appear. Sure enough, this plan cannot be convinced and overturned. And a problem also discovered during this process is that the passed signature string may have been processed by urlencode. This is still a small problem. Perform urldecode processing first, because decoding will not cause misunderstandings.
At that time, a friend proposed a solution, that is, wouldn’t it be enough to directly replace the number? Indeed, this is a way. But I think this method is very frustrating. What if the encryption algorithm changes or other special characters are added in the future, such as @#¥%...&**( etc., we cannot all match and replace. So, I agree Workaround, but I keep thinking.
rawurlencode and rawurldecode
rawurlencode: (PHP 4, PHP 5, PHP 7)
rawurlencode — Encode URLs per RFC 3986
string rawurlencode ( string $str )
Encodes the specified characters according to » RFC 3986.
rawurldecode: (PHP 4, PHP 5, PHP 7)
rawurldecode — Decodes an encoded URL string
string rawurldecode ( string $str )
Returns a string in which a percent sign (%) is followed by two characters The sequence of hexadecimal digits will be replaced with literal characters.
A new dawn has emerged, understanding rawurldecode and replacing it with literal characters. Therefore, the solution is ready.
rawurldecode(urlencode(urldecode($sign))));
At first glance, it seems bloated or why do you have to go around and deal with it like this? In fact, you really have to deal with it like this. As for why, please read the bragging above.
Postscript
As programmers, we must have two preparations, one is a temporary solution, which can quickly fix the current problem. After the production environment returns to normal, in the long run, we must have a stable and reliable solution. The solution comes from You keep trying and php.net.
The above is the detailed content of Analyze problems caused by special characters in PHP URLs (+,\,=). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
