php The base64 garbled code is because there are some Chinese characters. When passed in the GET format, the " " sign will be replaced with a space, resulting in garbled code. The solution is to replace and then decrypt.
Solution to decoding garbled characters after PHP base64 encoding
This article mainly introduces the decoding after PHP base64 encoding The solution to garbled codes is that the base64 encoding contains some special characters. Just replace them. Friends in need can refer to
Recommendation: "PHP Tutorial"
I discovered a problem when using PHP to make things. It can be simply attributed to the problem of garbled characters, but this problem is not caused by the function itself. Let’s find out who the culprit is.
Suspect: base64_encode and base64_decode
Crime: I wrote a jump and prompt function. After receiving the prompt information, it jumps to the specified page, but the Chinese characters are garbled during the jump prompt.
The jump template code is as follows:
The code is as follows:
<!DOCTYPE html><html><head><meta charset="utf-8"><meta name="author" content="王健 wj@yurendu.com" /> <title>跳转提示</title> <style type="text/css"> *{ padding: 0; margin: 0; } body{ background: #fff; font-family: '微软雅黑'; color: #333; font-size: 16px; text-align:center; } .system-message{ width:600px; margin:150px auto 0 auto; background:#f8f8f8; border:1px solid #ccc;-webkit-border-radius: 8px;-moz-border-radius: 8px;border-radius: 8px;-webkit-box-shadow: #666 0px 0px 10px;-moz-box-shadow: #666 0px 0px 10px;box-shadow: #666 0px 0px 10px;} .system-message h1{ font-size:30px; font-weight:normal; height:100px; line-height:100px; color:#c60;} .system-message .jump{ padding: 40px 0;} .system-message .jump a{ color: #333;} .system-message .success,.system-message .error{ height:60px; line-height:70px; font-size: 18px; color:#900;} .system-message .detail{ font-size: 12px; line-height: 20px; margin-top: 12px; display:none} </style> </head> <body> <div class="system-message"> <?php if( $_GET['success'] ){?> <h1 id="nbsp-nbsp-nbsp-恭喜">:) 恭喜!</h1> <p class="success"><?php echo base64_decode($_GET['message']); ?></p> <?php }else{?> <h1 id="nbsp-nbsp-nbsp-出错了">:( 出错了!</h1> <p class="error"><?php echo base64_decode($_GET['message']); ?></p> <?php }?> <p class="detail"></p> <p class="jump">系统将在 <b id="wait"><?php echo $_GET['time']; ?></b> 后跳转,可直接 <a id="href" href="<?php echo base64_decode($_GET['url']); ?>">点此跳转</a></p> </div><script type="text/javascript">(function(){var wait = document.getElementById('wait'),href = document.getElementById('href').href;var interval = setInterval(function(){ var time = --wait.innerHTML; if(time <= 0) { location.href = href; clearInterval(interval); };}, 1000);})();</script></body></html>
PHP redirect function is defined as follows:
The code is as follows:
/* 提醒后跳转 */ function _alert( $success=true, $message='success', $time='3', $url='/'){ header('Location:/include/redirect.php?success='.$success.'&message='.base64_encode($message).'&time='.$time.'&url='.base64_encode($url)); exit; }
If you call the function in PHP like this:
The code is as follows:
$query = "update content set bid='$clean[bid]',title='$clean[title]',content='$clean[content]',path='$clean[path]' where id=".$clean['id']; if( mysql_query($query) ){ _alert(1,'修改成功',3,'/admin/manage.php'); }else{ _alert(false,'修改失败'.mysql_error(),5,'/admin/manage.php'); }
You will see that the Chinese characters for "modification successful" or "modification failed" are garbled.
Why?
Sometimes after encrypting with base64_encode, it is transmitted to other pages in the form of GET, and when decrypted with base64_decode, garbled characters appear.
When I encountered this problem, I was wondering why some could be decrypted correctly, but some were garbled?
After checking later, I found that there were some Chinese characters. When passed in GET format, numbers will be replaced with spaces.
In order to prevent garbled characters from appearing, I made a one-step substitution and then decrypted it. Sure enough, the problem of garbled characters no longer exists!
Now the problem is very simple, just write more Just one step is enough
The code is as follows:
$str = base64_decode(str_replace(" ","+",$_GET['str']));
It turns out that the article identified the wrong suspect at the beginning and wrongly accused those two functions. . .
The above is the detailed content of What to do if php base64 is garbled. For more information, please follow other related articles on the PHP Chinese website!

The article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

Article discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

The article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

The article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.

The article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

WebStorm Mac version
Useful JavaScript development tools