Problems with Includes

I am really struggling with file paths and includes… :bawling:

In the “web root” of my web hosting account, I have this structure…

index.php
includes/header.html
css/style.css

index.php


<?php
	// This file is the home page.

	// Get Header file.
	include ('includes/header.html');

	// Other code
?>

header.html


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
	<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

		<!-- HTML HEADER -->
		<head>
			<title>
			</title> 
			<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
			<meta name="description" content="Place your description here" />
			<meta name="keywords" content="put, your, keyword, here" />
			<meta name="author" content="Templates.com - website templates provider" />
			[COLOR="Red"][b]<link href="/css/style.css" rel="stylesheet" type="text/css" />[/b][/COLOR]
		</head>

If I INCLUDE “header.html” in “index.php” then I thought that it is like “header.html” becomes part of “index.php” and so any references in “header.html” should be based on their relationship to “index.php”?? :-/

In my mind, the reference to my style sheet should be…

<link href=[b]"css/style.css"[/b] rel="stylesheet" type="text/css" />

because that is how the style sheet is positioned relative to “index.php”.

As it turns out, if I don’t add a “/” in front of the code above then I lose the styles.

Can someone please explain what is going on?!

Debbie

Hmm, odd. I would expect that to work too. The code from the include should just become part of the index.php page, as you say.

TBH, I would recommend using the slash before the css folder anyway, as then it works from any page, no matter where in the site. The same goes for the include link:

<?php include $_SERVER["DOCUMENT_ROOT"] . "/includes/header.html"; ?>

This can be a pain if you are developing locally, but I recently found that you can set up MAMP (and I presume WAMP etc) so act exactly like an online server—removing “localhost” etc from the url, so that the local setup exactly matches the online setup. It’s very handy.

Unfortunately it doesn’t work that way.

Any in fact, it seems like it works in some cases but not others.

My head is spinning between NetBeans and my Web Host.

TBH, I would recommend using the slash before the css folder anyway, as then it works from any page, no matter where in the site.

Yeah, but lots of other code examples in my book aren’t that easy.

It is a nightmare trying to learn PHP and debugging the author’s crappy coding style…

(What I need is a PHP “angel” to swoop in and help me work through things…)

The same goes for the include link:

<?php include $_SERVER["DOCUMENT_ROOT"] . "/includes/header.html"; ?>

That determines where your web root is at?

This can be a pain if you are developing locally, but I recently found that you can set up MAMP (and I presume WAMP etc) so act exactly like an online server—removing “localhost” etc from the url, so that the local setup exactly matches the online setup. It’s very handy.

How do you do that?

Debbie

I guess so. With includes, you need to use this for absolute links.

How do you do that?

This is how I learned to do it with MAMP:

http://foundationphp.com/tutorials/vhosts_mamp.php

… and I expect the process is similar for Windows equivalent like WAMP.