Jump to content

Help please - recursive include - variables not visible


theepan

Recommended Posts

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

------------------------------

<?php
require_once("../../incs/header.php");

head('Home');

foot();
?>

 

header.php

-----------------------------

<?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

-------------------------------

<?php
/** TITLE */
global $glo_title;
$glo_title = "123auto";


/** USERNAME/PASSWORDS */

//MySQL
global $username;
global $password;

$username = "testuser";
$password = "test";
?>

 

 

-----

thanks in advance :D

 

EDITED BY WILDTEEN88: Please use code tags (


) when posting code

Nested includes/requires are relative to the first/main file (unless you change the working directory.)

 

You are also using a mix of require and include, so the include could be failing and you won't know it unless you enable all php errors and check your web server log file for errors.

 

To avoid all problems with include/require paths, do what cunoodle2 has suggested and always use a full/absolute file system path. If you use $_SERVER['DOCUMENT_ROOT'] to form the path, you won't need to know what it actually is and you won't need to change your code if you ever move servers where the file system path is different.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.