How can we include a php file into a html file?
I know we can do this with:
<AddType application/x-httpd-php .html>
but I don't have access to the server. Any other solution which I can include in my website folder or file?
Why not just rename the file to end in PHP? That would be the simplest way to do this.
If you have access to your website folder, you could create a .htaccess file that includes:
<IfModule mod_mime.c>
AddType application/x-httpd-php .php
AddType application/x-httpd-php .html
</IfModule>
You could alternatively try:
<FilesMatch "\.(htm|html|php)$">
SetHandler application/x-httpd-php
</FilesMatch>
Depending on what the code looks like that you want to include, and if you really have no other choice like the ones presented by Druid, you can use one of the following less-than-perfect embedding solutions:
An IFRAME with frameborder="0" pointing to the PHP script (Downside: dynamic resizing according to output size is possible only with JavaScript
An AJAX Snippet that loads the PHP file onLoad and injects the output into a DIV of your choice
A tag pointing to the PHP file. The PHP file must then issue JavaScript commands like "document.write()".
The IFRAME is the most fail-safe in the event the client has no JavaScript. The AJAX Snippet is the most elegant solution.
The correct solution is not renaming it to .php.
It is renaming to .phtml ;)
.phtml is the official file type for crossover files containing PHP and HTML code.
Related
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.
I want to upload a PHP file to a server under a PNG extension, while still being able to view it in my browser as a PHP file. How can I do that? Thanks.
Use the following in your .htaccess file in the directories where your files are:
<FilesMatch "\.png$">
SetHandler application/x-httpd-php
</FilesMAtch>
http://httpd.apache.org/docs/2.2/mod/mod_mime.html
http://bytes.com/topic/php/answers/745293-force-apache-parse-html-files-php
Note that ALL files with .png will be handled by PHP with this.
You can add AddType application/x-httpd-php .png to an .htaccess file (assuming your host allows you to use .htaccess files), and Apache will send those file off to be parsed by PHP.
Why not use URL Rewriting for this?
You cannot. PHP is server-side code and is not rendered in the browser.
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.
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>
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