You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:include_once()无法在HTML页面中运行的原因是什么?

Why include_once() Isn't Working in Your HTML Page

Hey there! Let's break down why your include_once() call isn't behaving as expected in an HTML page—this is a super common pitfall, so you're not alone here. Here are the most likely reasons and fixes:

  • HTML files aren't processed by PHP by default
    Web servers are configured to only run PHP code in files with .php extensions (or other designated extensions) out of the box. If your file ends with .html, the server will send it straight to the browser without parsing any PHP inside—meaning your include_once() line is just treated as plain text (you might even see it in the page source!).
    Fix: Rename your HTML file to use a .php extension (e.g., index.html → index.php). If you need to keep the .html extension, you can configure your server (like Apache or Nginx) to parse HTML files for PHP code (for Apache, add AddHandler application/x-httpd-php .html .htm to your httpd.conf or .htaccess file).

  • Incorrect file path
    Even if you're using a .php file, a wrong path will cause include_once() to fail. Remember: the path is relative to the currently executing PHP script (not the location of your original HTML file, if you renamed it).
    Fix: Use an absolute path to avoid confusion. For example:

    include_once($_SERVER['DOCUMENT_ROOT'] . '/path/to/your/target-file.php');
    

    This uses the server's root directory as a starting point, so you won't run into relative path issues.

  • PHP isn't installed or enabled on your server
    If your server doesn't have PHP installed, or the PHP module isn't enabled, none of your PHP code will run—you'll just see the raw <?php include_once(...); ?> line in the page source.
    Fix: Create a test file named phpinfo.php with this content:

    <?php phpinfo(); ?>
    

    Access it in your browser. If you see a detailed PHP info page, PHP is working. If not, you'll need to install PHP or enable the module on your server.

  • File permission issues
    If the PHP file you're trying to include doesn't have read permissions for the server, include_once() will silently fail (or throw a warning if error reporting is enabled).
    Fix: Check the file permissions—most servers require the included file to have permissions set to 644 (owner can read/write, group and others can read).

Start with checking the file extension first—that's usually the fastest fix. If that doesn't resolve it, work through the other points one by one.

内容的提问来源于stack exchange,提问作者8727

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 06:45:51