Table of Contents
谁能帮忙详细讲解下其中的原理吗?
回复内容:
Home Backend Development PHP Tutorial javascript - %0a(换行符)的执行解析过程

javascript - %0a(换行符)的执行解析过程

Jun 06, 2016 pm 08:40 PM
javascript php Character Encoding Browser

test.php文件的代码如下:

<code>


<!--情况1-->


<script type="text/javascript">
//var a = "<?php echo $_GET['input'];?>";
</script>



<!--情况2-->


<script type="text/javascript">
//var a = "start_%0a_end";
</script>





</code>
Copy after login
Copy after login

情况1:
变量a的值是可控的,由参数input决定,
所以,当从浏览器访问:http://127.0.0.1/test.php?input=start_%0a_end
的时候
查看网页源代码,结果如下:

<code>//省略……
<!--情况1-->


<script type="text/javascript">
//var a = start_
_end;
</script>



<!--情况2-->


<script type="text/javascript">
//var a = "start_%0a_end";
</script>


//省略……
</code>
Copy after login
Copy after login

我们可以发现,情况1中,出现了换行,而情况2保持原样。
为什么会出现这种情况呢?

谁能帮忙详细讲解下其中的原理吗?

说下我对此问题的看法:
当客户端访问网址(http://127.0.0.1/test.php?input=start_%0a_end)的时候,由于是php文件,所以服务端会交个Apache服务器解析执行,并把结果返回给服务端,服务端再将结果通过http响应返回给客户端(浏览器),浏览器再将页面渲染出来,呈现给用户。
用户--->浏览器--->服务器--->apache
那么,
换行到底是:
1.出现在Apache的解析执行的阶段
2.还是浏览器将最终结果呈现给用户这一阶段
如果是出现在Apache的解析执行阶段,那么<?php echo "start_%0a_end";?>应该也会出现换行,但实际上并没有
而如果换行是出现在:浏览器的渲染阶段,那么

<code><br><br><script type="text/javascript">
var a = "start_%0a_end";//注意:此行没有注释
</script>


</code>
Copy after login
Copy after login

应该也会出现换行才是,但实际上也没有

回复内容:

test.php文件的代码如下:

<code>


<!--情况1-->


<script type="text/javascript">
//var a = "<?php echo $_GET['input'];?>";
</script>



<!--情况2-->


<script type="text/javascript">
//var a = "start_%0a_end";
</script>





</code>
Copy after login
Copy after login

情况1:
变量a的值是可控的,由参数input决定,
所以,当从浏览器访问:http://127.0.0.1/test.php?input=start_%0a_end
的时候
查看网页源代码,结果如下:

<code>//省略……
<!--情况1-->


<script type="text/javascript">
//var a = start_
_end;
</script>



<!--情况2-->


<script type="text/javascript">
//var a = "start_%0a_end";
</script>


//省略……
</code>
Copy after login
Copy after login

我们可以发现,情况1中,出现了换行,而情况2保持原样。
为什么会出现这种情况呢?

谁能帮忙详细讲解下其中的原理吗?

说下我对此问题的看法:
当客户端访问网址(http://127.0.0.1/test.php?input=start_%0a_end)的时候,由于是php文件,所以服务端会交个Apache服务器解析执行,并把结果返回给服务端,服务端再将结果通过http响应返回给客户端(浏览器),浏览器再将页面渲染出来,呈现给用户。
用户--->浏览器--->服务器--->apache
那么,
换行到底是:
1.出现在Apache的解析执行的阶段
2.还是浏览器将最终结果呈现给用户这一阶段
如果是出现在Apache的解析执行阶段,那么<?php echo "start_%0a_end";?>应该也会出现换行,但实际上并没有
而如果换行是出现在:浏览器的渲染阶段,那么

<code><br><br><script type="text/javascript">
var a = "start_%0a_end";//注意:此行没有注释
</script>


</code>
Copy after login
Copy after login

应该也会出现换行才是,但实际上也没有

@安坚实 的答案,query string由于地址栏显示、日志记录、或者转义等各方面的需要,必须将部分字符进行翻译(比如无法显示的字符、有特殊含义的控制字符等)

所以你在百度搜索一个&符号的时候 访问到的链接 实际是 http://www.baidu.com/s?wd=%26
因为这个字符与query string中的参数连接符冲突了,需要进行转义

这个过程就是一个编码的过程,这样的编码算法最常见的就是 URLEncodeBase64Encode
而此处使用的是 URLEncode,这个是在 RFC 3986 中定义的

服务端收到了这个请求时,先原样记录在日志中,然后将参数变成应用程序里的字符串 交给web应用处理
这个时候就需要进行一个解码过程,否则得到的数据就与预期不一致了

接上面那个例子,我想搜索的是 字符串& 而不是 字符串%26,因此需要解码变回 &

再解释LZ的例子,浏览器中访问http://127.0.0.1/test.php?input=start_%0a_end时,
其实input这个参数的实际值 并不是 start_%0a_end 这个字符串,只是因为地址栏无法显示换行符,将换行符进行了转义, 他的实际值就是start_[换行]_end,页面输出时,将他还原成了[换行]

如果你想要指定 input参数的实际值为 start_%0a_end, 需要将% 做一次转义,变为 %25

尝试访问一下 http://127.0.0.1/test.php?input=start_%250a_end

解析过程其实是这样的:
用户 --> 浏览器 --> 服务器 --> apache --> PHP解释器

首先,start_%0a_end 被传递给PHP解释器时,%0a 并没有被转换成换行。

<code>var_dump($_SERVER['QUERY_STRING']);   
# string(19) "input=start_%0a_end"
</code>
Copy after login

但是,当它被写入到 $_GET 全局数组里时,就变成换行了

<code>var_dump($_GET['input']); 
#string(11) "start_
#_end"
</code>
Copy after login

因此,应该是PHP解释器在把 query string 的值写入 $_GET 里时,进行了某些处理
至于为什么进行这样的处理,以及如何解决... 这个超出我的知识范围了...

其实这里只涉及到一个知识点,URIEncode,
URI内的字符在传输时会进行转义,当处理程序收到数据时会进行反转义;
%0a 的转义过程大致如下(JS,PHP的过程 @安坚实 已解释了部分):

<code>// encode
encodeURIComponent('\n') // %0A
encodeURIComponent('\n').toLowerCase() === '%0a' // true

// decode
decodeURIComponent('%0a').charCodeAt(0)  // 10
'\n'.charCodeAt(0) // 10
</code>
Copy after login

关于 JS URI 编码部分可见:
http://www.ruanyifeng.com/blog/2010/02/url_encoding.html

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)

The Compatibility of IIS and PHP: A Deep Dive The Compatibility of IIS and PHP: A Deep Dive Apr 22, 2025 am 12:01 AM

IIS and PHP are compatible and are implemented through FastCGI. 1.IIS forwards the .php file request to the FastCGI module through the configuration file. 2. The FastCGI module starts the PHP process to process requests to improve performance and stability. 3. In actual applications, you need to pay attention to configuration details, error debugging and performance optimization.

What to do if the USDT transfer address is incorrect? Guide for beginners What to do if the USDT transfer address is incorrect? Guide for beginners Apr 21, 2025 pm 12:12 PM

After the USDT transfer address is incorrect, first confirm that the transfer has occurred, and then take measures according to the error type. 1. Confirm the transfer: view the transaction history, obtain and query the transaction hash value on the blockchain browser. 2. Take measures: If the address does not exist, wait for the funds to be returned or contact customer service; if it is an invalid address, contact customer service and seek professional help; if it is transferred to someone else, try to contact the payee or seek legal help.

How to register an account on Ouyi Exchange Ouyi Exchange Registration Tutorial How to register an account on Ouyi Exchange Ouyi Exchange Registration Tutorial Apr 24, 2025 pm 02:06 PM

The steps to register an Ouyi account are as follows: 1. Prepare a valid email or mobile phone number and stabilize the network. 2. Visit Ouyi’s official website. 3. Enter the registration page. 4. Select email or mobile phone number to register and fill in the information. 5. Obtain and fill in the verification code. 6. Agree to the user agreement. 7. Complete registration and log in, carry out KYC and set up security measures.

Binance download link Binance download path Binance download link Binance download path Apr 24, 2025 pm 02:12 PM

To safely download the Binance APP, you need to go through the official channels: 1. Visit the Binance official website, 2. Find and click the APP download portal, 3. Choose to scan the QR code, app store, or directly download the APK file to download to ensure that the link and developer information are authentic, and enable two-factor verification to protect the security of the account.

Can two exchanges convert coins to each other? Can two exchanges convert coins to each other? Can two exchanges convert coins to each other? Can two exchanges convert coins to each other? Apr 22, 2025 am 08:57 AM

Can. The two exchanges can transfer coins to each other as long as they support the same currency and network. The steps include: 1. Obtain the collection address, 2. Initiate a withdrawal request, 3. Wait for confirmation. Notes: 1. Select the correct transfer network, 2. Check the address carefully, 3. Understand the handling fee, 4. Pay attention to the account time, 5. Confirm that the exchange supports this currency, 6. Pay attention to the minimum withdrawal amount.

What is on-chain transaction? What are the global transactions? What is on-chain transaction? What are the global transactions? Apr 22, 2025 am 10:06 AM

EU MiCA compliance certification, covering 50 fiat currency channels, cold storage ratio 95%, and zero security incident records. The US SEC licensed platform has convenient direct purchase of fiat currency, a ratio of 98% cold storage, institutional-level liquidity, supports large-scale OTC and custom orders, and multi-level clearing protection.

Web3 trading platform ranking_Web3 global exchanges top ten summary Web3 trading platform ranking_Web3 global exchanges top ten summary Apr 21, 2025 am 10:45 AM

Binance is the overlord of the global digital asset trading ecosystem, and its characteristics include: 1. The average daily trading volume exceeds $150 billion, supports 500 trading pairs, covering 98% of mainstream currencies; 2. The innovation matrix covers the derivatives market, Web3 layout and education system; 3. The technical advantages are millisecond matching engines, with peak processing volumes of 1.4 million transactions per second; 4. Compliance progress holds 15-country licenses and establishes compliant entities in Europe and the United States.

How to register an account on Sesame Open Exchange? Tutorial on Registration of Sesame Open Exchange How to register an account on Sesame Open Exchange? Tutorial on Registration of Sesame Open Exchange Apr 24, 2025 pm 02:00 PM

Registering a Sesame Door Account requires 7 steps: 1. Prepare a valid email or mobile phone number and a stable network; 2. Visit the official website; 3. Enter the registration page; 4. Select and fill in the registration method; 5. Obtain and fill in the verification code; 6. Agree to the user agreement; 7. Complete registration and log in, it is recommended to carry out KYC and set security measures.

See all articles