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.
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");
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
Related
Using Apache it's easy to enable PHP for all files of any extension:
AddHandler application/x-httpd-php .css .js .txt .xml
How do I enable PHP for only a specific file but not all other files with that same extension?
I have tried AddHandler application/x-httpd-php test.xml and it did not work and the search engines aren't coughing up anything remotely relevant.
AddHandler can be used inside a <Files> container.
AddHandler Directive
Description: Maps the filename extensions to the specified handler
Context: server config, virtual host, directory, .htaccess
... where directory stands for:
directory
A directive marked as being valid in this context may be used inside <Directory>, <Location>, <Files>, <If>,
and <Proxy> containers in the server configuration files,
subject to the restrictions outlined in Configuration Sections.
And <Files> can be used from .htaccess just fine:
<Files> Directive
Description: Contains directives that apply to matched filenames
Context: server config, virtual host, directory, .htaccess
Simply:
<Files hello.html>
AddType application/x-httpd-php .html
</Files>
The file tag selects only hello.html,the AddType then adds php support for all files it can "access". Due to the Files header, it can only "access" hello.html, and so only enable php for this one.
I have a quick question about changing the file types PHP parses. This website gave this line:
AddType application/x-httpd-php .php
But I'm not clear what file this goes into. Any help would be appreciated - thanks!
That line goes into a file called .htaccess, that changes the Apache server configuration for the folder it is on, and all its subfolders (Unless otherwise specified)
Your server should parse php files with the .php file extension by default though. You could use that to add custom file formats for example.
To parse .mp4 files, like you said in the comments, add to your .htaccess:
AddType application/x-httpd-php .mp4
You need to write that in your .htaccess file.
That makes Apache parse .php files through the PHP interpreter.
You can either add that to your .htaccess or httpd.conf file, depending on what you have access to.
Can somebody tell me how to treat .inc files as .php so I can include the .inc files into my php code. You can tell me change their extension to .php but it is not easy since there are hundred of pages which use the inc version.
Basically I need to declare types and handler in .htaccess.
I am running PHP over Apache.
Thanks.
add this to your httpd.conf
<FilesMatch "\.inc$">
SetHandler application/x-httpd-php
</FilesMatch>
Or in the .htaccess file
AddHandler application/x-httpd-php .inc
If you are including them via include() or require() into your PHP code, there's no need to change the extension. Anything included via the include() family is already treated as PHP.
Addendum:
Please also see #Darhazer's answer configuring Apache to serve .inc as PHP if you have a need to do so. (It's not totally clear from your question).
Do nothing.
PHP doesn't care what file extension included files has, it will still execute any code between <?php and ?> in them.
I currently have a Movable Type blog where the pages do not have any file extensions...so example.com/entries/this-is-my-entry
I added DefaultType text/html, so Apache knows if there isn't any extension on the end of the file to display as HTML. That works great.
Now the problem is I have PHP code in the HTML. The page won't render the PHP code if there is no extension. However, when I publish the entries with a .html extension....the PHP code works perfectly.
I have AddHandler php5-script .html to tell Apache to display PHP in the HTML files. I'm guessing, if there isn't any file extension, it doesn't know to render the PHP code, because it is expecting files with a .html extension...is there a way to say any file that doesn't have an extension can use PHP?
I'm doing the same implementation for my personal MT blog, with the entries having no file extension. (The theory being that I could switch to other techniques in the future without being hampered by extensions like .html, .php, etc.)
You can accomplish this by setting your default type to be PHP:
DefaultType application/x-httpd-php
All PHP files are interpreted at first as HTML, so this works even for files with no PHP in them.
Maybe you can try using <FileMatch> directive? Something like
<FilesMatch "^[^\.]$">
ForceType application/x-httpd-php
</FilesMatch>
There is nothing directly corresponding to DefaultType for AddHandler, but you can hack around it in a fairly ugly way:
<FilesMatch "^(?!.*\..+)">
SetHandler php5-script
</FilesMatch>
I'd like to accept other type of files that contains PHP code. For example, it would be nice to read an .aspx file by PHP as if it were .php.
Add this to your .htaccess file in Apache to make html parse as PHP:
AddType application/x-httpd-php .html
You can extrapolate what you need to do from there. :-)
Use this htaccess rule:
AddType application/x-httpd-php .php .aspx
Yes, this is done in your Apache configuration. You tell apache what kind of files you want it to send to the PHP engine. You can do this configuration the same way you do other apache configuration - in the main config file, per site, or in .htaccess files.