PHP Relative URL Help (I Think!)

Right then, I’ve read through Kevin Yank’s BYO Database Driven Website Using PHP & MySQL (4th Edition) and I’ve also read the PHP Documentation Manual and still need help!

Firstly I’ll post an example of what I know is a tried and tested and working method, then I’ll post the example that I’m currently using and tell you what’s going wrong :slight_smile:

Static HTML & CSS Website (Working Example)

Say for example we have the following folder structure:

/example.com

index.html

/css

[INDENT]stylesheet.css[/INDENT]

/about

[INDENT]index.html[/INDENT]

index.html (in the root of the folder)

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
		<title>Example - Home Page</title>
		<base href="http://www.example.com/">
		<link rel="stylesheet" type="text/css" media="screen" href="css/stylesheet.css">
	</head>

	<body>
		<div id="container">
			<div id="header">
				<h1>Home Page</h1>
				<a href="#content" title="Send yourself straight to the content." class="skipto">Skip to content</a>
			</div>

			<div id="nav">
				<ul>
					<li><a href="/">Home</a></li>
					<li><a href="/about/">About</a></li>
				</ul>
			</div>

			<div id="content">
				<h2>Hello, SitePoint!</h2>
			</div>

			<div id="footer">
				<p>Copyright &copy; <a href="http://www.example.com">Example</a> 2010. All rights reserved.</p>

				<a href="#" title="Send yourself back to the top of the Web page.">Back to top</a>
			</div>
		</div>
	</body>
</html>

index.html (in the about folder)

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
		<title>Example - About Page</title>
		<base href="http://www.example.com/">
		<link rel="stylesheet" type="text/css" media="screen" href="css/stylesheet.css">
	</head>

	<body>
		<div id="container">
			<div id="header">
				<h1>About Page</h1>
				<a href="about/#content" title="Send yourself straight to the content." class="skipto">Skip to content</a>
			</div>

			<div id="nav">
				<ul>
					<li><a href="/">Home</a></li>
					<li><a href="/about/">About</a></li>
				</ul>
			</div>

			<div id="content">
				<h2>Hello, SitePoint!</h2>
			</div>

			<div id="footer">
				<p>Copyright &copy; <a href="http://www.example.com">Example</a> 2010. All rights reserved.</p>

				<a href="#" title="Send yourself back to the top of the Web page.">Back to top</a>
			</div>
		</div>
	</body>
</html>

stylesheet.html (in the css folder)

@charset "utf-8";

body {
	background-color: #CACACA;
}

#container {
	margin-left: auto;
	margin-right: auto;
	width: 960px;
	background-color: #FFFFFF;
	border: 1px solid #000000;
	padding: 5px;
}

Dyamic HTML, CSS, and PHP Website (Non-Working Example)

Say for example we have the following folder structure:

/example.com

index.php

/css

[INDENT]stylesheet.css[/INDENT]

/about

[INDENT]index.php[/INDENT]

/includes

[INDENT][INDENT]head.php
nav.php
header.php
footer.php[/INDENT][/INDENT]

index.php (in the root folder)

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
	<head>
		<?php include_once ("/includes/head.php"; ?>
		<title>Home Page</title>
	</head>

	<body>
		<div id="container">
			<div id="header">
				<h1>Home Page</h1>
				<a href="#content" title="Send yourself straight to the content." class="skipto">Skip to content</a>
			</div>

			<div id="nav">
				<?php include_once ("/includes/nav.php"); ?>
			</div>

			<div id="content">
				<h2>Hello, SitePoint!</h2>
			</div>

			<div id="footer">
				<?php include_once ("includes/footer.php"); ?>

				<a href="#" title="Send yourself back to the top of the Web page.">Back to top</a>
			</div>
		</div>
	</body>
</html>

index.php (in the about folder)

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
	<head>
		<?php include_once ("includes/head.php"); ?>
		<title>Example - About Page</title>
	</head>

	<body>
		<div id="container">
			<div id="header">
				<h1>About Page</h1>
				<a href="#content" title="Send yourself straight to the content." class="skipto">Skip to content</a>
			</div>

			<div id="nav">
				<?php include_once ("/includes/nav.php"); ?>
			</div>

			<div id="content">
				<h2>Hello, SitePoint!</h2>
			</div>

			<div id="footer">
				<?php include_once ("/includes/footer.php"); ?>

				<a href="#" title="Send yourself back to the top of the Web page.">Back to top</a>
			</div>
		</div>
	</body>
</html>

head.php (in the includes folder)

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
		<base href="http://www.example.com/">
		<link rel="stylesheet" type="text/css" media="screen" href="css/stylesheet.css">

nav.php (in the nav includes folder)

<ul>
					<li><a href="/">Home</a></li>
					<li><a href="about/">About</a></li>
				</ul>

footer.php (in the includes folder)

<p>Copyright &copy; <a href="/">Example</a> 2010. All rights reserved.</p>

And the results of this method is that the include links in the index.php page in the about folder don’t work! :frowning: Help please?

Andrew Cooper

Hey, thanks for the reply Winawer :slight_smile: I’ve done that previously and it works fine, I probably should have mentioned that! But what I’m looking to do is ideally have the same relative include links across the whole Website no matter which directory the each file is in.

Can you think of a way to do this? Would be great if someone could help me accomplish this as soon as possible! :smiley: Would make my whole job a lot easier!

Andrew Cooper

Try include_once(“…/includes/head.php”) etc.