Home >Backend Development >PHP Tutorial >PHP secure URL string base64 encoding and decoding example code

PHP secure URL string base64 encoding and decoding example code

怪我咯
怪我咯Original
2017-07-09 09:26:562711browse

This article mainly introduces PHPSecurityURLStringbase64 encoding and decoding. Some unsafe characters are replaced on the basis of base64. Friends in need can refer to it. Next

If you use the base64_encode and base64_decode methods directly, the generated string may not be suitable for the URL address. The following method can solve this 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);
   $mod4 = strlen($data) % 4;
   if ($mod4) {
       $data .= substr('====', $mod4);
   }
   return base64_decode($data);
 }

The above is the detailed content of PHP secure URL string base64 encoding and decoding example code. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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