By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,640 Members | 1,557 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,640 IT Pros & Developers. It's quick & easy.

include, globals and classes all in one?

P: n/a
Hi,

here's the deal:
I have a config.php file in which I have set a few var's, to use in the
whole site. E.g.:

$db_host = "localhost";

Then I also have a class, which is supposed to handle all database
functions. In this class I try to recover the var's from my config file,
but this just doesn't seem to work. I tried to put the following
statements in the constructor as well as in a plain function, but the
output of the var is always empty.

include("config.php");
global $db_host;
echo "value of db_host: ".$db_host;

Now, I have a slight suspicion that it might have to do with the fact
that I'm using classes, but I don't know what's wrong. Normally I should
be able to use the values from the config.php file, right?

greetz
<T!M> aka wEEdpEckEr
Jul 17 '05 #1
Share this Question
Share on Google+
15 Replies


P: n/a

"wEEdpEckEr" <mi**********@pandora.be> wrote in message
news:p2**********************@hestia.telenet-ops.be...
Hi,

here's the deal:
I have a config.php file in which I have set a few var's, to use in the
whole site. E.g.:

$db_host = "localhost";

Then I also have a class, which is supposed to handle all database
functions. In this class I try to recover the var's from my config file,
but this just doesn't seem to work. I tried to put the following
statements in the constructor as well as in a plain function, but the
output of the var is always empty.

include("config.php");
global $db_host;
echo "value of db_host: ".$db_host;

Now, I have a slight suspicion that it might have to do with the fact
that I'm using classes, but I don't know what's wrong. Normally I should
be able to use the values from the config.php file, right?

greetz
<T!M> aka wEEdpEckEr


Doing that works just fine with a simple example for me. Try
error_reporting(E_ALL) and make sure the config file gets found, etc.

Garp
Jul 17 '05 #2

P: n/a
Garp wrote:
"wEEdpEckEr" <mi**********@pandora.be> wrote in message
news:p2**********************@hestia.telenet-ops.be...
Hi,

here's the deal:
I have a config.php file in which I have set a few var's, to use in the
whole site. E.g.:

$db_host = "localhost";

Then I also have a class, which is supposed to handle all database
functions. In this class I try to recover the var's from my config file,
but this just doesn't seem to work. I tried to put the following
statements in the constructor as well as in a plain function, but the
output of the var is always empty.

include("config.php");
global $db_host;
echo "value of db_host: ".$db_host;

Now, I have a slight suspicion that it might have to do with the fact
that I'm using classes, but I don't know what's wrong. Normally I should
be able to use the values from the config.php file, right?

greetz
<T!M> aka wEEdpEckEr

Doing that works just fine with a simple example for me. Try
error_reporting(E_ALL) and make sure the config file gets found, etc.

Garp


I generally use th following to define globals, nfi if it is really a
correct way to do it tho.

define("glb_MY_GLOBAL", "myglobalvalue");

Access is the same as you have by doing:

global glb_MY_GLOBAL;
echo glb_MY_GLOBAL;

-Steve
Jul 17 '05 #3

P: n/a
With total disregard for any kind of safety measures Stephen
Gordon <s4******@student.uq.edu.au> leapt forth and uttered:
define("glb_MY_GLOBAL", "myglobalvalue");

Access is the same as you have by doing:

global glb_MY_GLOBAL;
echo glb_MY_GLOBAL;


You don't have to globalize constants. They are already accessible
globally.

--
Phil Roberts | Dork Pretending To Be Hard | http://www.flatnet.net/
Jul 17 '05 #4

P: n/a
Stephen Gordon <s4******@student.uq.edu.au> wrote in news:c4o1ca$38k$1
@bunyip.cc.uq.edu.au:
Doing that works just fine with a simple example for me. Try
error_reporting(E_ALL) and make sure the config file gets found, etc.

Here it doesn't... but I must admit, it's in a class (db_administrator)
of wich instances are used in another class (visitor), and there ara
quite a few include statements involved.
I generally use th following to define globals, nfi if it is really a
correct way to do it tho.

define("glb_MY_GLOBAL", "myglobalvalue");


Indeed, this seems to work better. Good thing that their values don't
have to change. ;-)

Thanx both for your replies.

Maybe just another question: I used an instance of Visitor to store in a
_SESSION var, so _before_ I call session_start(), I'm obliged to include
the class-definition of Visitor. But when I do this, I get an error
Warning: session_start(): Cannot send session cookie - headers already
sent by (output started at XXX/dbadmin.class.php:80) in XXX/register.php
on line 4

Now, dbadmin.class.php is ofcourse the class definition wich is called
upon by visitor.class.php, but No where in the file I can find anything
that would have 'sent headers', no output at all is used in that class,
only calls to the database. The include to visitor.class.php is on line
3 and start_session() on line 4 in register.php. Beneath is included the
code of dbadmin.class.php, anyone of you know why this file would have
sent headers before any instances of it are used? I have commented the
"or die" statements that maybe could cause any output, but that won't
help.

TIA
Tim

***dbadmin.class.php***
<?

class DatabaseAdministrator {

function DatabaseAdministrator() {
require("config.php");
}

function open_db () {
global $link;
$link = mysql_connect(db_host, db_login, db_passw); //or die ("Can't
connect to host: db_host!\n");
mysql_select_db(db_name); // or die ("Can't select database
db_name: $!\n");
}

function close_db () {
global $link;
mysql_close($link);
}

function add_hit() {

}

function add_visit($v_ip, $v_provider, $v_country, $v_date,
$v_starttime, $v_browser, $v_os, $v_referer) {

}

function get_country($ip_addr) {
$this->open_db();
$table_name = db_table_ipcount;
$query = "SELECT COUNTRY_NAME FROM $table_name WHERE ip_from <=
$ip_addr AND ip_to >= $ip_addr";
$result = mysql_query($query);
$row = mysql_fetch_array($result);
$this->close_db();
return $row["COUNTRY_NAME"];
}
}
?>
Jul 17 '05 #5

P: n/a

"wEEdpEckEr" <mi**********@pandora.be> wrote in message
news:df*********************@hebe.telenet-ops.be...
<snip>
(output started at XXX/dbadmin.class.php:80)

<snip>

What's on this line? If it's not something you expect output from, stick a @
in front of it, but it's likely it's doing something, possibly a header
output of its own.

Garp
Jul 17 '05 #6

P: n/a
"Garp" <ga***@no7.blueyonder.co.uk> wrote in
news:3w*********************@news-text.cableinet.net:

"wEEdpEckEr" <mi**********@pandora.be> wrote in message
news:df*********************@hebe.telenet-ops.be...
<snip>
(output started at XXX/dbadmin.class.php:80)

<snip>

What's on this line? If it's not something you expect output from,
stick a @ in front of it, but it's likely it's doing something,
possibly a header output of its own.


Well, I left out a commented part, but apart from that, everything is in
the previous post. The line refers to the last line of my class, "}". So
it's not doing anything at all. And as you can see (code in previous
message), there's not any output at all. Weird, no?
Jul 17 '05 #7

P: n/a
"Garp" <ga***@no7.blueyonder.co.uk> schreef op zo, 04 apr 2004 11:41:51
GMT in news:3w*********************@news-text.cableinet.net:
<snip>
(output started at XXX/dbadmin.class.php:80)

<snip>

What's on this line? If it's not something you expect output from,
stick a @ in front of it, but it's likely it's doing something,
possibly a header output of its own.


Ok, looks like I found it: when using an include, the included page,
shouldn't have <?php and ?> tags. The opening php tag will be ignored,
because php is already opened, but the closing one will be used
therefore terminating the php-code, even if it isn't supposed to do so.
Stupid, when you don't know this. I thought all code had to be captured
between these php-tags.

greetz
<T!M> aka wEEdpEckEr
Jul 17 '05 #8

P: n/a
wEEdpEckEr wrote:
Ok, looks like I found it: when using an include, the included page,
shouldn't have <?php and ?> tags.
It *must* have <?php and ?> tags if it is going to be interpreted
The opening php tag will be ignored, because php is already opened,
PHP "closes" right after the include() and before opening the included
file. If the included file has PHP inside it must be delimited by the
php tags.
but the closing one will be used
therefore terminating the php-code, even if it isn't supposed to do so.
Stupid, when you don't know this. I thought all code had to be captured
between these php-tags.


It has to be. You're doing some other thing wrong.

--
USENET would be a better place if everybody read: : mail address :
http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
http://www.expita.com/nomime.html : to 10K bytes :
Jul 17 '05 #9

P: n/a

"Pedro Graca" <he****@hotpop.com> wrote in message
news:c5*************@ID-203069.news.uni-berlin.de...
wEEdpEckEr wrote:
Ok, looks like I found it: when using an include, the included page,
shouldn't have <?php and ?> tags.


It *must* have <?php and ?> tags if it is going to be interpreted
The opening php tag will be ignored, because php is already opened,


PHP "closes" right after the include() and before opening the included
file. If the included file has PHP inside it must be delimited by the
php tags.
but the closing one will be used
therefore terminating the php-code, even if it isn't supposed to do so.
Stupid, when you don't know this. I thought all code had to be captured
between these php-tags.


It has to be. You're doing some other thing wrong.


He's right, wEEdpEckEr. All my code is built this way, something else is
amiss. Try to reproduce the problem in two smaller files, and post them here
if it still fails s so we can try ourselves?

Garp
Jul 17 '05 #10

P: n/a
"Garp" <ga***@no7.blueyonder.co.uk> schreef op do, 08 apr 2004 09:37:01
GMT in news:13*********************@news-text.cableinet.net:
It has to be. You're doing some other thing wrong.


He's right, wEEdpEckEr. All my code is built this way, something else
is amiss. Try to reproduce the problem in two smaller files, and post
them here if it still fails s so we can try ourselves?


Hmmm, ok, so I left out the closing tags, and that seemed to work, now
I've put them back, and again it seems to work fine. So it must've been
something completely different, that I've changed and solved without
knowing... Weird. ;-)

Btw: while I'm buggin you guys: getting the screen resolution is not
possible in php I guess? I've been searching the web, and I only can
seem to find references to javascripts...

greetz
Jul 17 '05 #11

P: n/a
With total disregard for any kind of safety measures wEEdpEckEr
<toch_lekker_nie@geen_email.aub> leapt forth and uttered:
Btw: while I'm buggin you guys: getting the screen resolution is
not possible in php I guess? I've been searching the web, and I
only can seem to find references to javascripts...

greetz


PHP = Server Side
Screen Res = Client side.

So no, it's not possible. Besides, screen resolution is a red herring
as it makes no allowance for /window/ size.

--
Phil Roberts | Dork Pretending To Be Hard | http://www.flatnet.net/
Jul 17 '05 #12

P: n/a
Phil Roberts <ph*****@HOLYflatnetSHIT.net> schreef op do, 08 apr 2004
23:48:33 GMT in news:Xn************************@216.196.97.132:
PHP = Server Side
Screen Res = Client side.
Well, that's your explaination :p Browsername and OS are also client
side and those can be easily checken within php. ;-)
So no, it's not possible. Besides, screen resolution is a red herring
as it makes no allowance for /window/ size.


No, but it 's handy to know, weather most of your visitors are still in
800*600 or have advanced to 1024*768. This is pure for stats, not to
change the outcome of the page.

greetz
<T!M> aka wEEdpEckEr
Jul 17 '05 #13

P: n/a
On Fri, 09 Apr 2004 00:00:10 +0000, wEEdpEckEr wrote:
PHP = Server Side
Screen Res = Client side.


Well, that's your explaination :p Browsername and OS are also client
side and those can be easily checken within php. ;-)

[ snip ]
Browser name is sent by the client to the server hence PHP can detect this
value. The OS is often derived from this very same value also.

Check the user agent string in your logs.. normally, they'll contain the
browser and the OS the browser is running on.. neither of which are
actually _detected_ client side =)

Regards,

Ian

--
Ian.H
digiServ Network
London, UK
http://digiserv.net/

Jul 17 '05 #14

P: n/a
"Ian.H" <ia*@WINDOZEdigiserv.net> schreef op vr, 09 apr 2004 00:17:02
GMT in news:pa****************************@bubbleboy.digi serv.net:
Browser name is sent by the client to the server hence PHP can detect
this value. The OS is often derived from this very same value also.

Check the user agent string in your logs.. normally, they'll contain
the browser and the OS the browser is running on.. neither of which
are actually _detected_ client side =)


Yes, I know, I wrote the regex's myself, but it could be that this is also
being send by the browser. Just like the referer-string, or something. This
is also not "detected", so it could have been possible that the resolution
was available too. Helas, it isn't... Too bad, since I can't use javascript
in my model.

greetz
Tim
Jul 17 '05 #15

P: n/a
On 2004-04-09, wEEdpEckEr <toch_lekker_nie@geen_email.aub> wrote:
Yes, I know, I wrote the regex's myself, but it could be that this is also
being send by the browser. Just like the referer-string, or something. This
is also not "detected", so it could have been possible that the resolution
was available too. Helas, it isn't... Too bad, since I can't use javascript
in my model.


Meaby you want to read the rfc on http1.1 to know what a browser can
send ;)

--
http://home.mysth.be/~timvw
Jul 17 '05 #16

This discussion thread is closed

Replies have been disabled for this discussion.