First of all, there are plenty of questions asked in the same category but none of those problems i face.. so thats why i opened up this question.. and secondly im not php developer... know just basics include, require for header and footer..
this is my directory structure:
xampp>htdocs>website>"folders" and "files"
"folders" are css, sass, js, pictures and projects.
"Projects" folder has projecta.php and projectb.php
"files" are index.php, header.php, footer.php, menu.php
so above is my folder structure.. everything is working great when the files are in root directory.. say for example, including header, menu and footer php in index page is working fine..
so now i wanted to include the header, footer and menu page for projecta in "projects" folder, so i tried this
<?php include "../footer.php"; ?>
it is not working.. look at the below picture
it looks like the css files are not executing..
for more info here is the header snippit
<title><?php echo $page_title; ?></title>
<meta name="description" content="<?php echo $page_des; ?>">
<meta property="og:title" content="<?php echo $page_title; ?>" />
<meta property="og:description" content="<?php echo $page_des; ?>" />
<meta property="og:type" content="website" />
<meta property="og:url" content="http://www.example.com/<?php echo $page_url; ?>" />
and here is menu snippit
<ul>
<a <?php if ($page_nav == 'index.php') { ?>class="active"<?php } ?> href="index.php"><li><h2 class="mt">Home</h2><i>Go to</i></li></a>
<a <?php if ($page_nav == 'about.php') { ?>class="active"<?php } ?> href="about.php"><li><h2 class="mb">About</h2><i>Me</i></li></a>
<a <?php if ($page_nav == 'projects.php') { ?>class="active"<?php } ?> href="projects.php"><li><h2 class="mt">Work</h2><i>My</i></li></a>
<a <?php if ($page_nav == 'contact.php') { ?>class="active"<?php } ?> href="contact.php"><li><h2 class="mb">Contact</h2><i>Me</i></li></a>
</ul>
and this is how i include header and menu in my pages,
<?php
$page_title = "title a";
$page_des = "description a";
$page_url = "index.php";
include "header.php";
?>
<?php
$page_nav = basename($_SERVER['SCRIPT_NAME']);
require "menu.php"; ?>
so someone please help to find a valid answer? yes, i know, you guys will find plenty of mistakes in the code shown above.. but thats okay.. i need answers.. thanks...
found an answer: first of all thanks for everyone who gave thier suggestions.. i tried all the below, but nothing works.. so i found this code works fine.. but the thing is it somewhat looks like the answers by #Dane Caswell and #Scott Dallas.. but i have no idead why thier answer doesnt work and why this one works..! but anyways thank you everyone.. im pasting the answer here cause someone might find it useful..!
<?php
$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/your directory/common/header.php";
include_once($path);
?>
This problem is because the PHP interpreter has far greater access the file system than just the public_html or htdocs (site root) directory. It has access to all files and folders above the root. Can make it difficult to establish the actual current working directory. PHP will check the current working file location for the file reference before failing. see PHP Docs for include()
Files are included based on the file path given or, if none is given,
the include_path specified. If the file isn't found in the
include_path, include will finally check in the calling script's own
directory and the current working directory before failing.
The the solution I usually use is to define the servers document root using PHP $_SERVER variable. This solution is not completely fool proof as it depends on the server environments' document root configuration.
include($_SERVER['DOCUMENT_ROOT'] . '/footer.php');
I have found that this can also be annoying is you are working on a local server where the public_html or htdocs folder may contain sub folders for each website, like yours for example. The $_SERVER['DOCUMENT_ROOT'] will on most configurations return the absolute path to the server 'public' root, not your websites subfolder. This means you have to add the website folder to the include path.
include($_SERVER['DOCUMENT_ROOT'] . 'your_website/footer.php');
This is pretty annoying because it means that when the website is uploaded to a production server it is likely you will not have a additional sub folder for the website, so this include would need changing.
Obviously this is a real pain and not great practice, I afraid I don't know what the solution to that problem would be, albeit I am sure there would be a better way. Hopefully this can help you to get going and maybe someone else can provide (us both) a better practice solution.
This annoys me as well.. You can use
$_SERVER['DOCUMENT_ROOT']
I like to do this at the top of all my files when I'm starting a new project:
<?php include($_SERVER['DOCUMENT_ROOT'] . "/inc/config.php"); ?>
In my config file, I store lots of variables as useful values that I am going to use throughout my project so I don't have to keep typing $_SERVER['DOCUMENT_ROOT']
Or you can just stop being lazy like me and use $_SERVER['DOCUMENT_ROOT'] as much as you like :)
Related
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'); ?>
I have researched so much in blogs and forums and can not seem to find the solution to this problem. I am using the Wamp directory: C:\wamp\bin\apache\apache2.4.9\htdocs\lr
the lr at the end of the /htdocs is just the folder that I am holding my webpage in.
The code inside of my index.php :
<html>
<?php include('includes/head.php'); ?>
<body>
<?php include 'includes/header.php'; ?>
<div id="container">
<?php include 'includes/aside.php'; ?>
</div>
<footer>
© phpacademy.org 2011. All rights reserved.
</footer>
</body>
When I load the index.php in chrome or any browser the only thing that I am seeing is the footer which happens to be the only thing that is NOT set up in a php include tag.
Here is the code inside of my head.php
<head>
<title>Website Title</title>
<meta charset="UTF-8">
<link rel="stylesheet" href="css/screen.css">
Very basic code which only gives me more confusion on why it is not working. If you would like more examples of code that I am using let me know. I will gladly provide some as I really need this to continue creating the database for my login/registration page(s).
It is likely that PHP does not find the files to be included.
Are you sure the folder 'includes' is in the same directory as your index.php?
Also, I would suggest to use "**/**includes/..." with a slash at the start to be sure that it searches the included files from the root directory, and not from the relative path of your index.php.
Besides, shouldn't your root be C:\wamp\www\ in WAMP?
You can check if the included file exist, this can give
you a clue to what is going on
if(file_exists('includes/head.php')):
include 'includes/head.php';
else:
echo 'file either not readible or does not exist';
endif;
Try to close head tag first in your head include file, if that doesn't work, I will dig deeper :D
EDIT:
Sometimes there are problems with file path. I usually use:
<?php include $_SERVER['DOCUMENT_ROOT']."/filename.php";?>
In Your case that probably be:
<?php include $_SERVER['DOCUMENT_ROOT']."/lr/includes/header.php";?>
I'm using php include. Now the files are in the sub-folder.
The error goes exactly like this:
Warning: include(/headertop.php): failed to open stream: No such file or directory in D:\ROLDANKING\xampp\htdocs\mysite\pages\print_design.php on line 11
The HTML/PHP file is this:
<html>
<head>
<title>PRINT DESIGN</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<link rel="shortcut icon" href="../images/art_favicon.png" type="image/x-icon"/>
<link rel="stylesheet" href="../css/body.css" type="text/css" media="screen"/>
</head>
<?php include ("headertop.php"); ?>
<?php include ("header.php"); ?>
<?php include ("nav.php"); ?>
<body>
<div id="contents">
</div>
</body>
<?php include ("footer.php"); ?>
</html>
Assuming you have the paths correct and files in place you can try this...
<?php
include ("sub-folder/headertop.php");
include ("sub-folder/header.php");
include ("sub-folder/nav.php");
?>
The thing you want to avoid is having to change the path to an include on each page. You can do that with something like this:
<?php include $_SERVER["DOCUMENT_ROOT"] . "/includes/header.php"; ?>
That will work nicely online, but to work in XAMPP, you need to set up a vitrual host so that the link points to the same thing: http://sawmac.com/xampp/virtualhosts/
Warning: include(/headertop.php): failed to open stream: No such file or directory in D:\ROLDANKING\xampp\htdocs\mysite\pages\print_design.php on line 11
says there is NO headertop.php file in your directory
check if the file exists: D:\ROLDANKING\xampp\htdocs\mysite\pages\headertop.php
also you can just use:
<?php
include ("headertop.php");
include ("header.php");
include ("nav.php");
?>
instead of:
<?php include ("headertop.php"); ?>
<?php include ("header.php"); ?>
<?php include ("nav.php"); ?>
<?php include ("SUB_FOLDER/headertop.php"); ?>
try this
include '../headertop.php';
or
include '/headertop.php';
I have come across dozens of webpages over the last 3 days and I think I have collectively tested exactly what most people coming here are looking for.
ABSOLUTE PATHS. They must be manually established on your local host, and then again on your Live website, but this is nothing shy of declaring a variable set to a particular pre-built function.
Stay with me..
Every time I reference a link in a php (includes and echos), or in html and css, I reference a variable set to the root directory + that original link.
e.g.
background-image: url(<?php echo $root; ?>images/bg-0.jpg);
The only downside to this is the visibility of extra code and tediousness of adding a $variable to each and every link in a css or php document. And lord forbid javascript, because I havent even touched the root of that. Heh. Puns.
Anyways, to make this work..
In my Styles.css Doc, I simply convert it to Styles.php, encase the CSS code in
<style type="text/css"> *CSS* </style>
tags, which enable the echo of a PHP variable but as a string, or in our case an absolute path precursor.
In my header file, I now include my CSS as a PHP include with a new Root Variable.
include $php_root.'includes/css/styles.php';
Notice how this $variable is different than the echo's? The way I declared the variables plays a huge role in how CSS/HTML perceives a root destination, and how PHP sees it.
So these were my set variables.
// WAMP Localhost
$root = "http://localhost/PZD/";
$php_root = $_SERVER['DOCUMENT_ROOT'] . "PZD/";
// Live Server
$root = "http://prozechdesigns.com/";
$php_root = $_SERVER['DOCUMENT_ROOT'];
These may be set in your main header.php include itself.
Or if you're like me with database files to connect to and communicate with, you might pair it with a database connect.php file. I did; that way, when I call upon my init.php file or need to edit the init.php, I do not need to worry about which $root variables are being used after overwrite between localhost and live website.
You may not be using WAMP as you're reading this, and at your time and date or configuration, your PHP Root location may not be set like mine is. But if you are using WAMP and perhaps wish to find out where your root is set or change it, look for the httpd.conf file located by default in "wamp/bin/apache/apache#/conf".
I am making a website in which I want a column on the left hand side to be a list of links. This column populated with links will be on every single web page of the entire site, and it will be updated with more links quite frequently. So the problem I need to solve is how to make it so that whenever I want to add a new link to my website's "link list", I don't have to manually add it in the HTML code in every single web page.
I know some Java but I'm completely unfamiliar with PHP. I did a bit of research and found the "include" declaration, and kind of slapped together something that does work, but I have no clue whether this is a good idea or not - as in if its bad code/bad style, so could I get some opinions on this please? Is it good code? Or am I going about it the wrong way?
The entire php file that contains the array, title "videos_array.php":
<?php
$videoArr = array(
"<li>Video 1</li>",
"<li>Video 2</li>",
"<li>Video 3</li>");
?>
And the web pages where I want to insert these values into look like this:
<html>
<head>
<!--All the necessary tags-->
</head>
<body>
<!--More Tags-->
<div id="link_column">
<?php include 'videos_array.php';
foreach($videoArr as $val){
echo $val;
}
?>
</div>
</body>
</html>
When referencing a php file with the include declaration, is it a relative path that I should be using, or are absolute and virtual references ok as well?
Thanks in advance for your input
Your $videoArr is used only once, so there is no need to use it in another php file you can just echo after declaring it like,
<?php
$videoArr = array(
"<li>Video 1</li>",
"<li>Video 2</li>",
"<li>Video 3</li>"
);
foreach($videoArr as $val){
echo $val;
}
?>
Even no need to create an array if it is not used just echo it as a string like,
<?php
echo "<li>Video 1</li>",
"<li>Video 2</li>",
"<li>Video 3</li>";
?>
And in your index page use it like,
<div id="link_column">
<?php include 'videos_array.php';?>
</div>
When referencing a php file with the include declaration, is it a relative path that I should be using, or are absolute and virtual references ok as well?
What you're doing is fine however there's some points to be wary of around PHP's include path.
The include path is a FIFO stack of server file-system paths that PHP will use as base directories when resolving paths provided in include and require statements (and probably some other things). The basic include path usually includes . (or the current working directory). This is resolved as the directory containing the first (parent) PHP script executed. This can catch people out once you go a few includes deep.
What I find generally works best is to prefix include paths with the __DIR__ constant. This resolves to the parent directory of the current script.
For example, say you have the following directory structure
foo.php
bar.php
dir/baz.php
To include bar.php from foo.php, you can use
include __DIR__ . '/bar.php';
To include bar.php from dir/baz.php
include __DIR__ . '/../bar.php';
You can also manipulate the include_path configuration. For example (in foo.php)
// add "dir" to the top of the include path stack
set_include_path(implode(PATH_SEPARATOR, [
realpath(__DIR__ . '/dir'),
get_include_path()]));
// now include baz.php
include 'baz.php'; // this works because we added "dir" to the include path
I really hope there's a simple solution to this.
<?php include("header.php"); ?>
Let's say I have a php header in my root folder simply titled header.php. In that header there is a link back to the home page, main.php, and main.php is also located on the root. No problem so far. Here's what header.php looks like. Simple stuff, right?
<link href="style.css" rel="stylesheet" type="text/css" />
<div id="headerwrap">
<div id="linkbox">
<img src="images/mainlogo.png" />
</div><!-- End linkbox -->
</div>
However, let's say I have other pages in subdirectories. Subpage.php is located in a child directory of the root, and so it has to look back to the root to get the included header.php.
<?php include("../header.php"); ?>
That wouldn't be a big deal, except that header.php links back to main.php and also style sheets, none of which are in *subpage.php's directory, thus causing an error when someone on Subpage tries to get back to Main via the link in the header.
I'm just hoping there's a simple way to make this work, and that I don't have to copy and redirect all includes into every subdirectory. Also, there are too many pages to really reasonably include them all on the root. Sorry if this answer is posted elsewhere; I've looked and just have no real idea what I'm looking for. Thanks for your help. Hope all that makes sense.
You could just hard code main.php's path within header.php:
<img src="http://website.com/images/mainlogo.png" />
As opposed to a php prob this seems to be an html prob..
Your links should be relative links with a preceding / i.e.
Text
instead of
Text
how about using absolute links. header.php should also reference main.php absolutely, then there should be no troubles:
<?php include($_SERVER['DOCUMENT_ROOT'].'/header.php"); ?>
You can use the base html tag:
<base href="http://yoursite.com/" />
This way you can use that url as the base for all your links/stylesheets/images and you don't need to worry if they're in a subdirectory.
the best thing to do is to get in the habit of using
$_SERVER['DOCUMENT_ROOT']
that way you have no confusion as to what directory you're in, etc.
so including your header for example would be as simple as :
include $_SERVER['DOCUMENT_ROOT'] . "/header.php";