I'm having some problems on my website using some php code.
My site is mainly using html and php. Instead of repeating lots of lines of script at the top of every page, i'm just using the php include statement:
<?php include('main.php'); ?>
I have a local WAMP server and a mixture of html and php files on my site.
When I run everything locally it all runs perfectly well and the 'include' statement works.
However, when I upload everything to my site, which is hosted on goDaddy, the include statement only works inside .php files. If I use it inside a .html file it fails. It just doesn't include anything.
I have seen some posts which say that this could be resolved by using a .htaccess file. At the moment I don't have one on my site. I did try to upload one into the root directory but the only effect this had was that it displayed a 'Save As' dialog box when I clicked on a page.
I'm guessing that either my syntax is wrong or I've gone down the wrong road to resolve this.
Below is the contents of my .htaccess file. Any ideas as to how I can get the include statement to work inside .html files would be great. Thanks.
#
# Server root folder www .htaccess
# This file provides server security limiting access to the localhost only.
# Comment next four lines to deactivate. (Allows external access)
#
# Order Deny,Allow
# Deny from all
# Allow from 127.0.0.1
# Allow from ::1
AddType application/x-httpd-php .html .php .htm
# To allow execution of cgi scripts in this directory uncomment next two lines.
# AddHandler cgi-script .pl .cgi
# Options +ExecCGI +FollowSymLinks
# Activate this section to use the Private Server Feature!
# Defaults: Username - root; Password - root
# Note AuthUserFile: File path is relative to server root
# To lock server, uncomment the next 4 lines. (A name and password is required)
#AuthName "Uniform Server - Server Access"
#AuthType Basic
#AuthUserFile ../../../htpasswd/www/.htpasswd
#Require valid-user
Try using this:
AddHandler x-httpd-php5-cgi .html
See here: http://httpd.apache.org/docs/2.2/mod/mod_mime.html#addhandler
All you have to do is change all of the files you are including it to .php files. It will not affect them negatively just the browser will then know to interpret it the php.
All your formatting will still be valid.
If you use <?php include ... ?>
inside an HTML file it won't work. Reason? If the file has a .html
extension, the browser will not be expecting PHP, so it won't do anything. If you have any PHP code in any file, give that file a .php
extension. (Unless you have a specific reason for not doing this...?)
来源:https://stackoverflow.com/questions/16041729/use-of-php-include-in-html-files