1

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.

Taylan Aydinli
  • 4,333
  • 15
  • 39
  • 33
rockmandew
  • 820
  • 13
  • 22
  • `include ('../includes/header.php');` – shanethehat Nov 20 '13 at 09:33
  • for putting images on internet, you could use one of the free image hosting next time instead of google drive (most of them doesn't even require an account) – Asenar Nov 20 '13 at 09:59
  • Thank you both for your response. Asenar - I apologize for the image hosting issue, Google Drive was just the easiest, immediate solution for me because I have it installed on my desktop and have direct access to the link. I will keep your suggestion in mind for future posts. – rockmandew Nov 20 '13 at 20:37

3 Answers3

0

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.

garbetjie
  • 579
  • 3
  • 10
  • Thank you very much for your precise explanation, sorry for the formatting issues - I have always used StackOverflow for my coding questions but never actually created an account until last night so I am still getting the feel for the textarea input. Again, thanks you. I will post my updated code when I get the chance. – rockmandew Nov 20 '13 at 20:36
0

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

Krish R
  • 22,583
  • 7
  • 50
  • 59
0

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);
Asenar
  • 6,732
  • 3
  • 36
  • 49
  • I was looking at this, on another post [link](http://stackoverflow.com/questions/2418473/when-should-i-use-require-once-vs-include), as a possible solution but dont really understand how it works exactly. Would you say there is any particular advantage to this method, when compared to the (..include/header.php) method? – rockmandew Nov 20 '13 at 20:41
  • the advantage is if you make the code evolve it would be simplier if you do like this (and adding others constants like LIBPATH, VIEWPATH and such), so when you will move all your files you will only to adapt one little piece of code . The other advantage is that this will works even if you change the include path (set_include_path ). – Asenar Nov 20 '13 at 21:21