php code in shtml - php

Okay, so I'm very new to website design, so please keep that in mind.
I've been working on a website (with file extension .shtml), and the time has come to add php code to it. It's only a little code, but it doesn't work on the .shtml website; the php code ends up in the source code as a comment. I was wondering if there was a way to make php code work on an shtml website.
Thanks.

Change the .shtml to .php update the include files you probably have and your on your way.
To keep your .shtml add this
AddType application/x-httpd-php .shtml
Keep in mind this will put additional load on your webserver as it will now scan all .shtml for php instead of just .php files
This can be added in the httpd.conf file

Write your PHP code as a separate .php file and then include it using SSI, as so:
<!--#include virtual="/path/to/yourfile.php" -->
This worked for me on a hosted website with no changes to .htaccess required. The PHP executes before being included in the .shtml page.

You'll need to setup a web server to get this working. Checkout Apache, or http://www.apachefriends.org/en/xampp.html for something very easy to setup. You can't just drop PHP into an html page and expect it to execute, it needs the interpreter.

Related

Login & User Account Functionality on Pure Html Based Website

I have a piece of e-commerce software that is purely Html based. I want to create the functionality of logging in and having an account.
I can't use PHP or mysql due to .html not being able to process it. I can't change them to .php as the software wouldn't function correctly.
Is there any other way around this? How is it possible to have this functionality and also is it even possible?
Is there a way to make php sit over the top of the html controlling the log in feature somehow?
My host has Php and mysql installed, but I simply cant use it with my webpages
You can do this by telling Apache to process files with a .htm or .html file extension as PHP files. This will allow you to add php to your .html files to build a login and user account system effectively over the top of your existing site.
To do this you need to create a .htaccess file in your root web directory and add this line to it:
AddType application/x-httpd-php .htm .html
Then simply add the php tags to you .html pages with the desired php code.
<?php
// your php code
?>

Why wont my browser recognise php scripts within a html file?

I am designing a website that requires me to show the outputs of a mysql database on a webpage written in html. However my browser doesn't seem to recognize the php scripts within the html file and returns blank values where their should be details. The scripts worked fine in the .php files but have no effect when included in the .html files even after creating a .htaccess file as instructed in previously asked questions. Is there anything else that can be done to solve this issue?
There are two methods (that I've encountered) of changing the environment on an Apache server via .htaccess to allow PHP scripts to be processed in .html files, depending on the configuation. Whichever of these you've attempted, try the other.
AddHandler application/x-httpd-php5 .php .html .htm
Or
AddHandler cgi-script .html
SetEnv PHP_EXTENSION .html

HTML: Display a string from a separate PHP file

I've searched all over the internet and can't seem to find a solution to my problem. I want to be able to "call" a php file from an html file and display the string returned:
html_ONLY_file.html
...
<h1>GetHeader.php?type=main</h1>
...
GetHeader.php
if($_GET['type'] == 'main')
print 'Some header to display'; //or echo 'Some...';
exit;
I've done this for images, img src="image.php?file=file.jpg", where image.php does a header(...) and readfile(...) return but I do not know how to do this for simple text. I'm not looking for DOM or anything too involved, if I have to I will. I want to know if there is a simple solution. It's generating the html side that I'm lost on.
In case you want to know, I am doing this because I once used a <#virtual include=...> to call for a php file from my .shtml file. Well, the hosting company decided to change mod_security and now I cannot include any php files (I've already tried everything). Including html files works fine and so I am changing this part of the website around so I don't have to rename files because the site is for a small business that is now ranked highly on Google for its geographical area. If I change the file names, shtml to php, then I believe the Google ranking drops (don't comment on this part unless you are damn sure you are 100% correct).
If you can edit the .htaccess file you can add a line in your .htaccess file that will mean HTML files will be parsed as if they were PHP.
If your server is running PHP as an apache module:
AddType application/x-httpd-php .html .htm
If your server is running PHP as CGI:
AddHandler application/x-httpd-php .html .htm
Source: http://www.besthostratings.com/articles/php-in-html-files.html
Once you've added that your html_ONLY_file.html could look like:
<h1><?php print "some header"; ?></h1>
And it would function just as if it were a .php file.
Alternatively, you could convert all your files to .php and add redirects into the .htaccess file like so:
rewriteRule ^somefile.shtml$ somefile.php [R=301,L]
rewriteRule ^another-file.shtml$ another-file.php [R=301,L]
This effectively says to your server "whenever a user requests somefile.shtml, act as if they requested somefile.php instead". The R=301 is the most important part with regards to Google rankings. This tells Google (and anyone who requests the .shtml file) that it's permanently moved to the new location somefile.php. This transfers all / almost all of the ranking power from the old location to the new location.
Source: http://moz.com/learn/seo/redirection

After setting .htm files to render using PHP I'm being asked to download the page

I'm working on an old site, and I want to add cookie functionality in order to allow people to use the mobile site, but also opt-into using the full site if they want. The site is currently all .htm files, and I can't with the budget go in and change every link to .php so I tried adding this to my .htaccess file
AddType application/x-httpd-php .htm
This is causing the browser I'm using (Firefox) to ask me to download the file. It says it's a application/x-httpd-php file, so I know the .htaccess file is working. When I was building my home web sever, and trying to run a ruby on rails site I ran into the same problem because i hadn't set up ruby correctly and it wasn't rendering the file. But I have never run into a site that doesn't have some sort of support for PHP. Could this be caused by another problem. Or does that .htaccess file change break rules made by some web hosts?
Any support would be amazing! Thank you so much everyone :)
You need to add a handler for that type, otherwise the webserver isn't going to do anything with it:
AddHandler application/x-httpd-php .htm
That should be enough if you've already got phpv4 executing on your server. But you can also create a custom action explicitly:
AddHandler application/php-cgi .htm
Action application/php-cgi /path/to/php-handler

PHP HTML Embed PHP Tags within a .HTML Document Is This Possible?

Embedding HTML within .PHP files is one of the primary functionalities of PHP, but is it possible to do it the other way round?
I mean, embedding PHP tags, , within a .HTML document??
Is this sort of coding possible?
Any suggestions welcome.
Add this to your .htaccess file and HTML files will be handled like PHP files allowing you put PHP in HTML files.
AddType application/x-httpd-php .html
Some more information on it here
If you configure your server properly you can put php code in almost any kind of file.
If you are using apache, just add this line in the httpd.conf file
"AddType application/x-httpd-php .html"
Now you can embed php tags in .html files and they will be parsed correctly

Categories