Add Header, Footer and navbar files on multiple pages - php

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)

Related

how to set a global link in php

I have a question, I have a file in php where I need to include another one
<?php include('includefile.php'); ?>
but if I am in a subfolder I have to call this file like this
<?php include('../includefile.php'); ?>
however, some lines in this files will change, example
<a href="css/style.css">
now is
<a href="../css/style.css">
How can I do to set no matter where in the project
thanks
Generally, how this is achieved is by using a define which is named something like ROOT which stores the absolute path to the root of your project:
config.php
define('ROOT', __DIR__ . DIRECTORY_SEPARATOR);
Then in your other files, you include this one file (config.php) and make use of the ROOT define in your code. This saves you the hassle of having to worry about the ../ and how many directories up you need to go etc.
Reading Material
Predefined Constants

PHP - structuring folders, paths, templates

I have a question about templating my php project.
Currently my folder structure looks like this:
/Root
|index.php
|_footer.php
|_header.php
|_nav.php
|
|site_1.php
|site_2.php
|
|css/
|js/
|images/
|subfolder1
|site_3.php
|site_4.php
|subfolder2
|site_5.php
I usually include "_header" and "_footer.php" in my index.php file, but I have problems with subfolder pages. For example, my _header.php has an include "_nav.php". But when I include the _header.php in the site_4.php I get the problems with the assets and navigation.
I read to make a config file to define site url, asset paths etc, but It's not so clear to me. Ideally I would like to have an "assets" folder with subfolders for css, js etc.
Also I would like to know who can I with include "_nav.php" with site_url(). I tried that out, but I always get errors.
The main question is, how to make the site_url, base url (at this point I'm still confused about those terms and how to use them)
Thank you.
Because you're using pure PHP (no framework being used) this is a bit tricky. There are several ways to solve this, I'd suggest the following:
In your Root folder, create new file, let's name it "base.php", which has the content of:
<?php
// This is base.php
define('ROOT_PATH', realpath(dirname(__FILE__)));
Now, in each of your site pages like (site_1.php, subfolder1/site_3.php, etc.) include the base.php file:
<?php
// This is site_1.php
require_once 'base.php';
// ...
In subfolder site pages, include it like this:
<?php
// This is subfolder1/site_3.php
require_once '../base.php';
// ...
If deeper site page, include it like this:
<?php
// This is subfolder1/subfolder2/site_5.php
require_once '../../base.php';
// ...
Now, in any php file, if you want to include another php file (like _nav.php), you can include it like this:
<?php
// This is _header.php
require_once ROOT_PATH . '/_nav.php';
// ...
?>
<!-- to include any css/js/images files, it would be like this: -->
<link rel="stylesheet" type="text/css" href="/css/my_css.css">
<script src="/js/my_js.js"></script>
<img src="/images/my_image.jpg">
I didn't test the code, but it should work.
What you're looking for is the HTML base tag. This will make every relative link use the specified base.
ie. having this in your head:
<head>
<base href="http://www.yoursite.com/assets/" />
</head>
...will make the following code load the image from the assets folder:
<img src="image.jpg" />
See: https://www.w3schools.com/tags/tag_base.asp

Problems linking my files PHP

I have a problem connecting my profile.php
main folder
index.php
page1.php
page2.php
page3.php
server folder
css folder
include folder
user folder
server folder contains
connect.php
css folder contains
stylesheet.css
include folder contains
header.php
footer.php
user folder contains
profile.php
all the file have
include ROOT.'include/header.php';
include ROOT.'include/footer.php';
inside my header.php
<!doctype HTML>
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/stylesheet.css">
</head>
<body>
i used
chdir('maindirectory');
define('ROOT', getcwd().DS);
everything works except my profile.php cant seems to load my css file. The reason why iam separating profile into user because i am using htaccess to remove '.php' and i want to allow user to search through user/'username' as well.
if i never separate, i'll have problems going to other page like example.com/page2 it will search the user instead of going to page2.
Try to include the files using $_SERVER['DOCUMENT_ROOT'] instead of ROOT or define ROOT as $_SERVER['DOCUMENT_ROOT']
You don't need to use chdir and define to know working folder. You can use __DIR__ constant that returns working directory (see slash before include, __DIR__ don't return last slash):
include __DIR__ . '/include/header.php';
include __DIR__ . '/include/footer.php';
In profile.php __DIR__ have root/user value. This means that yo need to do:
include __DIR__ . '/../include/header.php';
include __DIR__ . '/../include/footer.php';
If your version of PHP don't support __DIR__, you can use dirname(__FILE__) instead:
include dirname(__FILE__) . '/include/header.php';
include dirname(__FILE__) . '/include/footer.php';
If you don't know or doubt the directory you are always can echo:
echo __DIR__;

PHP: Includes directory, strucuture, and/or path issue

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.

How to display same header and footer on different pages?

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') ?>
?>

Categories