Want to know Application Variable in PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nic9
Forum Newbie
Posts: 5
Joined: Sun Sep 21, 2003 4:12 am

Want to know Application Variable in PHP

Post by nic9 »

I'd like to create a global variable (in PHP) to store the value of a submitted field so that (the global variable) is available to all the visitors of my site. Its something like "Application Variable" in ASP, but I wonder how to do it in PHP.

I don't want to use database.

I know that in ASP we use Application Variable whch is Global, it means it stores the data and make it available to all the visitors who visits the site. And preserve the data(variables) until the system reboots. Has anyone have an idea how to do it in PHP.

I want to know how can we use Application Variable in PHP to store the data which is gobally accessible to all the visitors. And please remember I don't want to use Database, so please don't give a suggestion of using database.

Thanks.
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

If you don't want to use a database, then store it in a file.
jaxn
Forum Commoner
Posts: 55
Joined: Fri Jan 16, 2004 1:50 pm
Location: Nashville, TN

Post by jaxn »

If you are using Apache then you could try to set it as a an environment variable.

But then I think it would be set for all sites being hosted by that server, so it is probably/hopefully impossible in all shared hosting environments.

-Jackson
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post by BDKR »

Another option is to store it in memory. The Turck MMCache accellerator provides an API for storing data in memory. It's a good deal faster than getting the info from off a disk or out of a database.

However, what do you do when the site starts growing and you find yourself needing a web farm? Unless someone comes out with a method of storing application vars (memory or file based) consistently across a load balanced cluster, a database is your best bet.

Cheers,
BDKR
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post by BDKR »

Just thought I'd post the API...



Turck MMCache API

mmcache_put($key, $value, $ttl=0)
puts the $value into shard memory for $ttl seconds.

mmcache_get($key)
returns the value from shared memory which was stored by mmcache_put() or null if it is not exists or was expired.

mmcache_rm($key)
removres the $key from shared memory

mmcache_gc()
removes all expired keys from shared memory

mmcache_lock($lock)
creates a lock with specified name. The lock can be released by function mmcache_unlock() or automatic on the end of request. For Example:

<?php
mmcache_lock("count");
mmcache_put("count",mmcache_get("count")+1));
?>

mmcache_unlock($lock)
release lock with specified name

The site is
http://turck-mmcache.sourceforge.net/

Cheers,
BDKR
Post Reply