Solution for url transmitting Chinese characters and special dangerous characters (for reference only) urldecode, base64_encode_PHP tutorial

WBOY
Release: 2016-07-13 10:16:12
Original
1033 people have browsed it

The solution for url to pass Chinese characters and special dangerous characters (for reference only) urldecode, base64_encode

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;
}
Copy after login

The following is the effect obtained in the browser

xOO6w6Osuf65_aiy_atL_b00Ke5_b8jnus6ho6GjoaM_c

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

Articles you may be interested in

  • PHP functions urlencode(), urldecode(), rawurlencode(), rawurldecode()
  • js restricts the input of only English letters and numbers, and cannot input Chinese and other special characters
  • php creates a simple case of your own MVC framework, providing ideas, for reference only
  • POST in AJAX Or the solution to GET transmitting garbled Chinese characters
  • js Modify the value of a specified parameter in the url
  • In PHP, use curl's post method to submit data and get method to obtain web page data. Summary of methods
  • PHP restricts the input of regular expressions to only English characters and numbers
  • Prohibits right-clicking, copying, saving as, viewing source files and other functions on the webpage to achieve webpage source code protection

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/898528.htmlTechArticleSolution for url passing Chinese characters and special dangerous characters (for reference only) urldecode, base64_encode in the process of web development , when we need to pass Chinese characters or other characters in the url...
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template