Url transfer of Chinese characters, solutions for special dangerous characters (for reference only) urldecode, base64_encode

Source: Internet
Author: User

Url transfer of Chinese characters, solutions for special dangerous characters (for reference only) urldecode, base64_encode

In the process of web development, when we need to pass Chinese characters or other special characters such as html in the url, it seems that we will always encounter various small problems, because different browsers have different codes for them. For Chinese, the general practice is:

Before passing these text strings to the url, perform urlencode ($ text) first.

However, for some "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 usually filters them out.

So what if we need to keep these dangerous characters and do not filter them?

The solution I have come up with is to encode them with base64_encode ($ text) and decode them with base64_decode ($ text) on the server,

It looks perfect, but there is another problem in the use process. The base64_encode encoded string contains characters such as "/", "+", "=,

These characters are special characters in url encoding. For example, "+" indicates "space", but different browsers have different codes for "space, some are represented by "+" and some are represented by "20%". That is to say, when these base64_encode encoded strings are passed in the url and browsed in different browsers, the value obtained by the server is different.

As a result, I thought of a compromise. I first replaced these base64 encoded special characters, and then replaced them on 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 figure shows the effect in the browser.

XOO6w6Osuf65_aiy_atL_b00Ke5_b8jnus6ho6GjoaM_c

Haha, let's see how it works...

Articles you may be interested in
  • URL encoding functions such as urlencode (), urldecode (), rawurlencode (), and rawurldecode ()
  • Js restrictions: Only English letters and numbers can be entered. Chinese characters and other special characters cannot be entered.
  • Php builds a simple example of its own MVC Framework and provides ideas for your reference only
  • Solution to passing garbled Chinese characters through POST or GET in AJAX
  • Js modifies the value of a specified parameter in the url
  • In php, we use the curl post method to submit data and the get method to obtain webpage data.
  • Php restricts regular expressions that can only contain English characters and numbers
  • Prohibit web page right-click, copy, save as, View Source file and other functions to implement web page source code protection

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.