473,385 Members | 2,210 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Variables From My Include File Are Being Ignored

I have been developing a system in PHP that uses MySQL. To that end, I have used a config file to hold high level fixed variables (database name, user id, password, etc) and I using "require_once" to get it into my code with no problems.

The problem arises from my desire to integrate third party software, such as Wordpress, External Calendar, etc, into my site and have one central location for database configuration.

I can successfully use "require_once" in WP's or X2's code but when I refer to variables set in my include file, they seem to be null, which is puzzling. This is a snippet from my include file:

[php]
<?

$gs_database = "linwoods_onlinedems";
$gs_username = "linwoods_admin";
$gs_password = "plush32331";
?>
[/php]

That's not all in the file but is the relevant part. I set about a dozen variables and I can successfully refer to them in my code. Here is an example from X2:

[php]
<?php
require_once ('http://'.$_SERVER['HTTP_HOST'].'/lcs_config.php');
// ExtCalendar configuration file

// DB configuration
$CONFIG['dbsystem'] = "mysql"; // Your database system
$CONFIG['dbserver'] = "localhost"; // Your database server
$CONFIG['dbuser'] = $gs_username; // Your db username
$CONFIG['dbpass'] = $gs_password; // Your db password
$CONFIG['dbname'] = $gs_database; // Your database name

echo "|".$gs_username."|";

?>

[/php]

It finds the file alright because if I change the file name, it breaks. The problem is that the variable I'm echoing is NULL. This is happening in three different external applications I'm trying to integrate so I'm stumped as to why this is happening. I've been on google for the last 2 hours with no success.

Any ideas?

Thanks!
Paul
Dec 22 '06 #1
5 1889
ronverdonk
4,258 Expert 4TB
Could be several reasons for this, one of them that these vars are not globally defined and are used in functions, from where they are not addressable.

Have you considered using definitions, i.e. constants. You could then define your setup along the following lines:
[php]
define('SQL_SYS', 'mysys');
define('SQL_HOST', 'myhost');
define('SQL_USER', 'myuser');
define('SQL_PASS', 'mypassw');
define('SQL_DB', 'mydb');[/php]
and setup you CONFIG array as follows:
[php]
$CONFIG['dbsystem'] = SQL_SYS;
$CONFIG['dbserver'] = SQL_HOST;
$CONFIG['dbuser'] = SQL_USER;
$CONFIG['dbpass'] = SQL_PASS;
$CONFIG['dbname'] = SQL_DB;
[/php]

Ronald :cool:
Dec 22 '06 #2
I changed to definitions with no success. I found that you can't register definitions as globals so that didn't work and I went back to variables and defined them as global in my include file.

That also didn't work.

It's not a show stopper, but it's frustrating that I have to define my database in 4-5 places and once I install this application in multiple places, I'll have to have a checklist to configure the system.



Could be several reasons for this, one of them that these vars are not globally defined and are used in functions, from where they are not addressable.

Have you considered using definitions, i.e. constants. You could then define your setup along the following lines:
[php]
define('SQL_SYS', 'mysys');
define('SQL_HOST', 'myhost');
define('SQL_USER', 'myuser');
define('SQL_PASS', 'mypassw');
define('SQL_DB', 'mydb');[/php]
and setup you CONFIG array as follows:
[php]
$CONFIG['dbsystem'] = SQL_SYS;
$CONFIG['dbserver'] = SQL_HOST;
$CONFIG['dbuser'] = SQL_USER;
$CONFIG['dbpass'] = SQL_PASS;
$CONFIG['dbname'] = SQL_DB;
[/php]

Ronald :cool:
Dec 22 '06 #3
ronverdonk
4,258 Expert 4TB
When the $_SESSION array is available in all scripts you could use that. At least they are global.
But I still am, like you, also puzzled why the variables should be NULL.

Ronald :cool:
Dec 22 '06 #4
Solved!

After a few (more) hours of experimentation, and by a lucky chance, I found the solution to this problem.

I was using [php]require_once('http://'.$_SERVER['HTTP_HOST'].'/lcs_config.php');[/php] in my script and it was pointing properly to the correct location. I knew this not only by displaying the value, but when I deliberately misspelled the filename it failed.

What does work is:

[php]require_once('../lcs_config.php');[/php]

This MUST be some sort of bug in PHP, but as long as it works and I don't spend anymore time I don't have on this, that's fine with me.
Dec 22 '06 #5
ronverdonk
4,258 Expert 4TB
I am glad you found the solution to your problem yourself.

Ronald :cool:
Dec 22 '06 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

23
by: Mark Parnell | last post by:
I'm relatively new to PHP, and have just converted a site from ASP to PHP. There is one thing I haven't managed to do, though. When the site was using ASP, I had one file (called variables.asp),...
0
by: Tom Lee | last post by:
Hi, I'm new to .NET 2003 compiler. When I tried to compile my program using DEBUG mode, I got the following errors in the C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7 \include\xdebug...
15
by: Mon | last post by:
I am in the process of reorganizing my code and came across and I came across a problem, as described in the subject line of this posting. I have many classes that have instances of other classes...
6
by: ravi_shankar | last post by:
hi all. what's the advantage of prefixing a varible with "extern" storage class specifier other than its default initializion . you can mail to me : ravi_shankarprasad@rediffmail.com --...
13
by: Sunil | last post by:
Hi all, I want to know how good or bad it is using global variables i.e advantages and disadvantages of using global variables. Sunil.
0
by: Milos Puchta via .NET 247 | last post by:
(Type your message here) -------------------------------- From: Milos Puchta We have problem with the building Qt examples as follows from the fragment of log file. Thanks for your...
111
by: Nate | last post by:
Hello, I am looking for a method to automatically declare variables in C. I'm not sure if there is a good way to do this, but I had something like this in mind... int i; for(i = 1; i < 4;...
18
by: gutmant | last post by:
Say you have a file, a.h with an include guard. If you include it twice and look at the preprocessed output, you see there's no sign for the second inclusion. However, if you include it twice -...
1
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.