Forum Moderators: coopster

Message Too Old, No Replies

How to properly format links and includes?

Prefer not to use ../

         

King of Bling

9:22 pm on Nov 3, 2004 (gmt 0)

10+ Year Member



Good day,

I've got a simple question I hope the pro's around here can answer. I have a website with multiple sub-directories and I use PHP 'includes' to assemble my template. I am wondering how to properly format links and files so they can be accessed anywhere from the root to 3 directories down.

Wrong way
<a href="../cars/index.php">
Correct way?
<a href="$DOCUMENT_ROOT/cars/index.php">

Wrong way
<img src="../images/logo.gif" alt="" width="372" height="53" border="0">
Correct way?
<a href="$DOCUMENT_ROOT/images/logo.gif" alt="" width="372" height="53" border="0">

Thanks!
KOB

Birdman

9:46 pm on Nov 3, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I would not use DOCUMENT_ROOT for hyperlinks and images. You want to start the path at public root, not server root. The path to your public HTML root could be something like /home/username/www/, which you don't want the public to see at all.

You should format the links using the full path from the public root.

If you have an images folder that sits in the root(public) folder, then use /images/file.gif

Just remember to start your links at root(/) then use the true path from there.

<a href="/">this link will go the the home page no matter what directory you are currently in</a>

King of Bling

10:14 pm on Nov 3, 2004 (gmt 0)

10+ Year Member



Ok, that makes sense. What about includes?

For example:

<?php include '/inc/header.php';?> called from /cars/ gives this error

Warning: main(/inc/header.php): failed to open stream: No such file or directory in /home/u5/html/cars/index.php on line 13

Warning: main(): Failed opening '/inc/header.php' for inclusion (include_path='.:') in /home/u5/html/cars/index.php on line 13

Do I have to resort to this?
<?php include '../inc/a-header.php';?>

Birdman

10:44 pm on Nov 3, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry, I forgot you mento=ioned includes as well.

Your original solutions is GOOD for includes.

include($_SERVER['DOCUMENT_ROOT'] . '/inc/a-header.php');

The doc_root gets the path from server root to public root(eg. /home/user/html), then you add the path from there as you would if you were doing a regular link.

King of Bling

10:51 pm on Nov 3, 2004 (gmt 0)

10+ Year Member



Thank you Birdman. I'll give it a shot first thing tomorrow.

KOB

DaButcher

8:43 am on Nov 4, 2004 (gmt 0)

10+ Year Member



If you make a directory structure like this:


.
..
[images]
[includes]
[articles]
index.php

If you are inside the index.php and want to include something inside the includes, simply do like this:

<?php
include("includes/the_file.php");
?>

if you are inside the articles folder and want to include something in the includes folder:

<?php
include("/includes/the_file.php");
?>