473,233 Members | 1,470 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,233 software developers and data experts.

how can i store global variables for whole web site

hi

vishal here. can anyone help me that how can i store the global
variables for my web site. suppose i want to store the no. of users
visited my site then what strategy should i use ?????

can i implement some lock mechanism so that this global variable is not
accessed by more than one application at some time which may create
some confusion.

thanks for your reply in advance.............

Jul 17 '05 #1
6 2942
It sounds like you want to store persistent data (i.e. it exists
between sessions and/or server down-time).

One solution is to put your variables in a file and just read/rewrite
the file as required.

Another solution is to have a table of key/value pairs in a database
and update them as required.

Another solution is to use the $_SESSION object if you don't need
things to be persistent.

Jul 17 '05 #2
thxs for information but what i want is that i want to store the
information like how many users visited site. so global variable which
is accessible by all forms and is there any mechanism to lock this
variable so at a time only one application can access this variable.
once the lock is unlocked then other application can access it.

Jul 17 '05 #3
vishal <vi************@yahoo.co.in> wrote:
thxs for information but what i want is that i want to store the
information like how many users visited site. so global variable which
is accessible by all forms and is there any mechanism to lock this
variable so at a time only one application can access this variable.
once the lock is unlocked then other application can access it.


Why not simply use a database for this (it's pretty good at counting :)?

http://nl2.php.net/manual/en/ref.sem.php might be what you are looking
for.

Jul 17 '05 #4
Daniel Tryba wrote:
vishal <vi************@yahoo.co.in> wrote:
thxs for information but what i want is that i want to store the
information like how many users visited site. so global variable which
is accessible by all forms and is there any mechanism to lock this
variable so at a time only one application can access this variable.
once the lock is unlocked then other application can access it.

Why not simply use a database for this (it's pretty good at counting :)?

http://nl2.php.net/manual/en/ref.sem.php might be what you are looking
for.

<g>
Jul 17 '05 #5
In: <11**********************@g14g2000cwa.googlegroups .com>, "vishal" <vi************@yahoo.co.in> wrote:
can i implement some lock mechanism so that this global variable is not
accessed by more than one application at some time which may create
some confusion.


One of the things I regard as usually an asset, but sometimes a weakness is that
there is no way to store global, application wide variables the way you can
in java servlets. Part of this is the way php programs are parsed and run
each time the page is loaded. Variables don't hang around after. The other part
is the way fork() works.

With mod_perl you can "sort of" share variables, but they only apply to the current
process. (For this reason, synchronization is not an issue)

On a UNIX platform, have a look at:

http://us4.php.net/manual/en/ref.sem.php
http://us4.php.net/shmop

As far as I know, (hopefully someone will correct me if I'm wrong) this is the
only way to share _memory_.

I've used shared memory in other applications (other languages) and
generally recommend against it if at all possible. It can lead to a
royal nuisance when it's time to free them. Maybe things have changed,
maybe I was doing something wrong, but the shared memory was "leaked"
when the program was terminated abruptly.

Consider using a database, file, or even the serialize stuff for persistant
storage, it's slower but unless you require the speed, it's safer.

For obvious reasons, shared memory doesn't work across servers, so, if your
page is ever served via multiple web servers, (load balancing, etc..) it
won't work.

Jamie
--
http://www.geniegate.com Custom web programming
gu******@lnubb.pbz (rot13) User Management Solutions
Jul 17 '05 #6
vishal wrote:
thxs for information but what i want is that i want to store the
information like how many users visited site. so global variable which is accessible by all forms and is there any mechanism to lock this
variable so at a time only one application can access this variable.
once the lock is unlocked then other application can access it.


DB may be the right choice. Or may try something like:
<?php
$filename = 'globals.txt';
$fp = fopen($filename, 'rb+');
flock($fp, LOCK_EX); // exclusive lock
$contents = fread($fp, filesize($filename)+1);
$MYGLOBALS = unserialize($contents);
print_r($MYGLOBALS); //test
$MYGLOBALS['count'] += 1;
$MYGLOBALS['foo'] += 5;
print_r($MYGLOBALS); //test
rewind($fp);
fwrite($fp, serialize($MYGLOBALS));
flock($fp, LOCK_UN); //release lock
fclose($fp);
?>

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

Jul 17 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Jonathan | last post by:
Thanks for the help and commentary on sessions. I had another question or two on a related topic. I installed a newer version of php and went in to set it to read global variables, which was...
20
by: 2obvious | last post by:
I've been trying to create read-only global variables by creating constants (Const) in my global.asa, but I can't seem to reference them. Sticking them in an include works fine, but it seems more...
8
by: jose luis fernandez diaz | last post by:
Hi, I am reading Stroustrup's book 'C++ Programming Language'. In the 10.4.9 section (Nonlocal Store) he says: "A variable defined outside any function (that is global, namespace, and class...
5
by: j | last post by:
Anyone here feel that "global variables" is misleading for variables whose scope is file scope? "global" seems to imply global visibility, while this isn't true for variables whose scope is file...
7
by: Michael | last post by:
Hi newsgroup, as the subject indicates I am looking for an advice using global variables. I am not if this problem is more about style then C. If its wrong in thi group, sorry. So I have a...
4
by: BB | last post by:
Hello all, I might be missing something here, but am trying to understand the difference between using application-level variables--i.e. Application("MyVar")--and global variables--i.e. public...
4
by: Marc E | last post by:
All, I'm coming from java and coldfusion, where one can set a "global" variable in one place (the servletcontext in java, Application.cfm in coldfusion) and all files in that site can then take...
15
by: =?Utf-8?B?UGF0Qg==?= | last post by:
Just starting to move to ASP.NET 2.0 and having trouble with the Global.asax code file. In 1.1 I could have a code behind file for the global.asax file. This allow for shared variables of the...
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: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.