I have header.php and footer.php files which I include in all of my pages. A sample page looks like this -
<?php include($_SERVER['DOCUMENT_ROOT'].'/header.php');?>
<div id="content">
...
</div> <!-- content -->
<?php include($_SERVER['DOCUMENT_ROOT'].'/footer.php') ?>
Although this works well on the server, but when I test pages locally [ xampp on Windows 7 ], I get the following error message instead of the header, similarly for the footer -
Warning: include(C:/xampp/htdocs/header.php) [function.include]: failed to open stream: No such file or directory in C:\xampp\htdocs\f\index.php on line 1
Warning: include() [function.include]: Failed opening 'C:/xampp/htdocs/header.php' for inclusion (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\f\index.php on line 1
This makes testing very tedious as I have to upload to the server for every minor change.
Also, I dug around the WP code, and it uses a get-header() to display the header.php file. I could not completely understand the function. My site does not use WP.
What is the correct way for including header and footer files?
the correct way to include any file is the include() or require() function. Wordpress uses a get_header() function because the header is more then just 1 file, so they created a function for outputting it.
The problem you have seems to be a problem with the $_SERVER variable. It has been quite a long time since I've worked with PHP, but what I would advise you to do is just use relative paths. For example, if the header.php and footer.php files are in the same directory as your index.php, you can just do:
<?php include("header.php');?>
<div id="content">
...
</div> <!-- content -->
<?php include('footer.php') ?>
Simple and useful way (I use this in my all projects):
// find Base path
define( 'BASE_PATH', dirname(__FILE__) );
// include files
include BASE_PATH . '/header.php';
include BASE_PATH . '/footer.php';
It seems that $_SERVER['DOCUMENT_ROOT'] is pointing to C:\xampp\htdocs, while your scripts are at C:\xampp\htdocs\f\, check the value of $_SERVER['DOCUMENT_ROOT'] on your local environment.
edit:
<?php
$rootDir = "";
if(strpos($_SERVER['HTTP_HOST'],'localhost')===FALSE)
{
//On Production
$rootDir = $_SERVER['DOCUMENT_ROOT'];
}
else
{
//On Dev server
$rootDir = $_SERVER['DOCUMENT_ROOT'].'/f';
}
<?php include($rootDir.'/header.php');?>
<div id="content">
...
</div> <!-- content -->
<?php include($rootDir.'/footer.php') ?>
?>
Related
Let says below are my web directories:
From web searching i get to use include or require_once and something similar.But I'm confuse in different directory causes much of directory error.
And my question is, how to include Navbar or Footer or Specific Pages in different directory.
Condition: Same directory as above;
For header,navBar and footer you should used simple
include(page/header.php);
About require and include function Reference
The require() function is identical to include(), except that it
handles errors differently. If an error occurs, the include() function
generates a warning, but the script will continue execution. The
require() generates a fatal error, and the script will stop.
But for image,css files and other js files you should follow these steps
Create config.php file
add this in config.php
define('BASE_URL', 'http://your_website_url');//OR define('BASE_URL', 'localhost')
you can use this path like
<?php
include('config.php');//add this code into top of the page
?>
and finally you can used where you want, like
For Style sheet
<link rel="stylesheet" href="<?php echo BASE_URL; ?>/css/styles.css" />
Create a config.php file in your home directory.
<?php
/* Saves the directory path to base directory as `BASE_DIR`
Example: /var/user1/home/public_html/
'Base directory' is where your homepage/index.php is located */
define('BASE_DIR', dirname(__FILE__) . '/');
?>
now just require this config.php file
<?php
require_once('config.php'); //add this line at top of your pages
// No need to include this inside 'header.php' and 'footer.php'
?>
Now u can include files easily, use it in index.php as follows:
<?php
require_once('config.php');
require(BASE_DIR . 'query/conn.php');
// becomes '/var/user1/home/public_html/query/conn.php'
include(BASE_DIR . 'pages/header.php');
include(BASE_DIR . 'pages/navbar.php');
include(BASE_DIR . 'pages/footer.php');
?>
Note: Similar approach is used by WordPress (learn more)
Currently I am building a website in which I am trying to have one location (e.g. header.html or header.txt) to edit content on multiple pages (e.g. each page has the header). I assumed I could easily do this using PHP, importing the html code into the pages using
<?php
echo file_get_contents("header.txt");
// or echo file_get_html("header.html");
?>
Does not seem to be working. Any suggestions on how to do this? I want to be able to edit the header across all pages from one location.
Take care!
EDIT: Okay so I think I am making a simple mistake that prevents the php to run. Just to lay out what I have and what I want to do:
1: I have a piece of code that represents the header that I want to include on multiple pages. This is currently saved in a header.html file.
2: I have a webpage saved as trial.html where I am trying to place that piece of code using php.
Am I forgetting something?
You are making it too complicated:
<?php
require_once('header.php');
It does not matter if header.php contains just html, just php, or some mix of both.
Use PHP files, no plain html or txt because PHP can be protected to prevent directly access, include more parameters and your project can be more modular.
When you include(or require) php files, those are included in the main file before page loads (server side), so you can have more variables in it.
header.php
<?php
echo '<title>Welcome</title>';
body.php
<?php
echo '<h1>Hellow world!</h1>';
$custom_page = 'page 01';
footer.php
<?php
echo '<p>My site footer for ' . $custom_page . '</p>';
index.php
<?php
// debug errors
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
// Note: __DIR__ is absolute path for current file (index.php)
// if you have to go up one dir:
// include_once __DIR__ . '/../your_file.php';
// if you have to go down one dir:
// include_once __DIR__ . '/subdir/your_file.php';
/* more code */
?>
<html>
<header>
<?php include_once __DIR__ . '/header.php'; ?>
</header>
<body>
<?php include_once __DIR__ . '/body.php'; ?>
<?php include_once __DIR__ . '/footer.php'; ?>
</body>
</html>
Rendered HTML for index.php
<html>
<header>
<title>Welcome</title>
</header>
<body>
<h1>Hellow world!</h1>
<p>My site footer for page 01</p>
</body>
</html>
Recommended reading first page on google result:
PHP Getting Started
How To Start Programming
Getting Started With PHP: Basic Scripts
Problem ended up being in the path towards the file I wanted to include, as well as a multitude of other issues:
header.html -> header.php
index.html (page) -> index.php
get_contents -> include
include('/path/file') -> include 'path/file'
add set_include_path($_SERVER['DOCUMENT_ROOT']);
Thanks everybody for you input!
I am refurbishing a website for a friend, e.g. making it more easy to program/maintain.
The server is running PHP 5.6 and in order to make life easier for me I wanted to uses php's include function to easily include stuff like the head or menu in every web page.
The file structure I use is index.php in the / directory and e.g. history.php in /pages. The files I am including e.g head.php lie in /php.
My problem is that in index.php
<?php include ('php/head.php'); ?>
perfectly executes and includes the designated file but in all sub directories such as /pages the same php code in history.php just doesn't execute at all leaving me with a blank line in the source code. I figured that this has to do with my PHP config or that said might be wrong, but I couldn't find the issue. I also tried calling the tech support of my web hosting provider but although they told me that everything should be working now I still get a beautiful blank line.
I've been searching for a solution to my problem for quite some days now, but I sadly haven't had any luck so far.
Hope the community can help
Thanks in advance
If you set the include_path to the full path then no matter in which sub-directory you have scripts they will always use the full path to the required file.
<?php
/* If you set this on every page the path will always be correct */
set_include_path( $_SERVER['DOCUMENT_ROOT'] . '/php/' );
include( 'functions.php' );
?>
<!doctype html>
<html>
<head>
<title>Include PHP files</title>
<?php
include( 'head.php' );
?>
<body>
<?php
include( 'menu.php' );
?>
<!--
regualar html,javascript etc
-->
<?php
include( 'footer.php' );
?>
</body>
</html>
Is it simply a file paths issue? If you're in a sub-map, then the path to head.php will be different:
<?php include ('../php/head.php'); ?>
Problem is very simple.. It would seem.
Error:
Warning: include(includes/navigation.php): failed to open stream: No such file or directory in C:\xampp\htdocs\sub_page\about.php on line 27
Warning: include(): Failed opening 'includes/navigation.php' for inclusion (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\sub_page\about.php on line 27
I am redoing a site. Currently I am splicing it into PHP. Same way I have also ways done. Except this time I am making it more structured. Example of my code.
<!-- Start Navigation -->
<?php include 'includes/navigation.php'; ?>
<!-- End Navigation -->
<!-- Start Top Menu -->
<?php include 'includes/top_menu.php'; ?>
<!-- End Top Menu -->
<!-- Start Promo Menu -->
<?php include 'includes/pro_menu.php'; ?>
<!-- End Promo Menu -->
So lets that the file navigation is in includes folder which is located in the root. Lets say that the code above is on the services page which is '2' levels in. Not 1'. Typically i have it only '1' lvl of dir in so simply changing
<?php include 'includes/navigation.php'; ?>
to this
<?php include '../includes/navigation.php'; ?>
worked fine.
The above code is on the services page which is located at
/sub_page/services/services.php
If i moved the services.php file from the above dir path to 1 lvl close to root like so
/sub_page/services.php
and change the path on the include to
<?php include '../includes/navigation.php'; ?>
it works fine. I need to be able to call the files from anywhere. Does anyone know how to do this? Thanks!
EDIT: Will use the following as an example. I was pulling a path error on this line
<?php include 'sub_page/about_us/includes/meta.php'; ?>
I was able to use the following instead
<?php include __DIR__ . "/includes/meta.php"; ?>
to fix it. However if i tried to add or take away a directory level from the path it would again return an error. The same thing will not work for any of the other lines/paths.
before
<?php include 'includes/navigation.php'; ?>
after
<?php include __DIR__ . "/navigation.php"; ?>
Any idea?
Set an include path for PHP to "look" for these files: http://php.net/manual/en/function.set-include-path.php or use an absolute path.
If I understand you correctly you want to include a file (services.php) in other files which are on different levels. The included file itself includes some other files.
You must use absolute paths in services.php, e.g.
include __DIR__ . "/navigation.php";
Then it is irrelevant from which directory level you include services.php. __DIR__ will always have the correct path of services.php, no matter where services.php is included.
EDIT: Maybe even clearer...
Assuming directory tree:
<htdocs>/index.php
<htdocs>/includes/meta.php
<htdocs>/includes/navigation.php
<htdocs>/sub_page/about.php
navigation.php is to be included by meta.php.
meta.php is to be included by index.php and about.php.
Paths for includes:
meta.php: include __DIR__ . "/navigation.php";
index.php: include "includes/meta.php";
about.php: include "../includes/meta.php";
Any other file can now include meta.php with a path depending on its directory level.
The __DIR__ in meta.php will always provide the correct path to navigation.php.
There's a site I'm developing, and I don't really understand the problem that is occurring...
There's a link inside a table in the Home page. When I click on it, It is supposed to provide some GET parameters to the hyperlinked page. The receiving page processes it, updates the database and redirects to the Home Page.
I've included some necessary php files as "require_once()" in the Home page. But I can't do it on the processing page. It gives some warnings. I don't really understand why and I don't know the solution to this problem. Please help!
Code in the Home page:
<?php require_once("includes/db_connection_open.php"); ?>
<?php require_once("includes/functions.php"); ?>
<?php include("includes/header_main.php"); ?>
<?php
echo "<td><a href='includes/process.php?id=".$arr['id']."'>Process</a></td>";
?>
<?php include("includes/body_footer_main.php"); ?>
<?php require_once("includes/db_connection_close.php"); ?>
Code in the Processing Page:
<?php require_once("includes/db_connection_open.php"); ?>
<?php require_once("includes/functions.php"); ?>
//Processing codes
<?php require_once("includes/db_connection_close.php"); ?>
The warnings I'm getting are:
Warning: require_once(includes/db_connection_open.php)
[function.require-once]: failed to open stream: No such file or
directory in C:\xampp\htdocs\MySite\includes\process.php on line 1
Fatal error: require_once() [function.require]: Failed opening
required 'includes/db_connection_open.php'
(include_path='.;C:\xampp\php\PEAR') in
C:\xampp\htdocs\MySite\includes\process.php on line 1
It seems like you are currently in the includes directory because your error says 'in C:\xampp\htdocs\MySite\includes\process.php on line 1'.
However you are still trying to require within includes/ so you end up in includes/includes/ where your files aren't at.
If you are having troubles finding the correct path because you have a file that is loaded through inclusion as well as AJAX for example you can use __DIR__ (or dirname(__FILE__) in older PHP installations) to make sure you have the correct path.
So in process.php that would be for example:
require_once __DIR__.'/db_connection_open.php';
Well, the errors seem to indicate that the files don't exist at the location you're trying. By the look of things, the process.php file already resides in the includes directory, so on your require_once() statements on the page, you need to update the links as follows:
<?php require_once("db_connection_open.php"); ?>
<?php require_once("functions.php"); ?>
//Processing codes
<?php require_once("db_connection_close.php"); ?>
Hope that helps!