php中url传递中文字符,特殊危险字符的解决方案
本文章结合php中的urldecode,base64_encode函数然后再结合自己写的替换函数来进行安全传递url中文字符,特殊危险字符,有需要了解的朋友可参考。
我们需要在url中传递中文字符或是其它的html等特殊字符,似乎总会有各种乱,不同的浏览器对他们的编码又不一样,
对于中文,一般的做法是:
把这些文本字符串传给url之前,先进行urlencode($text)一下;
但是对于一些很“危险”的字符,比如说html字符,甚至是SQL注入相关的字符,如果很明显的传给系统,出于安全考虑,系统一般都会把它们过滤掉的。
现在,我们需要这些危险字符,该这么办?
我想到的办法是 先给它们 base64_encode($text) 编码,到服务端时,又给它们 base64_decode($text) 解码,
貌似很完美,但是在使用的过程中又遇到一个问题,base64_encode 编码后的字符串中含有 "/", "+", "=" 等字符,
base64_encode()函数由于要在url中传用户输入观点(少量的内容),当用户提交(post提交)过来的是一个数组.所以我把观点用bse64_encode()函数给加密.当跳转到处理页面时,我再给get接收,这时出现两边加密的数据不对.少了一个+字符.
用户提交加密:
tPK9tNPNyKUsuse6xyYjNDY7JiM0NjsufMavwcEhfMyrxq/BwcHLLMjDztLO3tPvLNXmz+vI69ehsKEhfHw=
在处理页面用get接收到的:
tPK9tNPNyKUsuse6xyYjNDY7JiM0NjsufMavwcEhfMyrxq/BwcHLLMjDztLO3tPvLNXmz vI69ehsKEhfHw=
对比找到少了一个加号,不知道是什么原因导制的(猜想可能是get时,+字符可能不能得到吧!).还请高手指点.
这些字符在url编码中又是特殊字符,比如 "+" ,它就表示 “空格”,但是不同的浏览器对“空格”的编码又不一样,有的是用“+”表示,有的是用“20%”表示,也就是说,让这些base64_encode编码后的字符串在url中传递,用不同的浏览器去浏览时,服务端得到值不一样。
于是乎,想到了一个折中办法,先将这些base64编码后的特殊字符替换掉,到服务端后,又替换回来:
解决方法:
一.在用户提交加密串的时间,我把+字符给换成别的字字符.如:str_replace('+', '_', $content);
二.在处理页面再次转换一次:如:str_replace('_', '+', $content);
代码如下 | 复制代码 |
function base_encode($str) { |
下面是在浏览器中得到的效果
xOO6w6Osuf65_aiy_atL_b00Ke5_b8jnus6ho6GjoaM_c
urldecode实例方法很简单
urldecode ( string $str )
解码给出的已编码字符串中的任何 %##。返回解码后的字符串。
Example #1 urldecode() 例子
代码如下 | 复制代码 |
$a = explode('&', $QUERY_STRING); |

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
