Home > Backend Development > PHP Tutorial > PHP中cookie传值出现乱码有关问题

PHP中cookie传值出现乱码有关问题

WBOY
Release: 2016-06-13 10:45:59
Original
1277 people have browsed it

PHP中cookie传值出现乱码问题
$username=mb_convert_encoding($row1[0], "GBK", "UTF-8");
setcookie('username',$username);
这个通过setcookie在页面上会出现乱码
但是通过setrawcookie会报错,未定义函数的错误
请教一下如何写cookie以及如何读取cookie

------解决方案--------------------
你设置的 cookie 是给 js 用的吗?
如果是,出现乱码是你读取 cookie 的函数有问题
如果是个 php 用的,那么本来 utf-8 编码的内容被你转成 gbk 的了。再读回来不还是 gbk 的吗?不转回来自然就是乱码了
------解决方案--------------------
楼上的正解啊,$username=mb_convert_encoding($row1[0], "GBK", "UTF-8");
setcookie('username',$username);
楼主可以试一下 setrawcookie()这个行不行。其实输出的时候转码就可以了
$a=mb_convert_encoding($_COOKIE["username"],"UTF-8" , "GBK");就ok了试试行不行

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template