HTML through PHP parser - php

I'm trying to hide the fact I'm using PHP on one of my site pages and I'm wanting to run it through the PHP parser (just that page not them all) so I can call it filename.html as usual. I have tried a few Apache directives I found online and have a few in my .htaccess file (hotlinks and for a 404 page).
When i use one of the scripts (in my .htacess) for the PHP purpose, the page wants to be saved/downloaded (like a vcard does) and a box shows - with no page to view. Can anyone please help. I'm new to PHP but believe a module might be needed or that it could be to do with the config of my server.

You should leave the page with a .php extension and have Apache handle the file as a normal PHP file. Then use a RewriteRule in your htaccess settings to hide the php file as follows:
RewriteEngine on
RewriteRule ^yourfile\.html$ yourfile.php

There is no real need to hide the fact that you are using PHP, but if you really want to parse PHP in html files you need to edit your Apache httpd.conf file. Open it up in a text editor and find a group of lines that looks like this:-
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz
AddType application/x-httpd-php .php
(Yours may be slightly different)
Then add
AddType text/html .html
restart Apache and php in html files will be parsed.

Related

How to setup RSS feed without access to httpd-conf

I was following a video tutorial on Youtube from phpacademy, https://www.youtube.com/watch?v=hxWYeCGa-PA&index=6&list=WL to make a dynamic RSS feed. In order to do that I need to be able to use php inside my rss file.
In this video the guy mentions adding the following line to the HTTPD-Conf file in order to allow this.
AddType application/x-httpd-php .rss
I contacted my web host 123-reg and asked them
Do I have access to the HTTPD.Conf file, or is it already configured to allow this.
I am looking to add the following line of code to the file if it isn't already in there.
AddType application/x-httpd-php .rss
What options do I have?
They responded with the following
The HTTPD.Conf file cannot be accessed with a shared hosting package
as the one used to host the website for the domain vwrx-project.co.uk.
This file is already configured to allow RSS, however you will need to
enable it. You can try adding the handler into the .htaccess file.
Does this make any sense to someone, I don't really know what code I should be looking to add into my .htaccess file
You can use url rewriting (if your hosting supports this) to direct non-existing files and directories, with .rss extension to .php:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.rss$ /$1.php [L,QSA]
Put all these lines into your .htaccess file and youtubefeed.rss will open youtubefeed.php, which you can program yourself.
After some more hunting around, and having looked up DaveG's code I wasn't sure it would do as I would have liked.
I have a file with a .rss extension for my rss feed, so from my understanding of the above code, it would redirect you from filnename.rss to filename.php where as what I was looking to do was retain the .rss extension but allow that file to read and interpret the php within it.
After some more searching I found that I could have used the original code from the video, and place it inside my .htaccess file instead on my httpd.conf file, I didn't realize that the code was interchangeable.
AddType application/x-httpd-php .rss // For older versions of php
Anyway whilst looking for this solution I saw some using the line of code below to achieve the same result, the only real difference being it is for PHP5 and later which is whats running on my server so AddType is now AddHandler and we add a 5 to the end of php
AddHandler application/x-httpd-php5 .rss // For use with PHP5+

php will not run in my .htm page?

index.htm I have 6 lines of PHP code that wont run on my page I don't get an error of any kind it just wont run if you look in source it shows my raw PHP. I have a file named test.php that works fine. in test.php I only have those 6 lines of code that you can see in my index.htm
Server Name defiro
cPanel Version 11.36.2 (build 9)
Theme x3
Apache version 2.2.24
PHP version 5.2.17
MySQL version 5.5.30-30.2
Architecture x86_64
Operating system linux
i have tryed to fix the problem by putting AddType application/x-httpd-php .html in my websites .htaccess file as listed in PHP not working on HTML file i have tryed a few other things by trying to get an error out of the php with no luck
Edit__
it wont let me use php or html in a file together no matter if it is .html or .php it wont work i don't know how to fix it?
Let me see
index.htm I have 6 lines of PHP code that wont run on my page I don't get an error of any kind it just wont run if you look in source it shows my raw PHP. I have a file named test.php that works fine. in test.php I only have those 6 lines of code that you can see in my index.htm
Let me make some assumptions
index.htm has php tags inside (<?php echo 'if this works'; ?>) that do not parse when you make your request. The tags don't even show up in your browser.
test.php is a file that starts and ends with php tags (<?php /*php content*/ ?>). It runs as expected. (mind you, you can add content before or after php tags and it will be displayed raw).
If these assumptions are not correct please say so in a comment
Some other assumptions
you set up your own server or you haven't fiddled with the configuration yet
my suggestion
Go to your httpd.conf apache configuration file
Look for something that resembles the following
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz
AddType application/x-httpd-php .php
The one that's talking about php is the line you're interested in, it determines which file extensions will be parsed by the php interpreter before being sent to the client. OK, change it to look like this
AddType application/x-httpd-php .php .html .htm
You can add as many space-separated extensions you want, but be warned, sometimes you want a raw file, so be carefull
SAVE and RESTART the server. Important, everytime you change the conf
Please give feedback if this does not help you
you have put the following in
AddType application/x-httpd-php .html
That targets .html files, You need to target .htm files so changing to
AddType application/x-httpd-php .htm
should fix it

How Do I enable PHP to be shown on HTML webpage?

I have been searching on forums and google for hours with no definitive answer other than "Your host may not support PHP". I have dumbed down my PHP page to simply display some text for testing purposes but I am still only getting a blank webpage. I have tried opening it locally into a browser and also through the web server, same result for both. Anyone have an idea as to why this simple PHP wont even show up on the webpage?
<?php
echo "Show some text";
?>
also tried:
<html>
<?php
echo "Show some text";
?>
</html>
What is the filename of your file? Is the file extension a .html file, or a .php file? If it's .html, rename it to .php and test with your first test again.;
To check and see if php is supported on your server, create another file titled: phpinfo.php and insert the following code in it:
<?php
phpinfo();
?>
and save it, and then go to it on your server. You should see the PHP configuration output on that page. If it doesn't work, then you do not have PHP correctly installed on your server.
Your file must be ending in .phtml or .php to make PHP work on a HTML page.
index.php
<?php
echo "Hello World!";
?>
Or you can trick your server, Apache and Nginx support this as does IIS. Basically you can have "index.x" or "index.px" - your own custom extension, it is not advisable for obvious reasons. In Apache (your public_html folder) enable this in your .htaccess file:
AddType application/x-httpd-php .php
AddType application/x-httpd-php .ac1d
AddType application/x-httpd-php .php4
AddType application/x-httpd-php .phtml
AddType application/x-httpd-php .x
# your custom extension above.
To view the source of everything (no processing):
Most server are setup with a source-viewing format already by changing the file name to use the .phps extension. If not, you'd have to create a .htaccess file and temporarily add this line to it:
AddType text/plain .php
AddType text/plain .html
That should force the files to be displayed as plain text whenever they're accessed. Just remember to remove it later.

htaccess - parsing php into html

I recently played with a .htaccess file to make one server to parse PHP files. Yesterday I uploaded the same .htaccess file and tried to test a PHP file. But something went wrong: visiting my page the browser offers to download the the html page rather then viewing the page!
On the server the filenames end in .html.
I added the following to my .htaccess file:
AddType application/x-httpd-php .html
I tried to find the htaccess file, but once uploaded it just disappears from the root dir.
I tried to upload other scripts I've found browsing. I even tried to search for some problem on a hosting forum. Nothing helped.
Please help!
Try this
AddType application/x-httpd-php .php .htm .html
OR
AddHandler application/x-httpd-php .php .htm .html
And remove other overriding handlers for application/x-httpd-php after above code.
Right, firstly things first, what host do you use?
Also what ftp client you are using? Some by default won't display files starting with . such as .htaccess and .htpasswd, that's why it may appear that you didn't upload it. Also it might be that you don't have the rights to upload in the very root directory, try to go one directory up.
Also from my experience, hosts won't allow you to modify headers via .htaccess this way, because the allowoverride directive is off; instead have a look at url rewrites (via mod_rewrite), which allow you to do the same thing without modifying headers.
Your rewrite .htaccess file might look something like:
RewriteEngine On
RewriteBase /
RewriteRule ^(\w+)\.html$ $1.php [NC]
(Not tested though)
Using a rewrite will also mean that your files will in fact maintain the php extension, however they will be access via urls that include .html extension.

Can I change the accepted extensions by PHP?

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.

Categories