Using PHP in a .html document - php

I'm working on a job at work where I need to create a web forum. In my stupidity I decided to use javascript, not knowing beforehand that javascript is a client-side language. I need a way to save the data from the javascript onto the server and then be able to read the data. I tried looking at things like node.js, however I would have reconfigure the entire web server (which isn't mine) in order to do this. The other solution is to use php. Here's the problem: There's a bunch of includes used in the html file that set up the layout of the webpage (i.e. css files, html files, and even a php include). I can't change the name of the index file to index.php because it breaks all of the includes inside of the file. So I need a way to save, say a text file, using javascript and html. If there's a way to, I would like to do something very simple, like include a php file in the html and then call a php function in my javascript code to get the contents of the file into my index.html page. I thought there was a way to call a simple command like this:
<script>
var thedata = <?php getData() ?>
</script>
where the php function getData() would return a json encoded string with all of the data in it (handled from a separate php file). Is there any way to do this? Any other suggestions for how to handle data storage on a server without changing my index.html file to index.php?
Note: I tried accessing the apache httpd.conf file and adding a handler to pre-process .html files as php files, but that doesn't seem to work (nothing as simple as echo 'test' works on the html file).

Add this to your .htaccess:
AddHandler application/x-httpd-php5 .html .htm
source
It causes Apache to treat HTML files as files that contain PHP. Be careful not to somehow accidentally use PHP syntax in a regular HTML file, though.
Remember that you'll still need to use PHP tags to enter PHP mode. This works as expected:
<p>html content ... <?php echo 'hello, world'; ?></p>
But this will output the the echo command:
<p>echo 'hello, world'</p>

If can make your webserver process your pages thru the mod_php.
if you are using apache just add this to your .htaccess
AddHandler x-httpd-php .html .htm
AddHandler php-script .php .html .htm
AddHandler php5-script .php .html .htm
AddType application/x-httpd-php .htm
AddType application/x-httpd-php .html
I hope you understand the repercussion of doing this. all your pages will be processed like that and the memory/cpu use of your pages will be way greater.
if this is to happen inside one single file, make sure to add it inside a statement.
and within your example you should add:
<script>
var thedata = <?php echo getData(); ?>
</script>

The file extension must be .php, so that the server knows to parse the file.

Related

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

htaccess modifications working on some pages but not on others?

I have been trying to execute php code within a document with an .htm or .html extension. I finally got it working using:
AddType application/x-httpd-php .php .html .htm
Now, being able to execute php within .htm documents, only works if I go directly to the .htm page such as: http://www.foobar.com/layout.htm
However, it does not work if I go to the index.php page which uses that layout.htm page...
This is an example of what the index page url looks like: http://www.foobar.com/index.php
What am I doing wrong?
Assuming that you are using something like include in your php file to include the html file, your .htaccess rules will not have any effect on the included file.
The .htaccess rules only get executed on requests that are made to the apache web-server and when you include a local file in php, you are simply requesting a file on the local file system; you are not requesting it through apache.
Edit: Based on the comment below your question, it is also possible that you are using something like readfile to get the contents of the htm file. If that is the case, you need to change that to include so that the php gets executed.
Your .htaccess rule does not apply for included files in php.
Did you check this page: http://php.net/manual/en/function.include.php
When a file is included, parsing drops out of PHP mode and into HTML mode at the beginning of the target file, and resumes again at the end. For this reason, any code inside the target file which should be executed as PHP code must be enclosed within valid PHP start and end tags.

can't seem to get php working in MAMP

I'm trying to learn php and step one is getting php working in some capacity. I'm attempting to use MAMP but I'm having some trouble.
Specifically: if I create a file with the below code and save it as index.html in MAMP's "Document Root" directory, I get a blank page when pointing my browser at http://localhost:8888/index.html.
Code:
<html>
<body>
<?php
echo "Hello World!";
?>
</body>
</head>
Alternatively, if I put a bit of php into its own file (say test.php) and then point my browser at this file, it just displays the full text of the file in the browser.
Any ideas what I might be doing wrong?
I had the similar issue.
Make a new file in TextWrangler or Komodo, or whatever, and add the folllowing code:
AddType application/x-httpd-php .html .htm
AddHandler application/x-httpd-php .html .htm
You're going to save the file as .htaccess (with the dot in the front; this is the file name).
Save it in /Applications/MAMP/htdocs. This is the same place you'll save your php and html files. This .htaccess will be an invisible file; you will not see it in Finder, tho you can if you cd to it in Terminal, or searching w/in Finder and choosing the File Visibility type under Kind.
Now try going to localhost:8888/ and you should see all of the available files there. And with this newly created .htaccess file, you can now embed php inside an html file too.
In MAMP, edit the file:
/Applications/MAMP/conf/apache/httpd.conf
and then search for '#AddHandler type-map' (exclude quotes). Below that, add,
AddHandler application/x-httpd-php .php .html
Save the file and stop and re-start MAMP. Php parsing will occur in files ending with the extensions: .php and .html.
You must save a file with PHP inside it with a .php extension. So you would need to name it index.php instead of index.html. Simple fix.
So, this just worked for me:
instead of having:
MAMP/htdocs/folder-that-contains-all-files/
put all your files directly in the htdocs folder!
so:
MAMP/htdocs/all your files including index.php etc.
Hope that helps!
Modifying /Applications/MAMP/conf/apache/httpd.conf
searching for #AddHandler type-map
and inserting AddHandler application/x-httpd-php .php .html
worked for me.

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