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);
<?php $username_php=$_GET['sha1_html']; echo $username_php; echo '<br/>'; echo strlen($username_php); echo '<br/>'; echo bin2hex($username_php); echo '<br/>';?>
该代码端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>";}打印 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/>';?>
不大理解你的疑问
浏览器会将收到的文本数据转成 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;?>
我将收到的sh转换为SS后,可以输出true,但是我把echo $SS返回到浏览器端var mm=xmlhttp1.responseText.substring(4);则出现mm和JS端sha1不一样(转换出来的16进制字符串也不一样),我按照您的指点,将此处mm理解为unicode模式,我试着将它转换成Latin1编码结果一直也不成功。

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

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),

SublimeText3 Chinese version
Chinese version, very easy to use

WebStorm Mac version
Useful JavaScript development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver Mac version
Visual web development tools
