This article mainly introduces the PHP security URL string base64 encoding and decoding, on the basis of Base64 replaced some unsafe characters, the need for friends can refer to the following
If you use the Base64_encode and Base64_decode methods directly, the resulting string may not be applicable to the URL address. The following methods can be used to resolve the problem:
URL-safe string encoding:
The code is as follows:
function Urlsafe_b64encode ($string) { $data = Base64_encode ($string); $data = str_replace (Array (' + ', '/', ' = '), array ('-', ' _ ', '), $data); return $data; }
URL-safe String decoding:
The code is as follows:
function Urlsafe_b64decode ($string) { $data = str_replace (Array ('-', ' _ '), Array (' + ', '/'), $string); $mod 4 = strlen ($data)% 4; if ($mod 4) { $data. = substr (' = = = ', $mod 4); } Return Base64_decode ($data); }