Jump to content

[SOLVED] Includes not sharing variables


Xeoncross

Recommended Posts

I have a file in an admin area that is not getting access to the $site_config variables in a config file. Here is what my simplified file system looks like:

 

config.php
index.php
/lang/en.php
/admin/index.php

 

When I run index.php in the root dir it reads config.php and finds the file "en.php" in the language folder. Because config has a value called "$site_config["lang"]" which is = "en" so config includes "en.php";

 

Now when I access /admin/index.php it also reads the config file but it tries to find "/lang/.php" because the variable "$site_config["lang"]" appears to be empty.

 

Here is what /admin/index.php looks like:

require("../config.php");
require("../lang/". $site_config["lang"]. '.php');

 

Anyone know what might be wrong? Why is $site_config["lang"] set to "en" for index.php and not for /admin/index.php?

 

Link to comment
https://forums.phpfreaks.com/topic/67077-solved-includes-not-sharing-variables/
Share on other sites

How does php read files? like take the "include" logic bellow. Each of the ">" means that it is requiring the following file:

 

index > functions > config > site_config
                  > lang

 

Would php get to the "config" page run it and then go back to "functions" and include "lang" before it included "site_config"?

ok, I found the problem - in config.php there is a the code like this:

 

 

<?php
$file = 'sites/site_config.php';

if(file_exists($file)) {
    require_once($file);
}
?>

Somehow that code was true on the "/index.php" file but not on "/admin/index.php".

Therefore the $site_config array was included on "/index.php" but not on "/admin/index.php".

 

aghhhh....

 

 

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.