Page 1 of 1

Problem on PHP's "include"

Posted: Wed Jun 06, 2007 4:27 am
by php_ghost
Hi everyone. I'm having problems displaying the file called "nav.php", using include, in my "index.php". The contents of nav.php displays but it can't display the images which are referenced inside nav.php with a variable declared in my config.php. Please see the codes below to understand the structure of the file.

What could be the problem with my code? TIA. :D

config.php

Code: Select all

<?
$image_path = "http://www.domain.com/images";
?>


include.php

Code: Select all

<?
include ("/var/www/domain/config/config.php");
?>
nav.php

Code: Select all

<table>
<tr><td>LINK: <img src="<?=$image_path?>/button.gif"></td></tr>
</table>
index.php

Code: Select all

<?include("http://www.domain.com/include.php")?>
<html>
<head><title>Test</title></head>
<body>
<?
include ("http://www.domain.com/common/nav.php");
?>
</body>
</html>

Posted: Wed Jun 06, 2007 4:53 am
by volka
php_ghost wrote:<?include("http://www.domain.com/include.php")?>
why do you use http://www.... here?
It starts a new php instance on the server domain.com that is not related to the "current" php instance, they do not share variables.

Posted: Wed Jun 06, 2007 5:21 am
by php_ghost
Thanks volka. That worked perfectly. :)

Code: Select all

Common
     >nav.php
Folder1
     >index1.php
Folder2
     >index2.php
Folder3
     >Folder4
           >index3.php
Case this is my file structure how can I reference my "common" folder so that everytime I want to include "nav.php" I just have to add the reference to common instead of using "../" for index 1 n 2 and "../../" for index4.

Thank you very much.

Posted: Wed Jun 06, 2007 5:47 am
by volka
I'm not sure wether it really fits your need or not but you can build such an "anchor" from $_SERVER['DOCUMENT_ROOT'], see http://www.php.net/manual/en/reserved.variables.php

Posted: Wed Jun 06, 2007 5:52 am
by php_ghost
Thanks a lot. I will try that one.

many many thanks! :D

Posted: Wed Jun 06, 2007 6:05 am
by php_ghost
Thanks volka. It worked just how I wanted it to. Thanks a lot. :D