PHP Base64 code after decoding garbled solution _php tips

Source: Internet
Author: User
Tags base64 php redirect

In the use of PHP to do things when found a problem, can be simply summed up as a garbled problem, but the problem is not the function itself. To see who the culprit is.

Suspect: Base64_encode and Base64_decode

Crime: I wrote a jump and prompt function, received the message to jump to the specified page, but the jump prompts when Chinese characters garbled.

The jump template code is as follows:

Copy Code code as follows:

<! DOCTYPE html><title> Jump Tips </title>
<style type= "Text/css" >
*{padding:0; margin:0;}
body{background: #fff; font-family: ' Microsoft Ya-black '; 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>
<body>
<div class= "System-message" >
<?php if ($_get[' success ']) {?>
<p class= "Success" ><?php Echo base64_decode ($_get[' message '));?></p>
<?php}else{?>
<p class= "Error" ><?php Echo base64_decode ($_get[' message ');?></p>
<?php}?>
<p class= "Detail" ></p>
<p class= "Jump" > System will be <b id= "Wait" ><?php echo $_get[' time ';?></b> after the jump, can be directly <a id= "href" href= "<?php Echo base64_decode ($_get[' url ')";?> "> Point this 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>

The PHP redirect function is defined as follows:

Copy Code code as follows:

/* Reminder after jump * *
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 a function like this in PHP:
Copy Code code 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, ' modified successfully ', 3, '/admin/manage.php ');
}else{
_alert (False, ' modify failed '. Mysql_error (), 5, '/admin/manage.php ');
}

You will see, "Modify success" or "modification failed" these few characters garbled.

Why?

Sometimes with Base64_encode encryption, to get the form to other pages, with Base64_decode decryption, there are garbled.

In the face of this problem, I wonder why some of the correct decryption, but there are some garbled it?

Later, after examination, found that there are some Chinese characters, in the form of get passed, + will be replaced with spaces.

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

Now the question is simple, just write one more step.

Copy Code code as follows:

$str = Base64_decode (Str_replace ("", "+", $_get[' str '));

Originally, the article first set the wrong suspect, wronged the two functions ...

Also refer to this article: PHP secure URL string base64 encoding and decoding

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.