Problem on PHP's "include"

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
php_ghost
Forum Commoner
Posts: 55
Joined: Thu Jan 11, 2007 3:29 am

Problem on PHP's "include"

Post 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>
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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.
php_ghost
Forum Commoner
Posts: 55
Joined: Thu Jan 11, 2007 3:29 am

Post 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.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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
php_ghost
Forum Commoner
Posts: 55
Joined: Thu Jan 11, 2007 3:29 am

Post by php_ghost »

Thanks a lot. I will try that one.

many many thanks! :D
php_ghost
Forum Commoner
Posts: 55
Joined: Thu Jan 11, 2007 3:29 am

Post by php_ghost »

Thanks volka. It worked just how I wanted it to. Thanks a lot. :D
Post Reply