I am trying to print the output of php file in HTML using server side includes.
Here is the code of index.html file.
<!DOCTYPE html>
<html>
<body>
<!--#include file="include.php" -->
<h1>This is a Example Of Server Side Includes</h1>
</body>
</html>
Here is the code of my include.php file.
<?php
echo "Hello From PHP\n";
echo "Hello To HTML\n";
?>
Here is the content of my .htaccess file.
Options +Includes
AddType text/html .shtml
AddOutputFilter INCLUDES .shtml
All the files located in /var/www/html.
This is the screen shot of while executing index.html.
Does Server Side Includes also work in localhost? if yes How to configure?
And where to put .htaccess file in var/www/html.
If you are using Nginx, an example of how to use SSI (Server Side Includes) is below:
<!--# include virtual="/body.php?argument=value" -->
Nginx SSI Docs
If you're using PHP, you need to have a file with the PHP extension. You cannot use PHP in a file with an HTML extension. Change index.html to index.php
If localhost does support ssi, it wouldn't be supported in an html file. Try changing the name to index.shtml. Also, since your include path is relative, try <!--#include file='...'-->
Or, you could just try what the other answers say, and use php instead.
Related
Given the following index.html file:
<html>
<body>
<p>Welcome to <?= $_SERVER ['HTTP_HOST']; ?></p>
</body>
</html>
I expect to see "Welcome to EXAMPLE.COM", All I see is "Welcome to ".
What would cause this to happen if the code checks out?
Your file is named index.html. Unless you told your server that .html files should be treated as PHP scripts, that means the PHP code is NOT being executed - it's going out as literal text. And since PHP tags make it look like HTML, your browser is properly hiding that unknown/illegal tag.
Rename it to index.php.
It's not running because that part is not html (it's php) but you have saved it as an html file instead of as a php file.
rename it to index.php and try again?
Web servers are usually configured to run PHP only on files with the .php extension. Your index.html file will be passed as-is to the browser, which will probably ignore the unknown PHP tags. If you take a look at the source code of the web page, the tags will probably be there.
If you must have a .html extension, you can usually configure the web server to run PHP on .html files. For example, in Apache, you can use the AddType directive in an .htaccess file or in the server configuration (httpd.conf):
AddType application/x-httpd-php .html
However, this will run PHP on all .html files (in that directory), which may put an unnecessary load on the server.
A much better way is to use URLs without extensions. In Apache, you can use the DirectoryIndex directive to specify a list of index files that the web server will search for:
Options +Indexes
DirectoryIndex index.html index.php
When a browser requests a URL that ends with a slash, such as http://mydomain.example/foo/, the server will search for foo/index.html or foo/index.php in the DocumentRoot (or, failing both, generate a directory listing). You can now use whichever type of index file is appropriate for the moment, without ever having to change your URLs.
I'm familiar with HTML but only just starting to learn php. I can't seem to figure out why this code isn't working. I've previously had no problems with include()'s but that is when the php code is it's own distinct file. This time around I'm trying to create a dynamic web page with php scripts. I have apache running with xampp and I have the gf.css file and testing.php in the same folder as the main html file.
The problem I can't seem to figure out is that nothing is showing up in the text box when I try and run the HTML page. It seems as though nothing in the php tags is running. I put in echo statements to try and determine what the problem was but nothing showed up unless, however, I placed a h1 tag after the first " in my echo statement. But inn this case, no matter where I placed the closing h1 tag, the rest of the php script just printed. It was as if I couldn't close the tag.
That's as much info as I can give really. Any help and/or explanations as to what I'm doing wrong would be much appreciated! Thanks in advance.
Here's the HTML file
<html>
<head>
<link rel='stylesheet' type='text/css' href='gf.css'>
<body>
<h1>This is my web page</h1>
<div id='main_box'>
<?php
include ('testing.php');
?>
</div>
</body>
</head>
</html>
Here's the .css file
#main_box
{
height:500px;
width:400px;
color:white;
background-color:1e1e1e;
}
And here's the testing.php file if that matters
<?php
echo "test page";
?>
If you want to write php code then file extension must be ".php". So change your main HTML file extension to PHP.
First Solution:
If you want to write php code then file extension must be ".php". So change your main HTML file extension to PHP.
Second Solution:
You can tell Apache to treat .html files as PHP by adding a new FilesMatch directive:
1. Open C:/xampp/apache/conf/extra/httpd-xampp.conf
2. Search:
<FilesMatch "\.php$">
SetHandler application/x-httpd-php
</FilesMatch>
3. Add those three line:
<FilesMatch "\.html$">
SetHandler application/x-httpd-php
</FilesMatch>
Third Solution:
You can do it by adding an .htaccess file into your document root that contains:
AddHandler application/x-httpd-php .html
i have a website that's mostly written in html, but i have to load it into a server that require the first page to be a index.php. How can i do that?
I have my index.html. is there a way to create a index.php that just load the index.html?
i tried
<html>
<head></head>
<body>
<?php include 'index.html'; ?>
</body>
</html>
but won't work.
i heard of get_file or to import header, but i'm clueless, i never used php. how should i do?
You can just rename the file to index.php. It doesn't require that any actual PHP code be in the file, the server's PHP engine will still process the file (pretty quickly too, since there's no server-side code to interpret) and show the HTML.
PHP
<?php
header( 'Location: http://www.redirect-location.com' );
exit();
?>
HTML
<meta http-equiv="refresh" content="1;url=http://www.redirect-location.com">
.htaccess ReWrite
RewriteEngine on
RewriteRule index\.html index.php [NC,R]
or
DirectoryIndex index.php index.html
In order for this to work you need to allow overrides for indexes in the apache config
AllowOverride indexes
Some pages seemingly don't have a .php extension, but they have server-side stuff in them. Or am I completely wrong with that?
Basically, I'd love to have include statements for my header and footer, without having to copy and paste changes into every HTML document, but rather update the one. Can I do this without making the page .php?
What's the best way to include those?
It does not require a .php ending, just include it :)
Yes, make your webserver parse your HTML files as PHP scripts, then you'll be able to add <?php include 'whatever.php'; from your .html files.
Example for Apache:
AddType application/x-httpd-php .html
Why not use .php?
Just create each page like this:
<?php
include 'header.php';
// your code
include 'footer.php';
?>
And create a header.php for the top part of your layout and footer.php for the bottom part.
You could also look at a template engine like smarty.
You could map the file extension html to be executed as php in a .htaccess file in apache
AddType application/x-httpd-php .php .php4 .php3 .html .htm
Maybe unsupported or different (1 and 1 hosted) on your server.
http://httpd.apache.org/docs/2.2/mod/mod_mime.html#addtype
I'm new to PHP. I installed XAMPP and have Apache running. I created helloworld.php in XAMPP's htdocs and got PHP to display in my browser. My question is, why does my PHP script in my HTML file not display in my browser? Ive never installed PHP on its own. Should I also install it? Would it conflict with XAMPP. My code is below. Any assistance will be appreciated. Thanks in advance:
<html>
<body>
<?php
echo "Hello PHP World";
?>
</body>
</html>
I assume you are trying to use php inside .html file?
Try adding .htaccess file or changing apache config with the following line:
AddHandler application/x-httpd-php .html
XAMPP already includes PHP, but unless you end the script name with .php it is unlikely to be processed by the PHP engine.
Stop the apache service, then add one change in c:\xampp\apache\conf\httpd.conf in the section by adding...
AddType application/x-httpd-php .html .htm
Restart apache!
This looks like a big fat 'feature' in the current xampp distribution for win 32-bit.
You should add mime type at http conf
for instance in apache at httpd.conf
entry
<IfModule mime_module>
#
# TypesConfig points to the file containing the list of mappings from
# filename extension to MIME-type.
#
TypesConfig "conf/mime.types"
.......
AddType application/x-httpd-php .html .htm
AddType text/html .shtml
AddOutputFilter INCLUDES .shtml
</IfModule>
The php module for apache registers itself as handler for the mime type application/x-httpd-php. And the configuration file apache\conf\extra\httpd-xampp.conf contains the lines
<FilesMatch "\.php$">
SetHandler application/x-httpd-php
</FilesMatch>
which tells the apache that all files having .php as name extension are to be processes by the handler for application/x-httpd-php.
If you (really) want to have your .html files handled by the php module as well you have to add something similar for .html extensions. (there are other methods to tell the apache which extension maps to which mime type/handler. But FilesMatch/SetHandler is fine.)
If you want to enable this "feature" for only one directory you can use an .htaccess file to change the configuration for that directory (and its subdirectories).
Too much overkill. All these suggestions lead me down the wrong path for like 5 hours. JK, but I did read a lot of google search items all giving wrong answers and each suggestion was just adding more wrong answers.
The answer is in fact so simple you would want to bang your head: Simply change the file extension from ".html" to ".php"!!! Remember that you can build a webpage entirely out of PHP and all JavaScript and stuff built off JavaScript like, JQuery, bootstrap, etc will work.
Here is a simple example of proof:
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Blank Web Page</title>
<link rel="stylesheet" type="text/css" href="css/css.css">
</head>
<body>
<?php
$son = 5;
$nos =10;
echo $son + $nos;
?>
<h4>test to see if this html element can be output too!</h4>
<script type="text/javascript" src="js/js.js"></script>
</body>
Notice that I am using your standard html, even though it doesn't show my HTML tags(trust me it's there), web page stuff and have php code inserted inside. Of course the result is 15 and the html element h4 renders correctly too. Change the extension back to "html" and you will get only the h4 element and you will find that your php code has been commented out using multi-comment for html.
I forgot to add that this works for Xampp too.