2

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
Eddie
  • 611
  • 3
  • 13
  • 23

3 Answers3

2

Try using this:

AddHandler x-httpd-php5-cgi .html

See here: http://httpd.apache.org/docs/2.2/mod/mod_mime.html#addhandler

Prisoner
  • 27,391
  • 11
  • 73
  • 102
2

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.

Adam Brown
  • 2,812
  • 4
  • 28
  • 39
  • Even though @Prisoner suggestion worked, is it better to change the file extension to .php? – Eddie Apr 16 '13 at 16:23
  • I don't know much about .htaccess so I don't know what it would do now you have changed it. However if you have files with php in them there really is no point in them not being called .php. Unless you have ten thousand and have to change them all :? – Adam Brown Apr 16 '13 at 17:35
1

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...?)

imulsion
  • 8,820
  • 20
  • 54
  • 84
  • But I do have the include statement inside a .html file on my local WAMP server and it works just fine. Apart from the include statement, I don't have any other php code inside of the .html file. I don't mind changing the extension to .php but being new to this, I wasn't sure if it's the right thing to do. – Eddie Apr 16 '13 at 16:13
  • Don't really know why it works on WAMP...but you should always give files with PHP code in them `.php` extensions unless you have a good reason not to – imulsion Apr 16 '13 at 16:29