search
Homephp教程php手册PHP base64编码后解码乱码的解决办法

在用PHP做东西的时候发现了一个问题,可以简单的归结为乱码的问题,但是这个问题不是函数本身造成的。来看看罪魁祸首是谁。

嫌疑人:base64_encode 和 base64_decode

罪行:我写了一个跳转和提示函数,接收提示信息后跳转到指定的页面,但是跳转提示时汉字乱码。

跳转模版代码如下:

复制代码 代码如下:



跳转提示



   

       
           

:)   恭喜!


           


       
           

:(   出错了!


           


       
       


       

系统将在 后跳转,可直接 点此跳转


       
   




PHP redirect函数定义如下:

复制代码 代码如下:


/* 提醒后跳转 */
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;
}


假如在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,'修改成功',3,'/admin/manage.php');
}else{
 _alert(false,'修改失败'.mysql_error(),5,'/admin/manage.php');
}

你就会看到,“修改成功”或者是“修改失败”这几个汉字乱码了。

为什么?

有时候用base64_encode加密后,以GET的形式传到其他页面,用base64_decode解密的时候,出现乱码。

遇到这个问题的时候,我就纳闷了,为什么有一些能正确解密,但是有一些却出现乱码呢?

后来经过检查,发现有一些中文字符,用GET形式传过来的时候,+号会被替换成空格。

为了防止出现乱码的情况,我做了一步替换,然后再解密,果然,乱码的问题,不复存在了!

现在问题已经很简单了,只要多写一步就好了

复制代码 代码如下:


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

原来,文章一开始定错了嫌疑人了,冤枉了那两个函数了。。。

还可以参考这篇文章:PHP安全的URL字符串base64编码和解码

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

DVWA

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