I have an extraordinarily simple website:
index.html contains:
<html>
<body>
<h1>Welcome to my home page!</h1>
<p>Some text.</p>
<p>Some more text.</p>
<?php include 'footer.php';?>
</body>
</html>
It is in /usr/share/nginx/html
footer.php is in the same directory and is:
<?php
echo "<p>printed!!</p>";
?>
When I go to http://MY_IP_address/footer.php I get the proper output of just a blank page with "printed!!" at the top left.
When I go to http://MY_IP_address/ I get:
"
Welcome to my home page!
Some text.
Some more text.
"
But I do not get "printed!!" written anywhere.
I am assuming that somewhere I missed something in my config files, but I can't figure out what it is.
The file which contained include() is a file with .html extension, thus it's not interpreted by Apache. Thus, the code in index.html was not included. (Credits to #RiggsFolly for the improved explanation)
Unless your server is set to parse .html files as PHP, it will not be included.
Thus you'll need to rename it to index.php.
The include() function in PHP includes code from other PHP files and copies them into the file that uses the include statement.
Source:
https://stackoverflow.com/tags/php-include/info
http://php.net/manual/en/function.include.php
Php tags(<?php ?>) can only be parsed when file extension is php.
So,
Change file name from index.html to index.php
Related
Basically i have a php script that i use to log requests on my website.(ip/browser, etc) which is log.php, how can i include the log.php in my index.html so every time someone visits my website they also get "logged"? I have tried a couple of things and its way off than what i want to do.
Basically i want to include domain.com/log.php on my main page..
I tried so many things so far and nothing has worked, i think its really really simple but i can't make it work.
Any help is highly appreciated.
Sorry for my bad english, not my first language. I hope you can understand what i mean.
The include statement takes all the text/code/markup that exists in the specified file and copies it into the file that uses the include statement.
Here's an example:
<HTML>
<head></head>
<body>
<h1>Welcome to this page!</h1>
<p>Some text.</p>
<?php include ('footer.php'); ?>
</body>
</html>
You can really execute a php code in a html file, what you can do is create a php file called index.php from there create add your code 'logging' code and then render the content of your page.
example index.php
<?php
// your code for logging the request
echo <html>...</html>
?>
I see multiple answers about creating php files in order to reuse headers/footers, but nothing specific enough. I can't seem to get it to work.
What exactly would the php file look like and what exactly would my html file look like (given the code as it currently is, below)? do I have to convert all my html files to php files in order to use the php include line?
<div id="footer2-wrap">
<div class="container">
<table id="header">
<tr>
<td class="phone-number"><span class='wsite-text wsite-phone'>Copyright 2015 | xxx Corporation</span></td>
</tr>
</table>
</div>
Create a new file with your footer data. Let's give it the name: footer.php:
<div id="footer2-wrap">
<div class="container">copyright etc...</div>
<div>
Then inside your master template (or index.php file), include the footer file:
include_once('footer.php');
You should work with include(), require() or require_once functions in PHP to include your files, which depends on your situation.
For instance, let's assume you have a basic php file, called index.php and you want to add sidebar.php, footer.php, navigation.php.
index.php
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8">
<link rel="stylesheet" href="style.css"/>
</head>
<body>
<?php
include("sidebar.php");
include("navigation.php");
include("footer.php"); // we include footer.php here. you can use .html extension, too.
?>
</body>
</html>
footer.php (or html)
About us
Our work
Testimonials
What we do
Contact us
Yes, you must have a .php file to use PHP, your server must also have PHP installed (but most already do). PHP also adds to the loading time of a page, so take that into consideration when using it. To add the footer with PHP, you can use the PHP function include(), or, I am not sure if this is considered correct, with file-get-contents():
include():
<?php
include("footer.html");
?>
file-get-contents():
<?php
$footer = file_get_contents('footer.html');
echo $footer;
?>
You could also do the same thing with JavaScript:
var xmlhttp, text;
xmlhttp = new XMLHttpRequest();
xmlhttp.open('GET', 'http://www.example.com/file.txt', false);
xmlhttp.send();
text = xmlhttp.responseText;
document.getElementById("footer").innerHTML=text;
Code taken from here.
Note: The file must be on the same domain to use JS.
First create a file called menu.php(if you use .html it wont work)
On that php file write the html code for your menu
<div id="navbar">
more code
</div>
At your main html file write
<?php
include("menu.php");
?>
The code should be able to run. Make sure you are running the code via Apache. Download Xammp or wammp. Start Apache, copy paste your project folder to the xammp folder under httdocs. Then on your browser type in localhost/[your project name] and make sure the html files are changed to .php.
I have a problem with my index.php together with my "include files". My index.php file is located inside the main folder named mysite and the rest inside it are subfolders with the respective .php files. The sub-folder files are working perfectly, except the index.php. The include files are messed up every time I preview it in the browser.
The path for the index.php file goes like this:
mysite/index.php
The path for the include files goes like this:
mysite/pages/include/header.php
Here is the HTML file with the include files:
<html>
<head>
<?php
include ("pages/include/headertop.php");
include ("pages/include/header.php");
include ("pages/include/nav.php");
?>
</head>
</html>
Kindly correct me if I have missed something here.
By the way, I'm using XAMPP.
Thank you and More power!
edit your index.php:
include ("../pages/include/headertop.php");
include ("../pages/include/header.php");
include ("../pages/include/nav.php");
and try again "mysite/index.php"
Firstly make sure that you are calling these include commands in a .php file and not .html file.
Secondly make sure that your server is up and running well.
After this try this code:
<html>
<head>
<?php
include "pages/include/headertop.php";
include "pages/include/header.php";
include "pages/include/nav.php";
?>
</head>
</html>
P.S: just remove the brackets from include.
Please also make sure that the file names are correct and they follow the proper case.
Where is the right place to include a file when working with HTML and php?
Before the HTML code:
<?php include 'file.php' ?>
<html>
<head>
</head>
<body>
</body>
</html>
In the head tag:
<html>
<head>
<?php include 'file.php' ?>
</head>
<body>
</body>
</html>
In the body tag:
<html>
<head>
</head>
<body>
<?php include 'file.php' ?>
</body>
</html>
Include the file wherever it would otherwise be in the code...
Example:
- If the include is an html form, it would go in the body.
- If the include is a php script to process the form, it would probably go in the head.
If your imported file is just code with no characters outside the PHP blocks then it doesn't matter. I'd personally put it in the top of the file, so that I could use ini_set affecting the whole execution or send headers or cookies.
I you have content to be printed in the file's main code or outside PHP blocks you should put the file where you want the content.
Just noting, if you want keep the main HTML structure static in your main file and still want to print to both <body> and <head> I suggest you do both in functions, add the import to the file top and call the functions to print.
PHP doesn't care where you put it. For purposes of displaying your page, though, it depends on what is is the included file. For example, if file.php contains the body of your table, obviously it should go in the <body> tag.
It depends on your need.
If your file.php file has some global functions that you'd like to access throughout your code, then I would say include it at the top. Additionally, if you're doing anything with the headers in the included file, definitely include it at the top.
However, say your file.php contains a dynamic javascript code (in other words a script that is changed by php depending on the situation), then the header is probably the best location for it, since that is more or less the standard location to place javascript.
Finally, if your file.php is meant to bring in actual html or structure to the file, then definitely include it in the body.
I have a menu.php file I need included in each page on a site I am building. When I open the menu.php by itself it works fine. It's when I have it included in other files such as index.php then nothing loads.
Below is my index.php content. My menu.php is strictly html with a .css style sheet linked. I have searched and can find nothing to solve my problem. Any suggestions?
<html>
<head>
<!-- ... -->
</head>
<body>
<?php include 'menu.php'; ?>
</body>
</html>
EDIT: that is menu.php in the include, not header.php.
Let me try to explain how directories work:
ROOTFOLDER--
home.html
contact.html
PHPFOLDER---
header.php
CSS-----
stylesheet.css
if in home.html you have <?php include 'header.php';?> that wont work because the header is in the PHPFOLDER so you must <?php include 'folderphp/header.php';?>
Check your path, inclusion location etc.
For example, if a file located in a certain folder includes a file, that also includes another file.. the relative path is always based on the very original file opened.
What I do:
include $_SERVER['DOCUMENT_ROOT'].'/menu.php'; // if file is at /public_html/menu.php
or
include $_SERVER['DOCUMENT_ROOT'].'/includes/menu.php'; // if file is at /public_html/includes/menu.php
THat way no matter where you're opening from, you're calling an absolute path.