473,837 Members | 1,705 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2962
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************ **********@g14g 2000cwa.googleg roups.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($filen ame)+1);
$MYGLOBALS = unserialize($co ntents);
print_r($MYGLOB ALS); //test
$MYGLOBALS['count'] += 1;
$MYGLOBALS['foo'] += 5;
print_r($MYGLOB ALS); //test
rewind($fp);
fwrite($fp, serialize($MYGL OBALS));
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
3579
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 originally set to 'off' when I installed it. The notes in the php.ini file said that it's less secure to allow reading them. What kind of security risks would be associated with this? For example, in my site, I just want to be able to read the...
20
4508
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 structurally sound to use Application_OnStart. Am I attempting the impossible, and if so, why?
8
2539
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 static variables) is initializated (constructed) before main is invoked . . ." .. . .
5
3500
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 scope. If you have a variable whose scope is file scope in another translation unit, you have to provide a local declaration to access that variable from the other translation unit. Also, I don't see "global variable" used once in the standard....
7
3150
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 couple of function that all need the same information (all located in the same file). By now it looks like /* file beginns */
4
3539
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 myVar as string, etc. It seems to me that the scope and duration are the same, as they both are there while the application is running, and both go away when it quits. I presume that one difference is that the application state can be "flushed," such...
4
3002
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 advantage of these variables without the need to "include" a variables page in every page on the site. Is there something comparable in php, like an Application.php or some such thing? One more question that's basically an extension of the first....
15
2583
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 Global class. Note: I use these shared variables for read only values that are set at application start. It would appear the 2.0 doesn't like you to use shared variables in the global class. How do I convert my 1.1 applications to 2.0 without...
1
29409
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 called it polluting the global namespace. This article explores what happens when the global namespace becomes polluted and how to avoid this condition. The opinions expressed in this article are those of the author alone although many have...
0
9693
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10899
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10583
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9419
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7824
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5862
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4481
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4058
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3128
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.