gpt4 book ai didi

php - PHP 中的 __halt_compiler 有什么用?

转载 作者:IT王子 更新时间:2023-10-29 00:01:00 26 4
gpt4 key购买 nike

来自manual :

void __halt_compiler ( void )

This function halts the execution of the compiler. This can be useful to embed data in PHP scripts, like the installation files.

Note: __halt_compiler() can only be used from the outermost scope.

谁能提供一个这个功能有用的实际案例?

最佳答案

假设您有一个包含一些 php 代码和大量二进制困惑的脚本。

<?php doStuff(); __halt_compliler(); [BIG_BINARY_MESS]

那么您希望编译器不要尝试解析二进制文件,因为如果有 <?它会在二进制文件中的某个地方中断。

关键是能够只发送带有二进制数据和 php 代码的 一个 文件。

有关一个小例子,请参阅 this blog post


因此,您不仅要停止 execution的脚本(如 exit() 会),但要停止 parsing这样您就可以在文件末尾出现“无效语法”,而 php 仍然可以执行第一部分。


另一个例子:

这将被解析为有效的 php 并执行得很好:

<?php $a = 1; echo $a; __halt_compiler(); §RW$FG$%ZDS$TSG$TSZ%U(); §$"§%"§$!!();

访问数据:

<?php
$file = fopen(__FILE__, 'rb');
// Go to the end of the __halt_compiler();
fseek($file, __COMPILER_HALT_OFFSET__);
echo stream_get_contents($file);
__halt_compiler(); §RW$FG$%ZDS$TSG$TSZ%U(); §$"§%"§$!!();

这将输出 §RW$FG$%ZDS$TSG$TSZ%U(); §$"§%"§$!!();

关于php - PHP 中的 __halt_compiler 有什么用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5270486/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com