Home  >  Article  >  Backend Development  >  What to do if php base64 is garbled

What to do if php base64 is garbled

藏色散人
藏色散人Original
2020-07-18 09:35:513426browse

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.

What to do if php base64 is garbled

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: &#39;微软雅黑&#39;; 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[&#39;success&#39;] ){?>
            <h1>:)   恭喜!</h1>
            <p class="success"><?php echo base64_decode($_GET[&#39;message&#39;]); ?></p>
        <?php }else{?>
            <h1>:(   出错了!</h1>
            <p class="error"><?php echo base64_decode($_GET[&#39;message&#39;]); ?></p>
        <?php }?>
        <p class="detail"></p>
        <p class="jump">系统将在 <b id="wait"><?php echo $_GET[&#39;time&#39;]; ?></b> 后跳转,可直接 <a id="href" href="<?php echo base64_decode($_GET[&#39;url&#39;]); ?>">点此跳转</a></p>
        </div><script type="text/javascript">(function(){var wait = document.getElementById(&#39;wait&#39;),href = document.getElementById(&#39;href&#39;).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=&#39;success&#39;, $time=&#39;3&#39;, $url=&#39;/&#39;){
 header(&#39;Location:/include/redirect.php?success=&#39;.$success.&#39;&message=&#39;.base64_encode($message).&#39;&time=&#39;.$time.&#39;&url=&#39;.base64_encode($url));
 exit;
}

If you call the function in PHP like this:

The code is as follows:

$query = "update content set bid=&#39;$clean[bid]&#39;,title=&#39;$clean[title]&#39;,content=&#39;$clean[content]&#39;,path=&#39;$clean[path]&#39; where id=".$clean[&#39;id&#39;];
if( mysql_query($query) ){
 _alert(1,&#39;修改成功&#39;,3,&#39;/admin/manage.php&#39;);
}else{
 _alert(false,&#39;修改失败&#39;.mysql_error(),5,&#39;/admin/manage.php&#39;);
}

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[&#39;str&#39;]));

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!

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