Home  >  Article  >  Backend Development  >  Solution to decoding garbled characters after PHP base64 encoding_PHP tutorial

Solution to decoding garbled characters after PHP base64 encoding_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:24:311257browse

I found a problem when I was making something with PHP. 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.

Suspects: base64_encode and base64_decode

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

The jump template code is as follows:

Copy code The code is as follows:


Jump prompt




              
& Lt; h1 & gt; :) Congratulations!
                


                                                                                                                                                                                                                                      


                                                                                                                                                                                        

The system will jump after , you can directly Click here to jump


                             







PHP redirect function is defined as follows:

Copy code

The code is as follows: /* Jump after reminder */ 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 like this in PHP:


Copy code
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,'Modification successful',3,'/admin/manage.php');
}else{
_alert(false,'Modification failed'.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 wondered why some could decrypt correctly, but some were garbled?

After checking later, I found that there are some Chinese characters. When passed in GET form, the + sign will be replaced by a space.

In order to prevent garbled codes, I made a step of substitution and then decrypted it. Sure enough, the problem of garbled codes no longer existed!

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

Copy code 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. . .

You can also refer to this article: PHP secure URL string base64 encoding and decoding

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/825406.htmlTechArticleI discovered a problem when making things with PHP. It can be simply attributed to the problem of garbled characters, but this problem It's not caused by the function itself. Let’s find out who the culprit is. Suspect:...
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