URL Pass Chinese character, special dangerous character solution (for reference only) UrlDecode, base64

Source: Internet
Author: User
Tags array base64 reference return string sql injection urlencode

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


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.