【问题标题】:PHP : insert executable php code in HTMLPHP:在 HTML 中插入可执行的 php 代码
【发布时间】:2017-06-13 08:31:20
【问题描述】:

我得到了简单的缓存脚本,它将 php 代码保存到 HTML 中。一切正常,但我需要将动态跟踪代码添加到每个保存的 HTML 中,方法是使用 include() 语句包含来自另一个文件的脚本

....

$s_getdata = http_build_query( array(
                   'cat' => 'CAT1',
                   'buffit'=>'TRUE',
                 )
             );
$s_page = file_get_contents('http://example.com/buffer.php?'.$s_getdata);
ob_start();
echo $s_page;
file_put_contents('./index.html', ob_get_contents());
ob_end_clean();

我尝试在 ob_start 之前添加此代码,但它只是将代码作为字符串中的简单文本添加(不起作用):

$s_page .= '<?php include("./tr/in.php"); ?>';

知道怎么做吗?谢谢!

【问题讨论】:

  • php 不会在 .html 文件中执行。./tr/in.php 中的内容也许你可以做一个 file_get_contents 如果它只是 html 或者再次做缓冲捕获

标签: php include ob-start


【解决方案1】:

你可以通过两种方式做到这一点,

选项 1:in.php 文件中以函数形式返回值,

您的in.php 文件代码,

function dynatrack() {
   // your dynamic tracking code generation here
   return $code; // where $code is your dynamic tracking code
}

您的原始文件,

$s_page = file_get_contents('http://example.com/buffer.php?'.$s_getdata);
include("./tr/in.php");
$s_page .= dynatrack();

选项2:再次使用file_get_contents获取in.php文件的内容,

$s_page = file_get_contents('http://example.com/buffer.php?'.$s_getdata);
$s_page .= file_get_contents('./tr/in.php');

我希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-17
    • 2018-06-04
    • 1970-01-01
    • 2019-06-27
    相关资源
    最近更新 更多