In the process of web development, when we need to pass in the URL of the Chinese character or other special characters such as HTML, it always seems to encounter a variety of small problems, because different browsers for their coding is not the same. For Chinese, the general practice is:
Before passing these text strings to the URL, UrlEncode ($text) is done first.
But for some very "dangerous" characters, such as HTML characters, or even SQL injection-related characters, if it is clearly passed to the system, the system will generally filter them out for security reasons.
So, what if we need to keep these dangerous characters and not be filtered?
My idea is to encode them Base64_encode ($text) First and then decode them Base64_decode ($text) to the server.
Seemingly perfect, but in the process of using a problem, Base64_encode encoded string contains "/", "+", "=" and other characters,
These characters in the URL encoding is a special character, such as "+", it means "space", but different browsers to "space" coding is not the same, some use "+" said, some use "20%" said, that is, let these base64_ The encode encoded string is passed in the URL, and the server gets a different value when browsing through the browser.
Thus, the idea of a compromise, the first of these Base64 encoded special characters replaced, to the service side, and then replaced back:
function Base_encode ($str) {
$src = Array ("/", "+", "=");
$dist = Array ("_a", "_b", "_c");
$old = Base64_encode ($STR);
$new = Str_replace ($src, $dist, $old);
return $new;
}
function Base_decode ($str) {
$src = array ("_a", "_b", "_c");
$dist = Array ("/", "+", "=");
$old = Str_replace ($src, $dist, $str);
$new = Base64_decode ($old);
return $new;
}
The following are the effects you get in the browser
Xoo6w6osuf65_aiy_atl_b00ke5_b8jnus6ho6gjoam_c
Oh, see how the effect ...
Articles that you may be interested in
- PHP addresses URL encoding problem functions UrlEncode (), UrlDecode (), Rawurlencode (), Rawurldecode ()
- JS limit can only input English letters and numbers, can not enter Chinese and other special characters method
- PHP builds a simple case for its own MVC framework, providing ideas for reference only
- A solution to the character garbled in post or get delivery in Ajax
- MySQL Command change table structure: Add, delete, modify fields, adjust field order
- Summary of methods for using Curl post submission data and get access to Web page data in PHP
- JS modifies the value of a specified parameter in the URL
- check box (checkbox) and Radio Box (radio) are centered vertically with the text horizontally