Question about file extensions

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

Post Reply
RyanP
Forum Newbie
Posts: 5
Joined: Tue Oct 09, 2007 12:26 pm

Question about file extensions

Post 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
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Re: Question about file extensions

Post 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.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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.
mrkite
Forum Contributor
Posts: 104
Joined: Tue Sep 11, 2007 4:19 am

Post 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.
RyanP
Forum Newbie
Posts: 5
Joined: Tue Oct 09, 2007 12:26 pm

Post 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!
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
RyanP
Forum Newbie
Posts: 5
Joined: Tue Oct 09, 2007 12:26 pm

Post 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.
RyanP
Forum Newbie
Posts: 5
Joined: Tue Oct 09, 2007 12:26 pm

Post 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)?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
User avatar
swiftouch
Forum Commoner
Posts: 80
Joined: Sun Dec 10, 2006 7:35 am
Location: Salt Lake City, Utah

Post 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.
Post Reply