【问题标题】:Cannot include PHP file to my HTML无法在我的 HTML 中包含 PHP 文件
【发布时间】:2016-04-01 03:42:45
【问题描述】:

我正在尝试访问我的 function.php 文件,以便能够使用其中的函数。但我无法连接它。我尝试单独运行PHP,没有遇到任何问题。但是当我将它包含在我的 HTML 中时,什么都没有发生。

HTML 代码:

 <!DOCTYPE html>
<?php 
include("functions/function.php");
?>
<html>
    <head>
    <title>Online Shop</title>
    <link rel='stylesheet' href='../CSS/style.css' media="all"/>
    </head>
<body>  
            <!--Main Wrapper starts here!-->
    <div class="main_wrapper"> 

            <!--Header Wrapper starts here!-->
        <div class="header_wrapper" >
        <img id="logo" src="../Pictures/logo.png"/>
        <img id="banner" src="../Pictures/green-banner.jpg"/>
        </div>
            <!--Header ends here!-->

            <!--Menu Bar starts here!-->
        <div class="menubar">
        <ul id="menu">
            <li><a href="#"> Home </a></li> 
            <li><a href="#"> All Products </a></li>
            <li><a href="#"> My Account </a></li>
            <li><a href="#"> Sign Up </a></li>
            <li><a href="#"> Shopping Cart </a></li>
            <li><a href="#"> Contact Us  </a></li>
        </ul>
        <div id="form">
            <form method="get" action="results.php" enctype="multipart/form-data">
                <input type="text" name="user_query"  placeholder="Search Product"/>
                <input type="submit" name="search" value="search"/>
            </form> 
        </div>
        </div><!--Menubar ends here!-->

            <!--Content Wrapper here!-->
        <div class="content_wrapper">
            <div id="sidebar"> 
                <div id="sidebar_title"> Categories</div>
                <ul id="cats">
                    <?php getCats(); ?>             
                </ul>   
            </div>
            <div id="content_area">THis is asa content</div>
        </div><!--Content Wrapper ends here!-->
        <div id="footer">  </div>
    </div><!--Wrapper-->
</body>
</html>

PHP 代码:

<?php

$con = mysqli_connect("localhost","root","","ecommerce");
//Getting Categories
function getCats(){

global $con;
$get_cats = "Select * from categories";

$run_cat = mysqli_query($con, $get_cats);

while ($row_cats=mysqli_fetch_array($run_cat)){

    $cat_id = $row_cats['cat_id'];
    $cat_title = $row_cats['cat_title'];
echo"<li><a href='#'>$cat_title</a></li>";
}
} //getCats() ENDS HERE
?>

其他信息:

我的 HTML 路径:C:/xampp/htdocs/ProjectWebDev/HTML/index.html

我的 PHP 路径:C:/xampp/htdocs/ProjectWebDev/functions/function.php

【问题讨论】:

  • include("../functions/function.php");

标签: php html xampp


【解决方案1】:

“但是当我将它包含在我的 HTML 中时,什么都没有发生。”

您需要指示 Apache 将 .html 文件视为 PHP。

如果你还没有,那就去做吧。 .html 文件默认不解析 PHP 指令。

咨询:Using .htaccess to make all .html pages to run as .php files?

并创建一个名为 .htaccess 的文件,并将其放置在服务器的根目录中,其中包含以下内容:

AddHandler application/x-httpd-php .html

如果您决定使用 .htaccess 方法,请记住重新启动您的服务器/服务/Apache/PHP。在您执行此操作之前,这些更改不会生效。

另外,请确保您已安装并正确配置了网络服务器/PHP。

如果您尝试在网络浏览器中访问您的文件,例如:

c://file.htmlc://file.php,那就不行了。

必须这样使用,例如:

  • http://localhost|example.com/file.html

  • http://localhost|example.com/file.php

还要确保包含的路径正确。

error reporting 添加到文件顶部,这将有助于查找错误。

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// rest of your code

旁注:显示错误应该只在暂存阶段完成,而不是在生产阶段。

【讨论】:

  • "在您网站的根目录创建一个 .htaccess 文件并添加此行:已接受反对票 在您网站的根目录创建一个 .htaccess 文件并添加此行:[Apache2 @ Ubuntu/Debian :使用此指令] AddType application/x-httpd-php .html .htm 如果您将 PHP 作为 CGI 运行(可能不是这种情况),您应该改为: AddHandler application/x-httpd-php .html .htm “先生,我完全不明白这一点。创建 .htaccess 文件是否与创建 html 和 php 相同?还有,他说的是什么意思:“在您网站的根目录创建一个 .htaccess 文件并添加这一行”
  • @user4932301 它包含在我在答案中添加的链接stackoverflow.com/questions/4687208/…
  • 您创建一个基于文本的文件.htaccess(带有类似的点)并将其放在服务器的根目录中。如果您不想遇到这种麻烦,只需将您的.html 文件扩展名重命名为.php。就像我在回答中提到的那样,默认情况下,Web 浏览器不会解析 PHP 指令,服务器会这样做。
  • 现在可以使用了!谢谢弗雷德-ii-!你对 stackoverflow 有很大帮助:D
  • @user4932301 不客气(谢谢),cheers 很高兴我能帮上忙。编码快乐/节日快乐/新年快乐 ;-)
【解决方案2】:

试试这个,并将您的 HTML 文件格式更改为 .php。我希望它会起作用。

<?php 
include("../functions/function.php");
?>

【讨论】:

  • 这和我 10 分钟前写的一样
  • 网络出现问题,对不起,我没有抄袭你。
【解决方案3】:

好像路径不对,你应该使用include("../functions/function.php");

您还需要将.html 文件重命名为.php(如果您的服务器中没有不同的配置)

【讨论】:

  • 如何在我的服务器中配置它?
  • 您不需要,但如果您想为您的确切服务器找到解决方案,我无能为力
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-14
  • 2010-09-23
  • 1970-01-01
  • 1970-01-01
  • 2015-11-06
  • 1970-01-01
相关资源
最近更新 更多