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

怪我咯
Release: 2023-03-13 09:06:02
Original
2713 people have browsed it

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;
 }
Copy after login

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);
 }
Copy after login

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!

Related labels:
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