/    Sign up×
Community /Pin to ProfileBookmark

Global Question

On the top of each PHP Page, I require a config.php page.

Some of the variables on the config.php page are used in files from the includes folder.

So for my footer, I include includes/footer.php.

A variable that is in config.php is $URL.

When I echo $URL in includes/footer.php, the it does not display anything, does any one have any ideas?

Should these variables in config.php be set as global?

to post a comment
PHP

12 Comments(s)

Copy linkTweet thisAlerts:
@comptech520authorSep 12.2008 — Please allow me to rephrase this:

When I require config.php, it popuplates all of the variables with data on index.php, the page I required it on.

On index.php I include files from other directories, with some variables echo'ed from config.php, on the files that are INCLUDED, the varibles don't populate.

How can I make this work?
Copy linkTweet thisAlerts:
@ReverendAlGreenSep 12.2008 — It's not a problem, including a file into another is the same of writing the code inside it, so you can use variables defined into an included file wherever you want.

If you can post the code it would be easier to find where the bug is.
Copy linkTweet thisAlerts:
@comptech520authorSep 12.2008 — [code=php]$website_URL = 'http://www.mydomain.com';

<a href="<?php echo $website_URL . 'index.php'?>" title="Home">Home</a><span class="separator">|</span><a href="<?php echo $website_URL;?>dj.php" title="DJ Services">DJ Services</a><span class="separator">|</span><a href="<?php echo $website_URL;?>photoweb.php" title="Photography services">Photography services</a><span class="separator">|</span><a href="<?php echo $website_URL;?>testimonials.php" title="Client Testimonials">Client Testimonials</a><span class="separator">|</span><a href="<?php echo $website_URL;?>vendors.php" title="Preferred Vendors">Preferred Vendors</a><span class="separator">|</span><a href="<?php echo $website_URL;?>about.php" title="About Us">About Us</a><span class="separator">|</span><a href="<?php echo $website_URL;?>contact.php" title="Contact Us">Contact Us</a>[/code]
Copy linkTweet thisAlerts:
@comptech520authorSep 12.2008 — This is my code, I am receiving undefined variables from files that are in other directories that reference variables in config.php

For example, I have footer.php to have some links in it. I use the links like this: <a href="<?php echo $website_URL;?>index.php" title="Home">Home</a>, it says that $URL is an undefined variable. When I put this ($website_URL = "[URL]http://www.mydomain.com/new/[/URL]"? at the top of the footer.php file, they load fine.

Can someone please take a look at my code to see if something is missing or in the wrong place or not closed on the PHP side?

config.php
[code=php]<?php
$website_URL = "http://www.mydomain.com/new/";
$website_FULL = "/var/www/vhosts/mydomain.com/httpdocs/new/";
$website_title = "";
$right_header = "";
$clients_link = "";
$photoweb_link = "";
$event_type_enabled = "1";
?>[/code]


index.php
[code=php]
<?php require ('config.php')?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title><?php echo $website_title;?></title>
<link href="<?php echo $website_URL;?>css/import.css" type="text/css" rel="stylesheet" />
</head>
<body>
<div id="main">
<div id="banner"><table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td><img src="<?php echo $website_URL;?>images/banner-1.jpg" width="43" height="117" alt="" /><img src="<?php echo $website_URL;?>images/banner-2.jpg" width="102" height="117" alt="" /></td><td><img src="<?php echo $website_URL;?>images/es-1.gif" width="335" height="23" alt="" /><br />
<img src="<?php echo $website_URL;?>images/es-2.gif" width="335" height="24" alt="SPEK" /><br /><img src="<?php echo $website_URL;?>images/es-3.gif" width="335" height="23" alt="Entertainment, the way it should be!" /><br /><img src="<?php echo $website_URL;?>images/es-4.gif" width="335" height="17" alt="Phone: 203-729-1172 &mdash; Toll free: 866-329-1172" /><br /><img src="<?php echo $website_URL;?>images/es-5.gif" width="335" height="30" alt="" /></td><td><img src="<?php echo $website_URL;?>images/banner-4.jpg" width="75" height="117" alt="" /></td><td width="396" valign="top"><img src="<?php echo $website_URL;?>images/banner-ball-1.jpg" width="396" height="52" alt="" /><img src="<?php echo $website_URL;?>images/banner-ball-2.jpg" width="396" height="12" alt="DISC JOCKEY SERVICES &bull; EVENT PHOTOGRAPHY" /><img src="<?php echo $website_URL;?>images/banner-ball-3.jpg" width="396" height="27" alt="" /><br />
<img src="<?php echo $website_URL;?>images/top-options.jpg" width="81" height="26" alt="" /><a href="<?php echo $clients_link;?>" title="Client Login" target="_blank"><img src="<?php echo $website_URL;?>images/top-options-client.jpg" width="100" height="26" alt="Client Login" /></a><img src="<?php echo $website_URL;?>images/top-options-2.jpg" width="17" height="26" alt="" /><a href="<?php echo $photoweb_link;?>" title="Photoweb Login" target="_blank"><img src="<?php echo $website_URL;?>images/top-options-photo.jpg" width="115" height="26" alt="Photoweb Login" /></a><img src="<?php echo $website_URL;?>images/top-options-3.jpg" width="83" height="26" alt="" /></td></tr></table></div>
<div id="navBarLeft" class="left"><img style="padding-bottom: 5px;" src="<?php echo $website_URL;?>images/navBarTop.gif" width="219" height="29" alt="" /><br />
<?php include $website_URL . 'includes/navigation.php'?>
<br />
<img src="<?php echo $website_URL;?>images/menu-bottom.jpg" width="219" height="374" alt="" /></div>
<div id="content" class="left">
<div><img src="<?php echo $website_URL;?>images/content-top.gif" width="746" height="27" alt="" /></div>
<div class="txt">
<div class="leftCol left">
</div>
<div class="rightCol left">
<?php
include $website_URL . 'includes/right_column_cond.php';
switch ($event_type_enabled) {
case 0:
echo "";
break;
case 1:
include $website_URL . 'includes/eventsnav.php';
break;
}
?>
</div>
<div class="clearThis"><img class="clearThis" src="<?php echo $website_URL;?>images/content-bottom.gif" width="728" height="11" alt="" /></div>
</div>
</div>
<div id="footer">
<?php include $website_URL . 'includes/footer.php';?>
</div>
</div>
</body>
</html>

[/code]
Copy linkTweet thisAlerts:
@svidgenSep 12.2008 — Yar, after taking a glance at your code, I don't see why you're having a problem with this. Are all of the config variables failing to appear anywhere/everywhere?
Copy linkTweet thisAlerts:
@comptech520authorSep 12.2008 — the config variables are failing to load ONLY IN FILES THAT ARE INCLUDED

They work fine on the index.php page, bit if I include a page from another directory they wont load.

Whats up with that?

[COLOR=#dd0000][/COLOR]

[COLOR=#dd0000][/COLOR]
Copy linkTweet thisAlerts:
@svidgenSep 12.2008 — Oh, forgive me. By the time I got through the code I had forgotten that the problem was specific to the other included files.

An included/required file should use the same scope as the line it is included on. So, all variables that are available [B]immediately[/B] before and after the include are also available to the included file: http://us2.php.net/manual/en/language.variables.scope.php

My assumption is that the config variables are being referenced from within a function call. If this is not the case, you could throw [I]global $variable_name;[/I] in there anyway--on the off-chance PHP just [I]thinks[/I] your variables are out of scope.
Copy linkTweet thisAlerts:
@comptech520authorSep 12.2008 — This is what I used in my config.php file, and still nothing. Please correct me if I'm wrong.

global $website_URL;

global $website_FULL;

[COLOR=#0000bb]$website_URL [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#dd0000]"http://www.mydomain.com/new/"[/COLOR][COLOR=#007700];

[/COLOR]
[COLOR=#0000bb]$website_FULL [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#dd0000]"/var/www/vhosts/mydomain.com/httpdocs/new/"[/COLOR][COLOR=#007700];[/COLOR]
Copy linkTweet thisAlerts:
@svidgenSep 12.2008 — If there's any need for the global keyword, it doesn't belong in config.php. It belongs in the other includes files. But again, according to PHP's documentation [I]and my experience[/I], this should only be an issue if you're trying to access these variables from within a function. In that case, [I]global $variable_name;[/I] needs to be in the function itself before referencing [I]$variable_name[/I].

Otherwise, there [I]should[/I] be no problems. Check for typos?
Copy linkTweet thisAlerts:
@NogDogSep 12.2008 — As I suggested in your [url=http://www.webdeveloper.com/forum/showthread.php?t=191092]other recent thread[/url], if you are including via URL and the file type is one which will be processed by your web server as PHP, then it is being separately parsed and executed and your calling file is only receiving the output of the include file. Thus the included file is totally ignorant of any variables in the calling file. To remedy this you would need to do the include via a file system path, not a URL. (You could alternatively give the include file a suffix which would not be processed as PHP, but then its source code would be viewable by anyone who might know/guess the file's name, plus it is inefficient as any URL still requires interaction with the web server.)

So, instead of a URL, do something like:
[code=php]
include $_SERVER['DOCUMENT_ROOT'] . '/includes/filename.php';
[/code]
Copy linkTweet thisAlerts:
@svidgenSep 12.2008 — if you are including via URL and the file type is one which will be processed by your web server as PHP, then it is being separately parsed and executed[/QUOTE]

Wow ... good call.

I'm actually a little embarrassed to have gone off searching for a function-scope related issue now ...
Copy linkTweet thisAlerts:
@comptech520authorSep 13.2008 — Thank you for the reply!

Charles, I was trying to avoid [COLOR=#0000bb]$_SERVER[/COLOR][COLOR=#007700][[/COLOR][COLOR=#dd0000]'DOCUMENT_ROOT'[/COLOR][COLOR=#007700]] because if I have an index.php file in my root directory and it was going to access navigation.php from the includes directory that would work.[/COLOR]

[COLOR=#007700][/COLOR]

[COLOR=#007700]I was thinking that if the includes directory is in the root, and I was calling [COLOR=#0000bb]$_
SERVER[/COLOR][COLOR=#007700][[/COLOR][COLOR=#dd0000]'DOCUMENT_ROOT'[/COLOR][COLOR=#007700]] in a subdomain, it would think that the includes directory is in the subdomain directory, which it wouldnt be there.[/COLOR][/COLOR]

[COLOR=#007700][/COLOR]

[COLOR=#007700]I would up using the server path /var/www/yada yada blah blah and that did the trick, it sort of made sense actually![/COLOR]

[COLOR=#007700][/COLOR]

[COLOR=#007700]Thanks again![/COLOR]
×

Success!

Help @comptech520 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...