Page 1 of 1

Question about file extensions

Posted: Tue Oct 16, 2007 4:17 pm
by RyanP
I am new to PHP and am not having as many questions about the syntax/code as I am about overall implementation. I'm hoping somebody can give me some insight into this question:

I assume you need a .php extension on your page to run php code in the page. So if you want to use sessions and session variables, it would seem every page in your site would need to be .php (and not .html). On each page of the site, I want to echo that the user is logged in (something like "Logged in as user"). I feel like I see sites that can do that as an .html page, but maybe I'm wrong.

I don't mind making every page a .php page, but was just curious if there's a better way I should be managing it. Thanks for your help.
Ryan

Re: Question about file extensions

Posted: Tue Oct 16, 2007 4:35 pm
by Oren
RyanP wrote:I feel like I see sites that can do that as an .html page, but maybe I'm wrong.
No, you are not wrong. It is possible, you need to tell your server to treat .html files just like if they were .php files.

Posted: Tue Oct 16, 2007 4:44 pm
by superdezign
File extensions mean nothing. They are only used for quick identification of file types. You can rename a file and it'll still have the exact same data. If you made your image editing software recognize *.blah files as JPEGs, it'd load the data the same way. The same goes for your server. php.exe is a program and needs to know which files to be run on.

Posted: Tue Oct 16, 2007 5:07 pm
by mrkite
You could also do a server-side include

Code: Select all

<!--#include virtual="/header.php" -->
in html to include a php header on a static html page... assuming your apache has includes enabled.

Posted: Tue Oct 16, 2007 5:13 pm
by RyanP
Thanks for the help. And that raises an additional question I have...I know I have control over my php.ini file and my local apache environment, but when I host the site with a service provider, who determines the php settings then? Will I have a control panel to modify those myself, or will the service provider dictate that stuff? (For example, would there be a way to have my hoster allow me to run html files as php?) Thanks!

Posted: Tue Oct 16, 2007 5:20 pm
by superdezign
Depends. If you are going to be on a shared host (which I assume you will be), then you will have absolutely no control over it, and you will not be able to modify the php.ini file.

Posted: Tue Oct 16, 2007 5:25 pm
by RobertGonzalez
Most hosts will probably not allow HTML files to be interpreted as PHP because it could have an effect on the entire server. It is not that huge of an issue for a single site to have every requested html file sent through the PHP engine. But when there are 50 on a box, it could have some serious implications.

There is a possibility that you could use some rewrite rules in a .htaccess file to push any extension file into a .php file and your users would be none the wiser.

Posted: Tue Oct 16, 2007 5:28 pm
by RyanP
Yes, it is a shared host. I would like to thank mrkite--that suggestion worked perfectly on my shared host. Thanks to everyone else too.

Posted: Tue Oct 16, 2007 6:17 pm
by RyanP
My mistake--my host actually didn't allow that line of code to work.

That said, I guess I'll just make every page a .php page. Is that a problem (or unprofessional)?

Posted: Tue Oct 16, 2007 6:33 pm
by RobertGonzalez
There is nothing wrong with making your pages .php. Ideally you would want your site structure to not have to use file extensions if at all possible, so you may want to consider some mod_rewrite type stuff anyway and keep your site without file extensions altogether.

Posted: Thu Oct 18, 2007 9:19 pm
by swiftouch
I assume you need a .php extension on your page to run php code in the page. So if you want to use sessions and session variables, it would seem every page in your site would need to be .php (and not .html).
Put this in your .htaccess file and save it at the root of your site. That should make it so .html files will be read as php.

Code: Select all

AddType application/x-httpd-php .html
...I believe that is only for apache servers

The only problem I find in doing this, some IDE's parse and display code according to its extension.