Php URL passing Chinese characters, special dangerous characters Solution _php Tutorial

Source: Internet
Author: User
This article combined with PHP Urldecode,base64_encode function and then combined with their own written replacement function to carry out the URL of the text character, special dangerous characters, there is a need to know the friend can refer to.

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 on their code is not the same,

For Chinese, the general practice is to:

Before passing these text strings to the URL, 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, for security reasons, the system will generally filter them out.

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

My idea is to give them Base64_encode ($text) code first, to the server, and Base64_decode ($text) decoding them,

Seemingly perfect, but in the process of using another problem encountered, Base64_encode encoded string containing "/", "+", "=" characters,


The Base64_encode () function is an array when the user submits (post commits) to the user input point of view (a small amount of content) in the URL. So I use the Bse64_encode () function to encrypt the view. When I jump to the processing page, I receive the Get again, The encrypted data on both sides is incorrect. A + character is missing.

User-Submitted encryption:

tpk9tnpnykususe6xyyjndy7jim0njsufmavwcehfmyrxq/bwchllmjdztlo3tpvlnxmz+vi69ehskehfhw=


Received with get received on the processing page:

Tpk9tnpnykususe6xyyjndy7jim0njsufmavwcehfmyrxq/bwchllmjdztlo3tpvlnxmz vi69ehskehfhw=

Compare to find a less than a plus, do not know what the reason for the lead (conjecture may be get when the + character may not get it!). Also please expert guidance.

These characters in the URL encoding is a special character, such as "+", it means "space", but different browsers on the "space" of the code is not the same, some with "+" means, there are some with "20%" means, that is, let these base64_ The encode encoded string is passed in the URL, and the service side gets a different value when it is browsed with various browsers.

So, think of a compromise method, the first of these Base64 coded special characters replaced, after the server, and replaced back:


Workaround:

I. When the user submits the encryption string, I change the + character to another character. such as: str_replace (' + ', ' _ ', $content);
Two. In the processing page to convert once again: such as: Str_replace (' _ ', ' + ', $content);

The code is as follows Copy Code

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;
}

Here's how it works in the browser

Xoo6w6osuf65_aiy_atl_b00ke5_b8jnus6ho6gjoam_c

UrlDecode instance method is simple

UrlDecode (String $str)
Decodes any%## in the encoded string given. Returns the decoded string.

Example #1 UrlDecode () example

The code is as follows Copy Code

$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/445609.html www.bkjia.com true http://www.bkjia.com/PHPjc/445609.html techarticle This article combined with PHP Urldecode,base64_encode function and then combined with their own written replacement function to carry out the URL of the text character, special dangerous characters, there is a need to know the friend can refer to ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.