Question

I am working on a project which will be connected to a MySQL database requires the following:

  • Registration
  • Login/Logged-in/Logout
  • Home
  • Links

Two items available for those who log in:

  • My Bookmarks
  • Contact Us

My current site structure is as follows:

 *Shortened* URL (project root folder): sois.com/440/finalproject/
  • finalproject Folder Contents: htdocs folder | mysql_connect.php
  • htdocs Folder Contents: bookmark folder | contact folder | Home folder | includes folder
  • bookmark Folder Contents (ALL PHP FILES): add | delete | deleteconfirm | index | search | searchform | update | updateform
  • contact Folder Contents: contact.php
  • Home Folder Contents (ALL PHP FILES): forgot | index | link | loggedin | login | logout | register
  • includes Folder Contents: footer.php | header.php | logo.jpg

This is an image of what you are supposed to see before LOGIN:

enter image description here

The code for /Home/index.php is as follows:

<?php # index.php
session_start();
//check session first
if (!isset($_SESSION['email'])){
include ('header.php');
}else
{
include ('header.php');
}
?>

<h2>Project Description</h2>
<h2>Blah Blah Blah ...</h2>
<h2>put images here if you want ...</h2>
<h2>change background ...</h2>
<h2>modify the site as you want ...</h2>

<?php
include ('footer.php');
?>

My question is fairly simply, I receive the errors:

  • "Warning: include(kethcart.uwmsois.com/public_html/440/finalproject/htdocs/includes/header.php): failed to open stream: No such file or directory in /home/kethcart/public_html/440/finalproject/htdocs/home/index.php on line 5"
  • "Warning: include(): Failed opening 'kethcart.uwmsois.com/public_html/440/finalproject/htdocs/includes/header.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/kethcart/public_html/440/finalproject/htdocs/home/index.php on line 5"
  • "Warning: include(kethcart.uwmsois.com/public_html/440/finalproject/htdocs/includes/footer.php): failed to open stream: No such file or directory in /home/kethcart/public_html/440/finalproject/htdocs/home/index.php on line 19"
  • "Warning: include(): Failed opening 'kethcart.uwmsois.com/public_html/440/finalproject/htdocs/includes/footer.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/kethcart/public_html/440/finalproject/htdocs/home/index.php on line 19"

Question: How can I properly link the file locations of header.php and footer.php (located in the include folder) so that my index.php (located in the Home folder) properly displays, like the above image?

While I know there are other posts floating around StackOverflow, I failed to locate one which I fully understood and could apply to my situation. I apologize if my question seems elementary but I fully appreciate any and all help!

Thanks everyone, if you have any further questions or need more details, let me know.

Was it helpful?

Solution

When you include files in PHP like you are currently doing (providing only the filename, such as header.php), PHP will look in the same directory as the file currently being executed.

From I understand of your description (I struggled to understand the formatting), this is your directory layout within your document root (I've only shown the files important to this answer):

/home/index.php
/includes/header.php
/includes/footer.php

So, if you're in /home/index.php, and you include 'header.php', PHP will assume that you're looking for the file located at /home/header.php. What you need to do is tell PHP that you're looking in a different location.

In answer to your question, a good way of doing this is telling PHP to look relative to the current directory. This is how your includes would look afterwards:

include '../includes/header.php';
include '../includes/footer.php';

This basically tells PHP to go one level up from /home (go into /), enter into the includes directory, and include the footer.php or header.php files.

OTHER TIPS

You can use dirname(__FILE__); php function - Given a string containing the path of a file or directory, this function will return the parent directory's path.

$file_path = dirname(__FILE__);
include $file_path . DIRECTORY_SEPARATOR . 'header.php';

REF: http://us1.php.net/dirname

You can get the path of the current script by using __DIR__ (or dirname(__FILE__) if DIR is not available).

Now, to include a file you can do:

<?php
define('ROOT', __DIR__.'/');
include (ROOT.'includes/header.php');
// or include relative to the root dir (if it's in a parent or sub dir)
include (ROOT.'../../header.php');
include (ROOT.'../includes/header.php');
// and if you want a "cleaner inclusion"
// the following will get the full absolute path without ".."
$path = realpath(ROOT.'../../header.php');
include ($path);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top