如何将包含 php 标头格式/函数的 php 代码包含在 HTML 分区中


How to include a php code containing php header format/function into a HTML division

>让我们对此进行采样.php

<?php
    $con=mysql_connect("localhost","root","");
    mysql_select_db("final_year_sample",$con);
    $qt=mysql_query("select * from gd_graph");
    header("Content-Type:image/jpg");
    $x_gap=40;
    $x_max=$x_gap*13;
    $y_max=250;
    $im = ImageCreate($x_max, $y_max) or die ("Cannot Initialize new GD image stream");
    $background_color = ImageColorAllocate($im, 234, 234, 234);
    $text_color = ImageColorAllocate($im, 233, 14, 91);
    $graph_color = ImageColorAllocate($im,25,25,250);
    $x1=0;
    $y1=0;
    $first_one="yes";
    while($nt = mysql_fetch_array($qt)){
        $x2=$x1+$x_gap;
        $y2=$y_max-$nt['sales'];
        ImageString($im,2,$x2,$y2,$nt['month'],$graph_color); 
        if($first_one=="no"){
            imageline($im,$x1, $y1,$x2,$y2,$text_color);
        }
        $x1=$x2;
        $y1=$y2;
        $first_one="no";
    }
    ImageJPEG($im);
?>

上面的示例.php代码运行良好,输出为 从上述代码生成的输出

我想做的是:

我想在下面的 HTML 页面中包含示例.php

    <html>
    <head>
        <title>Front page</title>
        <link rel="stylesheet" href="style.css">
    </head>
    <body>
        <div id="container">
            <span id="title">Prototype for vehicle Routing problem Solution</span>
            <div style="display:none;" id="graph_space">
                <?php
                    include('sample.php');
                ?>
            </div>
        </div>
    </body>
</html>

但是我收到以下错误,

当我尝试将示例包含在.php

您不能以这种方式在 html 文件中包含 PHP 文件,因为 html 文件无法识别<?php ?>标签

虽然你可以尝试这样做在目录中创建一个 .htaccess 文件,并将此代码添加到 .htaccess 文件

AddHandler x-httpd-php .html .htm

AddType application/x-httpd-php .html .htm

它将强制Apache服务器将HTML或HTM文件解析为PHP脚本

您必须

将文件声明为包含"HTML"的.php页面。更改: page.html > page.php

然后你的包含将运行,它不能在静态 HTML 页面上运行。想一想,哟。