Table of Contents
回复讨论(解决方案)
Home Backend Development PHP Tutorial 关于Latin1编码在JS和php端不一致问题

关于Latin1编码在JS和php端不一致问题

Jun 20, 2016 pm 12:39 PM

var sha1=CryptoJS.SHA1("abc");document.write("sha1:",sha1);document.write("<br>");var sha1_Latin=sha1.toString(CryptoJS.enc.Latin1);document.write("sha1_Latin:",sha1_Latin);document.write("Latin.len:",sha1_Latin.length);document.write("<br>");var sha1_hex=strToHex(sha1_Latin);//此处为自己定义的一个函数,将字符串转换为ASCII的16进制形式document.write("hex:",sha1_hex);document.write("<br>");var xmlhttp1;if (window.XMLHttpRequest)  {  xmlhttp1=new XMLHttpRequest();  }else  {  xmlhttp1=new ActiveXObject("Microsoft.XMLHTTP");  }	xmlhttp1.open("GET","web/tes.php?sha1_html="+sha1_Latin,false);//发送给php    xmlhttp1.send();    document.write(xmlhttp1.responseText);
Copy after login

<?php  $username_php=$_GET['sha1_html'];	echo $username_php;	echo '<br/>';	echo strlen($username_php);	echo '<br/>';	echo bin2hex($username_php);	echo '<br/>';?>  
Copy after login



该代码端JS自主输出的
sha1_Latin: / /此段为sha1(“abc”)生成的字符串,CSDN提示有特殊符号,此处输出省略
Latin.len:20
hex:a9993e364706816aba3e25717850c26c9cd0d89d//此段为sha1_Latin的16进制字符串形式,可以确定此处的hex转换是正确的

而通过AJAX传输到php后在浏览器端输出的
 //此处看着仍和前面的sha1_Latin对应,CSDN提示有特殊符号,此处输出省略
29//此处的长度不对应了
c2a9c2993e364706c2816ac2ba3e25717850c3826cc29cc390c398c29d//转换为16进制发现和前面的hex不一样,观察发现多了一些c2,c3,请问是为什么?????


回复讨论(解决方案)

浏览器的工作字符集是 unicode
传入的任何字符集内容都将在浏览器中自动转换成 unicode
比如

$gbk = '中文';$utf8 = iconv('gbk', 'utf-8', $gbk);if(isset($_GET['ch'])) {  echo "utf8 = '$utf8'; document.write(gbk == utf8);";}else {  echo "<script>gbk = '$gbk';</script>";  echo "<script src=?ch=1 charset=utf-8></script>";}
Copy after login
打印 true


浏览器的工作字符集是 unicode
传入的任何字符集内容都将在浏览器中自动转换成 unicode


感谢指导,我在PHP端将写成如下代码,则SS和JS端生成的sha1_html完全一样了,不过还是没太完全理解你的意思,你是说从浏览器端发送到PHP端的Latin1编码被自动转换成了UTF-8?所以我在PHP端将接收到的数据的编码模式由UTF-8转换成Latin1模式以后就正确了?
那么我还有个疑问,就是我echo $SS;返回到浏览器端的字符编码模式也会被自动转换为UTF-8,我需要再将其在浏览器端转换成Latin1模式?
我写了下面的php端代码

<?php  $sh=$_GET['sha1'];$sh1=sha1("abc",true);$SS=iconv("UTF-8","ISO-8859-1",$sh);	echo $SS;	echo '<br/>';	echo strlen($SS);	echo '<br/>';	echo bin2hex($SS);	echo '<br/>';?>  
Copy after login

不大理解你的疑问

浏览器会将收到的文本数据转成 unicode
浏览器在发送数据(比如表单提交)时会将 unicode 转成页面声明的字符集

如果你在 php 中按字节处理数据,那么要想得到和 js 处理相同的结果
需要将待处理的文字转成 ucs-2 字符集

不大理解你的疑问

浏览器会将收到的文本数据转成 unicode
浏览器在发送数据(比如表单提交)时会将 unicode 转成页面声明的字符集

如果你在 php 中按字节处理数据,那么要想得到和 js 处理相同的结果
需要将待处理的文字转成 ucs-2 字符集



<?php  $sh=$_GET['sha1'];$a="abc";$sh1=sha1("abc",true);$SS=iconv("UTF-8","ISO-8859-1",$sh);if($SS==$sh1)	echo "true";	echo $SS;?> 
Copy after login

我将收到的sh转换为SS后,可以输出true,但是我把echo $SS返回到浏览器端var mm=xmlhttp1.responseText.substring(4);则出现mm和JS端sha1不一样(转换出来的16进制字符串也不一样),我按照您的指点,将此处mm理解为unicode模式,我试着将它转换成Latin1编码结果一直也不成功。

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

Video Face Swap

Video Face Swap

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

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)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

What are Enumerations (Enums) in PHP 8.1? What are Enumerations (Enums) in PHP 8.1? Apr 03, 2025 am 12:05 AM

The enumeration function in PHP8.1 enhances the clarity and type safety of the code by defining named constants. 1) Enumerations can be integers, strings or objects, improving code readability and type safety. 2) Enumeration is based on class and supports object-oriented features such as traversal and reflection. 3) Enumeration can be used for comparison and assignment to ensure type safety. 4) Enumeration supports adding methods to implement complex logic. 5) Strict type checking and error handling can avoid common errors. 6) Enumeration reduces magic value and improves maintainability, but pay attention to performance optimization.

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

See all articles