web开发的过程中,当我们需要在url中传递中文字符或是其它的html等特殊字符时,似乎总会碰到各种各样的小问题,因为不同的浏览器对他们的编码又不一样。对于中文,一般的做法是:
把这些文本字符串传给url之前,先进行urlencode($text)一下。
但是对于一些很“危险”的字符,比如说html字符,甚至是SQL注入相关的字符,如果很明显的传给系统,出于安全考虑,系统一般都会把它们过滤掉的。
那么,如果我们需要保留这些危险字符,不被过滤,该这么办呢?
我想到的办法是先给它们 base64_encode($text) 编码,到服务端时,又给它们 base64_decode($text) 解码,
貌似很完美,但是在使用的过程中又遇到一个问题,base64_encode 编码后的字符串中含有 "/", "+", "=" 等字符,
这些字符在url编码中又是特殊字符,比如 "+" ,它就表示 “空格”,但是不同的浏览器对“空格”的编码又不一样,有的是用“+”表示,有的是用“20%”表示,也就是说,让这些base64_encode编码后的字符串在url中传递,用不同的浏览器去浏览时,服务端得到值不一样。
于是乎,想到了一个折中办法,先将这些base64编码后的特殊字符替换掉,到服务端后,又替换回来:
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; }
下面是在浏览器中得到的效果
xOO6w6Osuf65_aiy_atL_b00Ke5_b8jnus6ho6GjoaM_c
呵呵,看看效果如何。。。
您可能感兴趣的文章
- PHP解决网址URL编码问题的函数urlencode()、urldecode()、rawurlencode()、rawurldecode()
- js限制只能输入英文字母和数字,不能输入中文和其他特殊字符的办法
- php打造属于自己的MVC框架简单案例,提供思路,仅供参考
- AJAX中POST或GET传递中文字符乱码的解决办法
- mysql命令更改表结构:添加、删除、修改字段、调整字段顺序
- 在php中分别使用curl的post提交数据的方法和get获取网页数据的方法总结
- js 修改url中某个指定的参数的值
- 复选框(checkbox)和单选框(radio)与文字水平垂直居中对齐的解决方法

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 English version
Recommended: Win version, supports code prompts!

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver Mac version
Visual web development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.
