search
HomeBackend DevelopmentPHP ProblemWhat should I do if garbled characters appear when JS reads the Chinese cookies set in PHP?

Solution to the garbled code when JS reads Chinese cookies set in PHP: 1. First use the escape function to encode in PHP, and use unescape decoding in js when reaching the client; 2. Use [setrawcookie( )] function to replace the cookie value.

What should I do if garbled characters appear when JS reads the Chinese cookies set in PHP?

Solution to the garbled code when JS reads the Chinese cookie set in PHP:

In PHP first Use the escape function to encode, and use unescape decoding in js when reaching the client.

The escape function is as follows:

function escape($str)  
{      
       preg_match_all("/[\x80-\xff].|[\x01-\x7f]+/",$str,$r);      
       $ar    =    $r[0];   
        foreach($ar   as   $k=>$v)       
        {            
           if(ord($v[0]) < 128)  
               $ar[$k] = rawurlencode($v);
            else    
               $ar[$k]    =    "%u".bin2hex(iconv("GB2312","UCS-2",$v));   
       }   
       return    join("",$ar);   
}

Example: test.php

<?php
function    escape($str)    {   
   preg_match_all("/[\x80-\xff].|[\x01-\x7f]+/",$str,$r);   
   $ar    =    $r[0];   
   foreach($ar    as    $k=>$v)    {   
    if(ord($v[0])    <    128)   
     $ar[$k]    =    rawurlencode($v);   
    else   
     $ar[$k]    =    "%u".bin2hex(iconv("GB2312","UCS-2",$v));   
   }   
   return    join("",$ar);   
}
$name = escape("深圳人");
setcookie("name", $name);
?>
<scrīpt>
function get_cookie(name)
{
var result = null;
var myCookie = document.cookie + ";";
var searchName = name + "=";
var startOfCookie = myCookie.indexOf(searchName);
var endOfCookie;
if (startOfCookie != -1)
{
   startOfCookie += searchName.length;
   endOfCookie = myCookie.indexOf(";",startOfCookie);
   result = unescape(myCookie.substring(startOfCookie, endOfCookie));
}
return result;
}
</scrīpt>
<scrīpt>
document.write("js:" + unescape(getCookie("name")));
</scrīpt>

There is also another method:

In PHP5 , you can use the setrawcookie() function instead. It is not encoded when setting the cookie value, so there is no need to use the escape function to encode it when setting the cookie. At this time, JS can also read it directly. The value of cookie

Related learning recommendations:PHP programming from entry to proficiency

The above is the detailed content of What should I do if garbled characters appear when JS reads the Chinese cookies set in PHP?. For more information, please follow other related articles on the PHP Chinese website!

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)