php函数base64_encode后的参数包含加号解析出错的解决方案

WBOY
Release: 2016-06-23 13:29:59
Original
1616 people have browsed it

在进入主题之前我们先来看这样如下代码,test.php

<?php $url = "test2.php?name=".base64_encode('用来做测试的字符串')."&age=23";header("Location:$url");
Copy after login


test2.php

<?phpvar_dump (base64_decode($_GET['name']));die();
Copy after login

访问http://localhost/test.php地址时,重定向到http://localhost/test2.php?name=55So5p2l5YGa5rWL6K+V55qE5a2X56ym5Liy&age=23地址,浏览器输出结果为:

<pre class="n">string '用来做测??y??9ke??)?.,' (length=26)
Copy after login

怎么回事,base64_decode无法解码?


仔细观察重定向后的地址中加密后的name参数,其中包含“+”符号,而浏览器的地址栏中碰到“+”符号时会将加号转换为空格,于是要保证base64_decode进行正确的解码操作,我们可以先将参数中的空格替换成加号,如下代码所示:

<?php$replaces = array(' ' => '+' );var_dump(base64_decode(strtr( $_GET['name'], $replaces)));die();
Copy after login
得到正确的输出结果:

string '用来做测试的字符串' (length=27)
Copy after login



版权声明:本文为博主原创文章,未经博主允许不得转载。

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