I've been setting up an environment for html/php using Apache Lounge. Although my html worked fine, I started noticing issues after I tried to embed php. When I do only php, it just prints everything how it is, including the tags. When I try to embed it in html, it simply skips over all php parts, but sometimes prints the tags randomly. Yes, my file is .php. When looking at other people's similar questions, I found many saying to AddHandler application/x-httpd-php7 .html or AddType application/x-httpd-php .html. However, I have no such file anywhere, and both people who suggested that were using XAMPP. Any ideas? Thanks!
Related
There are a lot of solutions for this but they never describe how to do it.
Like how to do this:
AddType application/x-httpd-php5 .php
I don't know what people mean by this and if it works or not.
Is this a valid solution or does anyone have other solutions?
I have an issue parsing PHP in HTML Files.
I am using an install of Vesta and the domain is running fine. The site in question has
AddType application/x-httpd-php4 .htm .html
# and
AddType application/x-httpd-php5 .htm .html
in the .htaccess, which before I moved server it was allowing php to run in html. I have also tried every single variant of this which I have found on stack overflow and none of them are working.
I can't figure out for the life of me why its not now working?
Has anybody got any ideas?
Thank you
Dan Williams
Since your server will not allow you to use PHP in HTML, just rewrite all .html requests to php in .htaccess:
RewriteEngine On
RewriteRule ^(.*)\.html$ $1.php [L]
Should solve the problem. (https://stackoverflow.com/a/5990276/2119863)
Why won't it parse PHP in HTML?
The more types of files the server needs to push through the php interpreter, the more memory, processor and electricity it will consume. It's like with cars and trucks. Cars do not haul big trailors for a reason - trucks have much bigger engines and take the load but leave a bigger carbon footprint.
The second reason is the separation of functionalities. Seeing a html file, you should be 100% confident - across all and any servers - that this file will not print_r($_SERVER);. And when seeing a php file, you should be confident it performs some dynamic actions. And just like you shouldn't expect a nurse to build houses, neither should you expect HTML to parse PHP. :)
Since I want to have PHP code run properly on my website, should I add
AddType application/x-httpd-php .html
to my htaccess file, or just change all of my *.html files into *.php files?
I've heard that changing the file extension to *.php causes the website to load slower, but I'm wondering if changing the htaccess file does the same.
Either way, the files will be passed through the PHP interpreter, making them ever-so-slightly slower than if they were plain HTML files directly served down. It's the same process however you set it up. The difference in speed from plain HTML is going to be quite small unless you have a lot of dynamic PHP in there. Given that you are considering renaming existing files from .html to .php, I suspect you don't have much PHP code in there already (or any).
So it doesn't really matter which way you handle it.
However...
Leaving them as .html has the possible disadvantage that if you ever forget to setup this configuration, you could wind up serving raw PHP code to the browser, which might include your database connection details or other secrets.
it does exactly the same. .php is not slower than html, html is not slower than php, just a different setting in your webserver config.
AddType application/x-httpd-php .html would be a fraction slower, as apache load this line dynamically. If you set it in httpd.conf it would be exactly the same.
Agree with Michael that you need to be careful with renaming them HTML and having the chance that it not be setup, or your host provider do something screwy with your account.
If you do this, make sure any database/password files remain as PHP that you simply include in your HTML file.
As discussed a bit in this question, I am using Apache mod_include with conditional flow control statements to alter the behavior of included shtml files depending on the URL of the parent page. The problem I'm having is that some of the pages on the site are PHP pages, which seems to mean that the mod_include directives are ignored (and instead treated as standard html comments).
Is there any way to have PHP pages correctly process these mod_include directives?
Specifically, here is what I am trying to have processed:
<!--#if expr='"$DOCUMENT_NAME" = /(podcasts\.php)|(series\.php)/' -->
<li id="features" class="current">
<!--#else -->
<li id="features">
<!--#endif -->
Similar lines blocks work in the .shtml files on the site, but for the php pages, all of the above ends up output to the client.
Edit: The closest thing to a solution I have come up with is to mimic the functionality of the included shtml file in a php file. I don't like this solution because it means that adding links in the future will require adding them to multiple places.
Assuming you're running PHP via mod_php (may not even matter) just adding:
AddOutputFilter INCLUDES .shtml .php
and it works fine for both .shtml and .php with both being properly parsed.
I have just started to read about SSI but found this quote at
http://httpd.apache.org/docs/2.2/howto/ssi.html#configuring
A brief comment about what not to do. You'll occasionally see people
recommending that you just tell Apache to parse all .html files for
SSI, so that you don't have to mess with .shtml file names. These
folks have perhaps not heard about XBitHack. The thing to keep in mind
is that, by doing this, you're requiring that Apache read through
every single file that it sends out to clients, even if they don't
contain any SSI directives. This can slow things down quite a bit, and
is not a good idea.
So if I understand it right, you should not include .php in AddOutputFilter since if forces Apache to search all .php pages for SSI directives since it will slow down the server.
Maybe there is another solution to your problem?
http://httpd.apache.org/docs/2.2/mod/mod_include.html#xbithack
/Philip
Is there any problem with running HTML as PHP via .htaccess? such as security or best practices etc. was doing this to make URLs cleaner.
## run the following file types as php
Addhandler application/x-httpd-php .html .htm .rss .xml
Well ideally id like to have my URLs like
localhost/blog/posts/view.php?id=64
to be
localhost/projects/bittyPHP/bittyphp/posts/view/id-64
But having trouble accomplishing that without routing everything to one file and having PHP run determine the paths. I guess this is my real question
I would use mod rewrite.
Probably you do not need to run all html files as PHP, and if you have short_tags enabled "<?" in XML will give you trouble.
Keep in mind that you will run each and every of those files through the PHP handler then. If there is no PHP inside the files, the parser will still inspect them to see if there is any PHP in it. This adds some overhead, but it is likely neglectable in most setups.
Main issue I would say is performance. If you have a significant number of plain HTML files then you're creating unnecessary overhead by always running them through the PHP interpretter.
Best practice is not to do this, but use "friendly" URLS like mysite.com/item/123 and use mod_rewrite to convert them to mysite.com/displayitem.php?id=123 internally
Like many people have already stated, mod_rewrite is the best solution for accomplishing friendly URLs.
Sitepoint has a decent guide to getting started with mod_rewrite.