In the process of web development, when we need to pass Chinese characters or When it comes to other special characters such as HTML, it seems that you will always encounter various small problems because different browsers encode them differently. For Chinese, the general approach is:
Before passing these text strings to the url, perform urlencode($text) first.
But for some very "dangerous" characters, such as html characters, or even SQL injection-related characters, if they are obviously passed to the system, the system will generally filter them out for security reasons.
So, what should we do if we need to keep these dangerous characters from being filtered?
The way I think of it is to encode them first with base64_encode($text), and when they get to the server, decode them with base64_decode($text),
seems to be perfect, but I encountered another problem during use. base64_encode The encoded string contains "/", "+", "=" and other characters,
These characters are special characters in URL encoding, such as "+", which means "space", but different browsers encode "space" differently, some use "+" to represent it, and some use "20%" means, that is to say, if these base64_encode encoded strings are passed in the URL, when browsing with different browsers, the server will get different values.
So, I thought of a compromise, first replace these base64 encoded special characters, and then replace them back after reaching the server:
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 is the effect obtained in the browser
xOO6w6Osuf65_aiy_atL_b00Ke5_b8jnus6ho6GjoaM_c
Haha, let’s see how it works. . .