求助:include_once()无法在HTML页面中运行的原因是什么?
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.phpextensions (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 yourinclude_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.phpextension (e.g.,index.html→index.php). If you need to keep the.htmlextension, you can configure your server (like Apache or Nginx) to parse HTML files for PHP code (for Apache, addAddHandler application/x-httpd-php .html .htmto yourhttpd.confor.htaccessfile).Incorrect file path
Even if you're using a.phpfile, a wrong path will causeinclude_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 namedphpinfo.phpwith 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 to644(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

