Solution of decoding garbled code after PHP base64 encoding

  • 2021-07-01 06:53:00
  • OfStack

When doing something with PHP, I found a problem, which can be simply attributed to garbled code, but this problem is not caused by the function itself. Let's see who is the culprit.

Suspects: base64_encode and base64_decode

Crime: I wrote a jump and prompt function, after receiving prompt information, jump to the specified page, but when jumping prompt, Chinese characters are garbled.

The jump template code is as follows:


<!DOCTYPE html><html><head><meta charset="utf-8"><meta name="author" content=" Wang Jian wj@yurendu.com" />
<title> Jump hint </title>
<style type="text/css">
*{ padding: 0; margin: 0; }
body{ background: #fff; font-family: ' Microsoft Yahei '; 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>:) &nbsp; Congratulations! </h1>
            <p class="success"><?php echo base64_decode($_GET['message']); ?></p>
        <?php }else{?>
            <h1>:( &nbsp; There's been a mistake! </h1>
            <p class="error"><?php echo base64_decode($_GET['message']); ?></p>
        <?php }?>
        <p class="detail"></p>
        <p class="jump"> The system will be used in the <b id="wait"><?php echo $_GET['time']; ?></b> Back jump, which can be directly <a id="href" href="<?php echo base64_decode($_GET['url']); ?>"> Click here to jump </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>

The PHP redirect function is defined as follows:


/* Jump after reminding */
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 the function is called like this in PHP:

$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,' Successful modification ',3,'/admin/manage.php');
}else{
 _alert(false,' Modification failed '.mysql_error(),5,'/admin/manage.php');
}

You will see that the Chinese characters "modified successfully" or "modified failed" are garbled.

Why?

Sometimes, after being encrypted with base64_encode, it is transmitted to other pages in the form of GET, and when decrypted with base64_decode, garbled codes appear.

When I met this problem, I wondered why some of them could be decrypted correctly, but some of them appeared garbled codes.

Later, after checking, it was found that there were 1 Chinese characters. When they were passed in the form of GET, the + sign would be replaced with a space.

In order to prevent the occurrence of garbled code, I made a step replacement, and then decryption, sure enough, garbled code problem, no longer exist!

Now the problem is very simple, just write one more step


$str = base64_decode(str_replace(" ","+",$_GET['str']));

Originally, Article 1 began to set the wrong suspect and wronged the two functions. . .

You can also refer to this article: PHP Secure URL String base64 Encoding and Decoding


Related articles: