Page 1 of 1

include link problem when in different folders

Posted: Mon Aug 30, 2004 3:50 pm
by fxchain
I searched for an answer, but no luck.

I have header.php in root folder:

Code: Select all

<html><body>
<a href="folder1/folder2/file1.php">File1</a><br>
<a href="folder1/folder2/file2.php">File2</a>
</body></html>
I have index.php in root:

Code: Select all

<html><body>
<?php include('header.php');?>
</body></html>
(php parses links ok)

I then have file1 in /folder1/folder2/:

Code: Select all

<html><body>
<?php include('../../header.php');?>
</body></html>
But when php parse file1.php, the links from header.php are wrong, they go to:
folder1/folder2/folder1/folder2/file1.php
as oppose to just:
folder1/folder2/file1.php

It duplicates locations. The header links + it's current location.

Please help.
Francois

Posted: Mon Aug 30, 2004 4:40 pm
by timvw
You could use /folder/path/file

And if you tweak the php include_path you don't have to bother about the relative path to header.php either.

Posted: Mon Aug 30, 2004 4:54 pm
by fxchain
You could use /folder/path/file
Not quite sure what you mean...

Posted: Mon Aug 30, 2004 5:00 pm
by feyd
absolute paths, versus relative paths.

Posted: Mon Aug 30, 2004 5:26 pm
by ol4pr0

Code: Select all

#include
#require...
require_once($_SERVER['DOCUMENT_ROOT']."/include/path/page.php");

Posted: Mon Aug 30, 2004 5:51 pm
by fxchain
Found much easier answer:

Changed header.php in root folder:

Code: Select all

<html><body> 
<a href="/folder1/folder2/file1.php">File1</a><br> 
<a href="/folder1/folder2/file2.php">File2</a> 
</body></html>
Note the / in front of path. That made it work.

Thanx for all your help

Posted: Mon Aug 30, 2004 5:54 pm
by chemburn
This question isn't really realated to PHP, but it sorta is.

He's talking about when the page is rendered in the browser, the links are linked to folder1/folder2/folder1/folder2/file1.php. The way to fix this is prepend all of your <a tags, with a /. So in your instance.

Code: Select all

<html><body> 
<a href="/folder1/folder2/file1.php">File1</a><br> 
<a href="/folder1/folder2/file2.php">File2</a> 
</body></html>
OR, add a base href tag to the document. So that no matter where in the site the header file is included, the links point to the right place on on the root of the site.

Code: Select all

<html><body> 
<base href="/">
<a href="folder1/folder2/file1.php">File1</a><br> 
<a href="folder1/folder2/file2.php">File2</a> 
</body></html>
I'd recommend the first option, it's more work, but it also wont effect other links on the other pages that may be included. Just get in the habbit of prepending a / to all of your links.

Have fun when you move it to another folder. :)

"Moderator, I wonder what happened to my first amendment rights???"