Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

does .php files need the .php extension?

Tags:

file

php

I am setting up a couple of PHP documents that include .cfg for configuration files, .tpl for template structure files, .dlf for document layout files, .dbh for database connections and so on.

Now they're called .tpl.php, .dlf.php etc. But do they need to have the .php extension as well?

If not are there any extensions I shouldn't be using? like .exe for executables.

like image 552
ThomasK Avatar asked Feb 26 '16 12:02

ThomasK


Video Answer


2 Answers

From Hiding PHP on PHP.net:

Another tactic is to configure web servers such as apache to parse different filetypes through PHP, either with an .htaccess directive, or in the apache configuration file itself. You can then use misleading file extensions:

# Make PHP code look like unknown types
AddType application/x-httpd-php .bop .foo .133t

So you can add a .htaccess rule that would mean that your server treats .tpl, .dlf etc as if they were PHP files, like so:

AddType application/x-httpd-php .tpl .dlf .dbh

However, if you are just using include or require, it doesn't matter what extension you use:

include "inc/template.tpl";
require "inc/database.dbh";
require_once("inc/config.ext.php.url.tpl.cfg");
like image 153
Ben Avatar answered Oct 07 '22 18:10

Ben


You can change server config to enterpret other file extensions as PHP

In Apache you can add to this:

<FilesMatch \.php$>
SetHandler application/x-httpd-php
</FilesMatch>

if you include the files, then any extension will do:

in script.php:

include 'includes/foo.inc';
include 'inlcudes/bar.whatever';

will all work

like image 33
B-and-P Avatar answered Oct 07 '22 17:10

B-and-P