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

User-defined Globals?

P: n/a
Having come to PHP from another CGI, there's one thing I really miss...

The previous tool used was Witango, which had one major difference: the
interpreter ran as a service/daemon. One benefit of this was, you could
have variables scoped at a number of different levels above the
once-only variables in PHP (i.e. sessions).

Specifically, it was possible to create variables at a system-wide and
site-wide level. I could set a value and it would be available to any
app run within that site -- a classic example would be a variable
containing the title of the site.

Can I replicate this functionality in PHP?

I'd guess that the norm in PHP is to re-read this kind of information
from a DB with *every* hit; a huge overhead.

Can PHP be run in a more "persistant" manner?

Thanks,
Jon
Jul 17 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Jon Grieve wrote:
Having come to PHP from another CGI, there's one thing I really miss...

The previous tool used was Witango, which had one major difference: the
interpreter ran as a service/daemon. One benefit of this was, you could
have variables scoped at a number of different levels above the
once-only variables in PHP (i.e. sessions).

Specifically, it was possible to create variables at a system-wide and
site-wide level. I could set a value and it would be available to any
app run within that site -- a classic example would be a variable
containing the title of the site.

Can I replicate this functionality in PHP?

I'd guess that the norm in PHP is to re-read this kind of information
from a DB with *every* hit; a huge overhead.

Can PHP be run in a more "persistant" manner?

Thanks,
Jon


Hi Jon,

Yes, a database can be used, but that indeed creates some overhead.

I always include a file that contains 'global' variables, and typically put
it just outside the public_html.
Very easy.
But if you want to change these values, you will have to open and change the
file of course, but that is also very easy in PHP.

I am unaware of a 'real' global solution, but that doesn't mean there isn't
one. ;-)

Regards,
Erwin Moller
Jul 17 '05 #2

P: n/a
Jon Grieve wrote:
Having come to PHP from another CGI, there's one thing I really miss...

The previous tool used was Witango, which had one major difference: the
interpreter ran as a service/daemon. One benefit of this was, you could
have variables scoped at a number of different levels above the
once-only variables in PHP (i.e. sessions).

Specifically, it was possible to create variables at a system-wide and
site-wide level. I could set a value and it would be available to any
app run within that site -- a classic example would be a variable
containing the title of the site.

Can I replicate this functionality in PHP?

I'd guess that the norm in PHP is to re-read this kind of information
from a DB with *every* hit; a huge overhead.

Can PHP be run in a more "persistant" manner?


You could write your own session handler. The default one simply
serialises $_SESSION and saves it in a plain file. I've seen a
discussion somewhere about using a session server instead. At this point
it would be relatively easy to maintain $_SESSION['global'] and
$_SESSION['local'] separately.

It does beg the question though as to why you need site/application
global variables. Constants are easily supported using a config file.
I've never come across a requirement for global variables, only session
specific ones.
Jul 17 '05 #3

P: n/a
Kevin Thorpe wrote:
Jon Grieve wrote:
Having come to PHP from another CGI, there's one thing I really miss...

The previous tool used was Witango, which had one major difference:
the interpreter ran as a service/daemon. One benefit of this was, you
could have variables scoped at a number of different levels above the
once-only variables in PHP (i.e. sessions).

Specifically, it was possible to create variables at a system-wide and
site-wide level. I could set a value and it would be available to any
app run within that site -- a classic example would be a variable
containing the title of the site.

Can I replicate this functionality in PHP?

I'd guess that the norm in PHP is to re-read this kind of information
from a DB with *every* hit; a huge overhead.

Can PHP be run in a more "persistant" manner?

You could write your own session handler. The default one simply
serialises $_SESSION and saves it in a plain file. I've seen a
discussion somewhere about using a session server instead. At this point
it would be relatively easy to maintain $_SESSION['global'] and
$_SESSION['local'] separately.

It does beg the question though as to why you need site/application
global variables. Constants are easily supported using a config file.
I've never come across a requirement for global variables, only session
specific ones.


A couple of examples would be...

I have some dynamic site, where just about everything to do with layout
and presentation is dynamic (title, width, colours, etc., etc.). From
what I've seen so far, a common way to handle this would be to maybe
include() a file that defines and initialises all these values. This
might be from a DB or simply a static set of consts. So, every PHP app
includes this massive overhead.

Now, of course, you should handle not having to do that for every single
page view - presumably by using session variables - but you still have
to do it every time a new session is started.

What I'm used to is more like this (pseudo code):

if (!$_DOMAIN['initialised'])
{
\\ hit the hugely time-consuming db to load
\\ hundreds of globals
$_DOMAIN['initialised'] = TRUE;
}

echo "<head><title>".$_DOMAIN['title']."</title></head>";

The main point here is, the special site-wide variables will have been
initialised by the first person to hit the site, and will persist for
*every other* session forever - not just a single "visitor".
Another example I've used this for in the past would be to maintain a
hit counter. Obviously, it's a massive overhead to 1) Read current
count from DB; 2) Increment by 1; 3) Write back to DB - for every hit on
every page. What I've done in the past is simply $_DOMAIN['counter']++.
I've then used a scheduled/cron task to fire every "n" minutes that
commits the current value to the DB -- vastly reduced overhead. Sure,
if the server blew up, I might lose "n-1" minutes with of hits from the
DB value, but in this particular example, it wasn't vastly important.
As I said in the original post, the fundamental difference between
Witango and PHP is that Witango runs as a service. Now, it may well be
that this is partly due to the fact it has to maintain an always-running
element to maintain such variables, and if PHP is never going to head in
this direction, then this is all irrelevant.

My point all along has been, this was a real killer feature of Witango,
and I miss it... ;)

So, assuming there's no plan to duplicate Witango's functionality, are
there any elegant solutions to emulate this?

Jon
Jul 17 '05 #4

P: n/a
Jon Grieve wrote:
Specifically, it was possible to create variables at a system-wide and
site-wide level. I could set a value and it would be available to any
app run within that site -- a classic example would be a variable
containing the title of the site.
One option is to use Apache's "SetEnv" in your httpd.conf or .htaccess
Can I replicate this functionality in PHP?

I'd guess that the norm in PHP is to re-read this kind of information
from a DB with *every* hit; a huge overhead.

Can PHP be run in a more "persistant" manner?


Short of using sessions or cookies, the only other options I see for
this is to use a config file (http://us4.php.net/parse_ini_file) or
include/require a file with everything defined in it.

--
Justin Koivisto - sp**@koivi.com
PHP POSTERS: Please use comp.lang.php for PHP related questions,
alt.php* groups are not recommended.
Jul 17 '05 #5

P: n/a
Jon Grieve wrote:
I have some dynamic site, where just about everything to do with layout
and presentation is dynamic (title, width, colours, etc., etc.). From
what I've seen so far, a common way to handle this would be to maybe
include() a file that defines and initialises all these values. This
might be from a DB or simply a static set of consts. So, every PHP app
includes this massive overhead.

Now, of course, you should handle not having to do that for every single
page view - presumably by using session variables - but you still have
to do it every time a new session is started.

What I'm used to is more like this (pseudo code):

if (!$_DOMAIN['initialised'])
{
\\ hit the hugely time-consuming db to load
\\ hundreds of globals
$_DOMAIN['initialised'] = TRUE;
}

echo "<head><title>".$_DOMAIN['title']."</title></head>";
Why would you initialize anything that you won't use for that particular
request? If you break up your variables or constants into multiple ini
files, you only have the overhead needed to complete that request.
The main point here is, the special site-wide variables will have been
initialised by the first person to hit the site, and will persist for
*every other* session forever - not just a single "visitor".
I don't think this is possible (at least I haven't heard of it).
Basically, HTTP is a stateless protocol, so unless you are using a
database, files, or _SESSION, even a single "visitor" won't be able to
save the information from request to request.
So, assuming there's no plan to duplicate Witango's functionality, are
there any elegant solutions to emulate this?


No, not that I see. However, you could likely create your own solution
to this in the form of some kind of PHP extension.

--
Justin Koivisto - sp**@koivi.com
PHP POSTERS: Please use comp.lang.php for PHP related questions,
alt.php* groups are not recommended.
Jul 17 '05 #6

P: n/a

"Jon Grieve" <jgrieve@southdown-co-uk> wrote in message
news:10************@corp.supernews.com...
Kevin Thorpe wrote:
Jon Grieve wrote:
Having come to PHP from another CGI, there's one thing I really miss...

The previous tool used was Witango, which had one major difference:
the interpreter ran as a service/daemon. One benefit of this was, you
could have variables scoped at a number of different levels above the
once-only variables in PHP (i.e. sessions).

Specifically, it was possible to create variables at a system-wide and
site-wide level. I could set a value and it would be available to any
app run within that site -- a classic example would be a variable
containing the title of the site.

Can I replicate this functionality in PHP?

I'd guess that the norm in PHP is to re-read this kind of information
from a DB with *every* hit; a huge overhead.

Can PHP be run in a more "persistant" manner?

You could write your own session handler. The default one simply
serialises $_SESSION and saves it in a plain file. I've seen a
discussion somewhere about using a session server instead. At this point
it would be relatively easy to maintain $_SESSION['global'] and
$_SESSION['local'] separately.

It does beg the question though as to why you need site/application
global variables. Constants are easily supported using a config file.
I've never come across a requirement for global variables, only session
specific ones.


A couple of examples would be...

I have some dynamic site, where just about everything to do with layout
and presentation is dynamic (title, width, colours, etc., etc.). From
what I've seen so far, a common way to handle this would be to maybe
include() a file that defines and initialises all these values. This
might be from a DB or simply a static set of consts. So, every PHP app
includes this massive overhead.


*massive* is a relative term. Many sites load static values from a config
file with every page, but it is usually up to the underlying file system to
hold this in cache memory so as to avoid a disk read each time. With modern
servers being so fast you should not notice any discernable delay.

--
Tony Marston

http://www.tonymarston.net

Jul 17 '05 #7

P: n/a
"Jon Grieve" <jgrieve@southdown-co-uk> wrote in message
news:10*************@corp.supernews.com...
Having come to PHP from another CGI, there's one thing I really miss...

The previous tool used was Witango, which had one major difference: the
interpreter ran as a service/daemon. One benefit of this was, you could
have variables scoped at a number of different levels above the
once-only variables in PHP (i.e. sessions).

Specifically, it was possible to create variables at a system-wide and
site-wide level. I could set a value and it would be available to any
app run within that site -- a classic example would be a variable
containing the title of the site.

Can I replicate this functionality in PHP?


Use a temporary file to store the system wide variables.

To load these, just acquire a shared lock, read the content, then
unserialize it back to the array:

$f = fopen('../sysvar', 'rb');
flock($f, LOCK_SH);
$SYSVARS = unserialize(fread($f, 1000000));
fclose($f);

To save them, acquire an exclusive lock, then write the serialized array to
the file:

$f = fopen('../sysvar', 'wb');
fwrite($f, serialize($SYSVARS));
fclose($f);

Basically you're doing what PHP does with session variable, except these
will be shared among different users.

Keep in mind that variables are supposed to be variable--i.e. they are meant
to change over time. Unless the title of your site change from day to day,
it should be stored in a variable. The preferred way of stored system
configuration info is the use of .ini files.
Jul 17 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.