PHP > PHP Include
How to run PHP in any web Pages
How to use PHP include function
How to use PHP with HTML/HTM pages
How to use Include PHP code
To easily mantain your website ,specially if it has many pages ,PHP include
function is probably one of the best solution.Say,for instance, you have to update
or change only the header or
navigation or any section that is the same in all the pages, you must edit
them separately one by one,and it will take a lot of time and the risk of
making mistakes is high.
But if you create few include files ,everything will be easier and you'll be
able to change or update all your site from one page only.
Also you can include content to your pages from other websites.
How PHP Include function works
In this example you'll see how to make the top and bottom of your pages
dynamic,but you can use this principle to any section.So you will be able to
change the top of your 1000 pages web site by editing only 1 single page.
Obviously the first requisite is to have PHP running on the Web Server that
hosts your website.
This is a normal web page using HTML and CSS (testInclude.css) to style
the page.
-
First step open a text editor like notepad and type the code or copy and
paste the code below ,for
a normal web page using HTML and CSS (testInclude.css) to style the page.
Save it as TestInclude1.php .
<html>
<head>
<title> Test Include Page</title>
<link rel="stylesheet" type="text/css" href="../testInclude.css">
</head>
<body>
<!-HEADER->
<div class="header">
This is the Header
</div>
<!-HEADER->
<div class="container">
All your content Here
</div>
<!-FOOTER->
<div class="bottom">This is the Bottom
</div>
<!-FOOTER->
</body>
</html>
-
Then open a new document and copy and
paste the HTML code between
<!-HEADER-> and
<!-HEADER-> including the two comments tags.Save the document as
header.php in a folder named PHPinclude.Do the same for
the FOOTER part and save the file as footer.php in the
PHPinclude folder'
-
Open TestInclude1.php and replace the
text between
<!-HEADER-> and
<!-HEADER-> with
<?php include 'PHPinclude/header.php'?>
Do the same for the FOOTER and save the new file as
TestInclude2.php.
Copy the code below for the CSS and save the
file as testInclude.css
.header {
width: 100%;
background-color: #33CC33;
text-align: center;
font-weight: bold;
font-size: large;
}
.container {
width: 100%;
background-color: #DEDDDA;
font-size: medium;
color: #000080;
height: 300px;
position: relative;
}
.bottom {
width: 100%;
border: thin solid #0000FF;
background-color: #FFCC00;
font-size: medium;
font-weight: bold;
text-align: center;
margin-right: auto;
margin-left: auto;
}
-
Now upload the 2 files and the folder PHPinclude and the CSS
file to your host. Open the 2 pages in your web browser.They
should appear the same. Even if you go on View/Source you should
see the same code because when you use the PHP include function
,is like if you copied the entire page in that point ,so basically even to the search engines crawlers will
appear normal and nothing hidden.
How to use PHP with HTML/HTM pages