TIP: __COMPILER_HALT_OFFSET__ constant is used to get the beginning of the data bytes. Requires __halt_compiler() to be used. Next, let’s talk about a specific example of a php installation file:According to the introduction in the PHP manual, this function is commonly used to embed data in scripts, similar to installation files. That is to say, after __halt_compiler();, put some files that do not need to be compiled, such as binary noise (clutter), compressed files and other types of files. Such as the following code:
// open this file $fp = fopen(__FILE__, 'r'); // seek file pointer to data fseek($fp, __COMPILER_HALT_OFFSET__); // and output it var_dump(stream_get_contents($fp)); // the end of the script execution __halt_compiler(); the installation data (eg. tar, gz, PHP, etc.)Copy after login
Before the introduction of __halt_compiler() in php5.1, files compressed using gzdeflat() often contained files that could not be parsed by the php interpreter (parser ) to read the special ASCII code, causing an error. In order to prevent errors, base64_encode() is used to encode the data generated by gzdeflate(), which will increase the file size by approximately 33%. This is a waste of memory.
With __halt_compiler(), we can no longer use base64_encode() to encode, but directly put the data after __halt_compiler(), so that it will not be compiled, resulting in Wrong.$packed = base64_encode(gzdeflate('the old package')); //unpacked $unpacked = base64_decode(gzinflate($packed));Copy after login// 打开脚本自身文件 $fp = fopen(__FILE__, 'rb'); // 找到数据在文件中的指针 //__COMPILER_HALT_OFFSET__ 将会返回 //__halt_compiler();之后的指针 fseek($fp, __COMPILER_HALT_OFFSET__); // 输出文件 $unpacked = gzinflate(stream_get_contents($fp)); __halt_compiler(); //now here... all the binary gzdeflate already items!!!Copy after login
The above introduces some summaries of __halt_compiler, including aspects of content. I hope it will be helpful to friends who are interested in PHP tutorials.