当前位置: 首页 > 后端技术 > PHP

PHP安全URL字符串base64编码解码

时间:2023-03-29 20:06:26 PHP

字符串使用base64编码后,会有特殊符号'+','/','=',如果通过url传递字符串,会出现意想不到的问题,所以这里我们需要过滤编码字符串中的特殊字符来解决这个问题。如果直接使用base64_encode和base64_decode方法,生成的字符串可能不适合URL地址。下面的方法可以解决这个问题:URL安全字符串编码:functionurlsafe_b64encode($string){$data=base64_encode($string);$data=str_replace(array('+','/','='),array('-','_',''),$data);返回$数据;}URL安全字符串解码:functionurlsafe_b64decode($string){$data=str_replace(array('-','_'),array('+','/'),$string);$mod4=strlen($data)%4;如果($mod4){$data.=substr('====',$mod4);}返回base64_decode($data);}注:本文转载,原文地址