Many times, we need to pass in the URL of the Chinese character or other special characters such as HTML, there always seems to be a variety of chaos, different browsers for their coding is not the same,
For Chinese, the general practice is:
Before passing the text string to the URL, first urlencode ($text);
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.
Now, we need these dangerous characters to do this.
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:
1 2 3 4 5 6 7 8 9 |
function base_encode ($str) { $src = Array ("/", "+", "="); $dist = Array ("_a", "_b", "_c"); $old = base64_encode ($STR); $new = str_replace ($src, $dist, $old); |