Forum Moderators: coopster

Message Too Old, No Replies

Newbie - file extension

index.php works but index.html does not

         

DougW

3:02 pm on Nov 10, 2003 (gmt 0)

10+ Year Member



All of my pages have the same header html. I have put this html in a file named header.inc. I "require" the file in index.php and it works as expected. If I just rename index.php to index.html it doesn't work.

Is the extension "php" required on any file to "process" a php statement?

Thanks

Doug Wilkinson

Sinner_G

3:15 pm on Nov 10, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



When you say you "require" that header.inc, I guess you are talking about server side include. This means that, to the browser that sees the page, there is only one file, including the content of both index.* and header.inc. So if header.inc has PHP, yes, the index has to have a .php extension. Else, the browser is set to read HTML and encounters statements it doesn't understand in that context.

Hmmm, don't know if that was very clear...

Paul in South Africa

3:16 pm on Nov 10, 2003 (gmt 0)

10+ Year Member



You have to tell your server to process htm files as php.

You can do this by adding

AddType application/x-httpd-php .php .php3 .phtml .html .htm

to your .htaccess file if you are running Apache.

davidpbrown

3:18 pm on Nov 10, 2003 (gmt 0)

10+ Year Member



Just add this to your .htaccess or httpd.conf:
AddType application/x-httpd-php .htm .html

Make sure you take any other AddType and AddHandler out as these extensions that might interfere.

Sinner_G: you can use require and include in php

dpb

DougW

3:24 pm on Nov 10, 2003 (gmt 0)

10+ Year Member



Thanks for your help

Sinner_G

3:24 pm on Nov 10, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



you can use require and include in php

AFAIK, it does exactly the same.

Just looked it up, only difference is error handling.

As Doug wrote he wanted to name his file index.html, I thought that had to be an include, as require would work only in a .php (or php-processed) file.

davidpbrown

4:16 pm on Nov 10, 2003 (gmt 0)

10+ Year Member



Sinner_G: why would 'require' only work in a .php?

AddType application/x-httpd-php suggests to the server the .html *is* a php file, the extension is somewhat irrelavant. As I understand it you could have .apples or .pears but for consistency being important.

dpb

Sinner_G

4:22 pm on Nov 10, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Notice the "(or php-processed)"? That's basically what the AddType does, process the .whatever as if it was php.

ergophobe

9:55 pm on Nov 13, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




you can use require and include in php

AFAIK, it does exactly the same.

Just looked it up, only difference is error handling.

Thanks Sinner_G. I was going to correct this, but then I checked it and you're right. I never noticed this change. So now the way it works is that a failed require stops script execution, whereas a failed include just throws an error.

Before PHP 4.0.2 the main difference was that require always functioned, whereas include only functioned if the code was actually executed.

Tom