Home  >  Article  >  Backend Development  >  Solution to passing Chinese characters and special dangerous characters in php URL_PHP Tutorial

Solution to passing Chinese characters and special dangerous characters in php URL_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 14:59:091052browse

We need to pass Chinese characters or other special characters such as HTML in the URL. There seems to be all kinds of chaos. 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.

Now, we need these dangerous characters, what should we do?

The way I think of it is to base64_encode($text) them first, and then decode them with base64_decode($text) when they get to the server,

It seems perfect, but I encountered another problem during use. The string encoded by base64_encode contains "/", "+", "=" and other characters,


The base64_encode() function needs to pass the user input view (a small amount of content) in the URL, and when the user submits it (post submission), it is an array. So I use the bse64_encode() function to encrypt the view. When jumping to the processing page, I received it again with get. At this time, the encrypted data on both sides was wrong. There was a + character missing.

User submission encryption:

tPK9tNPNyKUsuse6xyYjNDY7JiM0NjsufMavwcEhfMyrxq/BwcHLLMjDztLO3tPvLNXmz+vI69ehsKEhfHw=


Received using get on the processing page:

tPK9tNPNyKUsuse6xyYjNDY7JiM0NjsufMavwcEhfMyrxq/BwcHLLMjDztLO3tPvLNXmz vI69ehsKEhfHw=

In comparison, I found that there is a missing plus sign. I don’t know what the reason is (guess it may be that the + character may not be obtained during get!). Please give some advice from experts.

These characters are special characters in URL encoding, such as "+", which represents "space", but different browsers encode "space" differently. Some use "+" to represent it, and some use "+" to represent "space". "20%" means, that is to say, if these base64_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:


Solution:

1. When the user submits the encrypted string, I replace the + character with other characters. For example: str_replace('+', '_', $content);
2. On the processing page again Convert once: such as: str_replace('_', '+', $content);

Copy code The code is as follows:
Function base_encode ($ str) {
$ src = array ("/" " ,"+","=");
$dist = array("_a","_b","_c");
$old = base64_encode($str);
$new = str_replace ($src,$dist,$old);
                                                                               . ,"_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

The urldecode instance method is very simple

urldecode ( string $str ) Decode any %## in the given encoded string. Returns the decoded string.

Example #1 urldecode() Example


Copy code

The code is as follows:

$a = explode('&', $QUERY_STRING );$i = 0;while ($i < count($a)) {
$b = split('=', $a[$i]);
echo 'Value for parameter ', htmlspecialchars(urldecode($b[0])),
       ' is ', htmlspecialchars(urldecode($b[1])), "
";
            i++;
}
?>






http://www.bkjia.com/PHPjc/328166.html
www.bkjia.com

true

TechArticleWe need to pass Chinese characters or other html and other special characters in the url. There always seems to be all kinds of chaos. , different browsers encode them differently. For Chinese, generally do...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn