PHP header file isn't working in a subfolder - php

I have a reviews page on my website, and I wanted my website to be extremely user friendly, so I made it a sub-index. I have my index.php under a folder named reviews (found here) so the domain is just /reviews. When I try to include a PHP or CSS it abandons them and excludes them.
The code I am using to include my CSS (which is working on every other page) is:
<link rel="stylesheet" type="text/css" href="/stylesheets/index.css">
The PHP include() that I'm using is:
<?php
include('header.php');
?>
This PHP works on all pages that do not use a parent folder, ex. index.php (for my homepage).
The HTML in the PHP document is:
<html>
<center><nav>
<ul>
<li>Home</li>
<li>Arcade
<ul>
<li>Action</li>
<li>Arcade</li>
<li>Puzzle</li>
<li>Vehicle</li>
<li>Violence</li>
<li>Defense</li>
<li>RPG</li>
</ul>
</li>
<li>Watch
<ul>
<li>TV Shows</li>
<li>Movies</li>
</ul>
</li>
<li>Extras
<ul>
<li>Updates</li>
</ul>
</li>
<li>Support</li>
</ul>
</nav></center>
</html>
Anybody know any solutions to get my PHP and my CSS working in sub-folders?
My website is http://www.gameshank.com/
The homepage is using the header.php file!

If you know that header.php is in the same directory as from the file you want to include it to:
include(__DIR__ . '/header.php');
This is immune to changes in the working directory and therefore pretty fail-safe.
Magic constants Docs
You might find as well the following Q&A interesting:
what is the difference of absolute path in html and php ?
Relative paths : going down and up
PHP include with ../ failed open
Also it's important to understand the difference between the CSS-URL (URL-path is resolved in the browser) and the paths in PHP (those file-paths are resolved on the server, the browser is yet far far away).

Does this work?
$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/header.php";
include($path);

Call the .PHP file by:
<?php
include('../header.php')
?>
The /../header.php won't work either...
Then clear your browser history and cookies, then it works from the editing end of things.
Strange fix, but it worked. But until the editor was cleared of history and cookies, it wouldn't work.

Related

PHP - Main file wont include header

I'm trying to make a website and to reuse as much code as possible. I'm using the PHP include statement, but it won't seem to work for me. At the moment I'm only trying to get it to work with the header but I will be doing the same with the nav menu and a footer. I'm only new enough to HTML and have only started learning the include part of PHP today so any help is appreciated. The header file is in an 'includes' folder which is contained in the main website folder. Thanks.
<body>
<?php include"Includes/Header.php";?>
<div class="container-fluid">
<div id="menu">
<ul id="menu">
<li>Home</li>
<li>Our Products</li>
<li>Our Brands</li>
<li>Contact Us</li>
</ul>
</div>
</div>
<div id="img1">
<img src="lasange.jpg" alt="lasange">
</div>
</body>
Header Code
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/font- awesome/4.4.0/css/font-awesome.min.css">
<link rel="stylesheet" type="text/css" href="NewHomepageStyleSheet.css">
</head>
<body>
<div id="toplocation">
<i class="fa fa-fax"></i>
(01)-8393790&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp<i class="fa fa-map- marker">&nbsp Unit 10,11,12, Baldoyle Industrial Estate, Dublin 13, Ireland</a> </i>
</div>
</body>
</html>
Try this but make sure this below code must be written in php file:
<body>
<?php include("Includes/Header.php"); ?> // or
<?php require("Includes/Header.php"); ?>
<div class="container-fluid">
<div id="menu">
<ul id="menu">
<li>Home</li>
<li>Our Products</li>
<li>Our Brands</li>
<li>Contact Us</li>
</ul>
</div>
</div>
<div id="img1">
<img src="lasange.jpg" alt="lasange">
</div>
</body>
Because your page requires the header, change include to require. This will force PHP to throw an error when it doesn't find the file (instead of just ignoring and proceeding). The text of the error will be the key
My bet is that your path is not correct. If you use a relative path, it must be relative to the file with the include statement itself, not relative to the main website folder. My advice is to use absolute paths instead.
Finally, while you're in development I suggest that you either enable display errors so you can see error details in the browser, or find out where PHP logs errors so you can check there when something goes wrong.
Putting all of that together, I suggest you replace your include line with:
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
require "$_SERVER[DOCUMENT_ROOT]/Includes/Header.php";
?>
This should be a comment, but its getting a bit long....
In terms of structure this is messy (this not about your problem but your approach). The HTML head part should not appear inside the body tags, and frequently you'll find you want to modify the HTTP headers - which requires PHP code before any html output.
Although there's a case for putting self-contained fragments in individual include files, these should be well formed (i.e. open and close tags within the same file).
A better approach is to use functions to output structural elements of the content and invoke these from your page script, in addition to ensuring that your HTML is well formed it also protects you against security vulnerabilities introduced by emitting partial content directly from an include file when its URL is typed directly into a browser.
consider:
<?php
define('BASE_PATH', dirname(__FILE__)); // defining as root for your app simplifies includes
include(BASEPATH . '/pageTemplate.php');
include(BASEPATH . '/menu.php');
?>
<html>
<head>
<title>My demo page</title>
<?php template_required_css_and_js(); ?>
</head>
<body>
<?php template_body_start(); ?>
...
<?php menu(); ?>
...
<?php template_body_end(); ?>
</body>
</html>
(BTW: check '.' is in your php.ini include_path and the permissions on the file allow your webserver to read it. Reading the error log is always a good idea when you have a problem).
This is a collection of points from the comments, all of which are pretty good:
Your syntax could do with having a space: include "file.php";.
Your includes should be treated as case sensitive, on LAMP case sensitivity is a standard. It is a good habit to get into.
Includes need to run on a PHP page, if your page is HTML it will not include anything. PHP can include any page, it doesn't need to always call a PHP page.
Turn on PHP Error logging to check what the reason for the include not displaying is. See PHP Error Displaying.
Be sure that your include file does contain something to output to the browser!
Includes and requires and their _once counterparts are all practically identical in their way they work (there's a shocking number of folks on this question with answers thinking just changing from include to require will make it work, or changing to include_once will make it suddenly work.
The difference with require is that if the call fails then PHP ends the script with an error. Hence the contents included is required (high-hat!).
Directories
Includes typically start from your current working PHP directory, so if you have a file at /home/account/public_html/horses/index.php and you are including a file from the root html directory /home/account/public_html/include.php this can not be reached relatively from the current directory which is /horses/.
To sidestep this whole issue above you should use best practise of including files with an absolute file path, such as using $_SERVER['DOCUMENT_ROOT'] if including files which are within the public website area of your account. If including files from other parts of your account then still give an absolute path but manually, (using a DEFINE or suchlike can help) like : /home/account/extras/include.php
A few examples of each scenario:
Relative path:
include "folder/file.php";
Above: The folder needs to be in the same directory as the file running the include.
include "../parentfolder/folder/file.php";
Above: This will not work. Includes can not change relative directories above the current working directory.
include $_SERVER['DOCUMENT_ROOT']."/folder/file.php";
// reference address: /home/account/public_html/folder/file.php
Above: This include will be displayed from any page on any location of your website, as long as the include is withing the HTML Document Root (typically public_html). I recommend using $_SERVER['DOCUMENT_ROOT'] as the standard way or referencing includes.
define("RootHost","/home/account/secret");
include RootHost."/folder/file.php";
// reference address: /home/account/secret/folder/file.php
The above code would typically be used for including content that is not reachable by the website browser such as being not within your public_html HTML document root folder.
PHP root is not read the same as your standard root. Try replacing your code with this:
include($_SERVER['DOCUMENT_ROOT']."Header.php");
above Solution is right just small change:
<?php include_once("Includes/Header.php"); ?> // or
<?php require_once("Includes/Header.php"); ?>

Managing Links within PHP Includes using htaccess

Thanks for reading!
I am managing a header with links using a PHP include. It is within a folder /includes/header.php.
Here's an example of what header.php looks like:
<nav>
<ul>
<li>Home</li>
<li>Page</li>
</ul>
</nav>
When I add the include to a file within the root directory, like /index.php, I add it like so: <?php include_once("header.php"); ?>. This all works fine, and the links point where they need to.
When I do the same thing but with a file in a subdirectory, for instance a file called /foo/page.php I will add the include like this: <?php include_once("../includes/header.php"); ?> - this way it grabs the file correctly.
My problem is that all of the links in the header.php file aren't going where I want them to. I found some information about using a set environment function in .htaccess, but I don't know what to make of it.
If you have an answer to this problem I'd love to hear it! Thanks!
Start all the links in the header from the root web directory.
Just do;
"/index.html"
"/subdirectory/link.html"
So basically just start all the links with a forward slash, as without it, it will look for the page within its current directory.
You can set the base url in your HTML head.
Store the base url of your application in a config file or database and then use it to build absolute links not relative ones. For example you have a file like config.php:
<?php
$baseUrl = "http://yourdomain/yourapp/";
And in header.php:
<?php include_once("config.php"); ?>
Page
It may seem inconvenient having to edit a file in case you move your application, but this way your links will work in any directory any time, and as your application grows there will be some other things like DB access that also have to be changed if you move your application, and can be stored in the same config file.

Making a header.php file

I want to make a header file, but am not experienced with PHP in almost any aspect. My uncle was telling me that I can use a header file with PHP, and that it was like a CSS with HTML.
The following is the HTML I want in my PHP:
<center><nav>
<ul>
<li>Home</li>
<li>Arcade
<ul>
<li>Action</li>
<li>Arcade</li>
<li>Puzzle</li>
<li>Vehicle</li>
<li>Violence</li>
<li>Defense</li>
<li>RPG</li>
</ul>
</li>
<li>Watch
<ul>
<li>TV Shows</li>
<li>Movies</li>
</ul>
</li>
<li>Extras
<ul>
<li>News</li>
<li>Updates</li>
</ul>
</li>
<li>Support</li>
</ul>
</nav></center>
How could I incorperate that into a header file?
I have looked at other websites such as W3Schools, but they don't seem to work. I think my main issue is that I am not sure how to put the HTML into the PHP document. When I do it, I try to type out the HTML as if it were header.html, only, ya know, it's not.
But anyways, if anybody could even just point me in the right direction of how to incorperate that HTML I included into a header.php file, and what to put in the html.
From what I have seen, the best scenario for including the PHP document in the HTML code is:
<html>
<?php
header('Location: http://www.example.com/');
?>
Right now, I'm putting that HTML in every single page. This is restricting me from making navigational tab changes!
My website is http://www.gameshank.com/
Again, thanks in advanced, sooo much!
Ah... the PHP header command is used to output a HTTP header, so I'm really not sure what you're hoping to achieve via its use.
What you want to do is save your generic header HTML/PHP code into a new PHP file (perhaps called "header.php") and then include the contents of that file within each of your existing PHP pages via an include statement. For example:
<html>
<head><title>Sample HTML page</title></head>
<body>
<?php include 'header.php'; ?>
<h1>Page specific content</h1>
<p>And stuff.</p>
</body>
</html>
By doing this, each page will automatically contain the contents of the header.php file (which will appear wherever you place the include statement) and any changes you require can simply be made to the header.php file.
You definitely can!
On your main page (Lets say index.php), on the top you can put in
<html>
<?php
include 'header.php';
?>
</html>
(Or whatever your header file is called.).
Put that HTML in header.php you want to include.
You have to use include like this
<?php
include('header.php');
?>
or use require
<html>
<body>
<?php require("xxmenu.php"); ?>
<p>This is an example to show how to include PHP file!</p>
</body>
</html>
While you're at this, it's also a pretty good idea split the footer off of your page and place it in a seperate PHP file like you're doing with your header.
The point of this is it allows you to make changes to code that is featured in multiple pages throughout your site without having to go through every single page and make the same change.
For instance if you have "Copyright (c) 2013 Blah Blah Blah" at the bottom of 50 pages on your site, when 2014 rolls around you don't want to edit 50 pages to change 2013 to 2014. You'd just make it once in "footer.php".
And like the others have said, simply include the file in your pages.
<?php
include 'footer.php';
?>

How can I define the constant for absolute path only once in PHP?

I have the following files:
admin/config.php
header.php
index.php
contact/index.php
Both index.php and contact/index.php call header.php.
And header.php calls admin/config.php which sets the website root path WEBSITE_HTTP_ROOT. I need WEBSITE_HTTP_ROOT to be able to provide the link to the main page.
Here is header.php:
<?php
require_once('./admin/config.php');
?>
<div id="menu">
website
</div>
The line require_once('./admin/config.php');
works when called by index.php, but does not work when called by contact/index.php, because the working folder is different.
How can I define the constant for absolute path only once? and be able to call it from anywhere? or how to best avoid the above problem?
One solution is to always include files using the relative path from the current php file location obtainable using dirname(__FILE__).
For example, in header.php, you would include config.php with:
require_once(realpath(dirname(__FILE__)) . './admin/config.php');
Then in index.php:
require_once(realpath(dirname(__FILE__)) . './header.php');
and in contact/index.php:
require_once(realpath(dirname(__FILE__)) . '../header.php');
You could define a base url parameter:
define ( 'BASEURL', 'http://www.domain.com' );
And then use: <a href="<?= BASEURL ?>/header.php">
Or for front page, just
<a href="<?= BASEURL ?>">
Otherwise, just reference it as:
<a href ="/header.php">
or for frontpage:
<a href ="/">
In fact, I found a work-around (for now) since I did the include of config.php already in the main index.php, I did not need to re-include the config.php in the header.php.
So following code works fine:
<?php
?>
<div id="menu">
website
</div>
this does not really answer the question... but it fixes my problem. Might be useful for someone else.

Parent directories and PHP

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";

Categories