Help Plz - recursive include - variable not visible

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
theepan
Forum Newbie
Posts: 2
Joined: Sat Mar 29, 2008 4:14 pm

Help Plz - recursive include - variable not visible

Post by theepan »

Hello There,

I am trying to include one php file that includes another php file. All three files are in different folders. The variables in first php page is not visible.


Folder Structure:

Webapp
-config/config.php
-incs/header.php <- includes config
-public_html/index.php <- includes config.

Variables in config.php from header.php is not visible in index.php.
If I run header.php, config.php variables are visible.


Code:
index.php
------------------------------

Code: Select all

 
<?php
require_once("../../incs/header.php");
 
head('Home');
 
foot();
?>
 
 
header.php
-----------------------------

Code: Select all

 
<?php
include_once("../config/config.php");
 
function head($title = " "){
    ?>
    <html>
    <head>
    <title><? echo "$title - $glo_title"?></title>
    </head>
    <body>
    <?php
   
}
 
 
 
 
function foot(){
    ?>   
    </body></html>
    <?php
}
 
?>
 
config.php
-------------------------------

Code: Select all

 
<?php
/** TITLE */
global $glo_title;
$glo_title = "123auto";
 
 
/** USERNAME/PASSWORDS */
 
//MySQL
global $username;
global $password;
 
$username = "testuser";
$password = "test";
?>
 
 
-----
THx in advance :D
Last edited by theepan on Tue Apr 01, 2008 1:48 pm, edited 3 times in total.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Help Plz - recursive include - variable not visible

Post by Christopher »

index.php

Code: Select all

<?php
require_once("../incs/header.php");
 
head('Home');
 
foot();
?>
(#10850)
theepan
Forum Newbie
Posts: 2
Joined: Sat Mar 29, 2008 4:14 pm

Re: Help Plz - recursive include - variable not visible

Post by theepan »

That didn't help... Thanks anyway
sarmadmahar
Forum Newbie
Posts: 2
Joined: Mon Apr 09, 2007 12:10 am

Re: Help Plz - recursive include - variable not visible

Post by sarmadmahar »

Hello,
it is possible to use variable from different files. just just Global keyword,


EDIT | Chris Corbyn says 'no thanks' to your irrelevant advertisement sarmadmahar!
User avatar
it2051229
Forum Contributor
Posts: 312
Joined: Tue Dec 25, 2007 8:34 pm

Re: Help Plz - recursive include - variable not visible

Post by it2051229 »

am sorry to say but i've been doing an include like

Code: Select all

include("../DatabaseConnector.php");
and it really doesn't work whenever it has something like "../" in the path.. i dont know why.. it's supposed to be working it just ruined my project directory structure
User avatar
deejay
Forum Contributor
Posts: 201
Joined: Wed Jan 22, 2003 3:33 am
Location: Cornwall

Re: Help Plz - recursive include - variable not visible

Post by deejay »

how about using something like

Code: Select all

 
include ("".$_SERVER['DOCUMENT_ROOT']."includes/config.php");
 
 
there's a useful list of $_SERVER commands at http://uk.php.net/reserved.variables
Post Reply