Table of Contents
:)   恭喜!
:(   出错了!
Home php教程 php手册 PHP base64编码后解码乱码的解决办法

PHP base64编码后解码乱码的解决办法

Jun 13, 2016 am 09:30 AM
base64 php Garbled characters decoding

在用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 of this Website
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 ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

CakePHP Logging CakePHP Logging Sep 10, 2024 pm 05:26 PM

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

See all articles