Jump to content

Global Variable/Value for Multiple Visitors


Baez

Recommended Posts

I'm trying to figure out if there's a way to set a global variable that will be available to all users on one page without accessing a database.

 

Example:

 

1. I create a variable called $num_users.

2. User #1 comes to the page and $num_users is then set to 1.

3. User #2 then comes to the page and $num_users is now set to 2 for both User #1 (after a page refresh obviously) and User #2.

 

Can global variables or any other method do this?

 

Thanks :)

 

Link to comment
Share on other sites

Two things, you need a space for you to persist your data, the number count on how many people visit this page.

If you don't wished to use database, you can use a text file. Check out php's file functions.

 

Second thing is the increment of the number, make sure you use cookie or session or other method to keep track that this user are only allowed to increase the number by 1 in the text file for first time only.

 

There are no such variable in php can persist your value and share with other client.

Link to comment
Share on other sites

Thanks for the reply. It's not that I don't know how to use databases. It's more the fact that the SQL server is working hard enough already and this will really push it.

 

But I guess it's the only option if there's no other way to do it.

 

Thanks again.

Link to comment
Share on other sites

Thanks for the reply. It's not that I don't know how to use databases. It's more the fact that the SQL server is working hard enough already and this will really push it.

 

But I guess it's the only option if there's no other way to do it.

 

Thanks again.

 

From your statement I assume you're fairly new to database based programming, I doubt you're pushing it to its 1/1000th load limit per page load, especially on such an incredibly simple increment on a row.

Link to comment
Share on other sites

No I'm not new at all. That was simply an example. The table will be holding the number of users as well as up to 17 usernames. At any given time anywhere from 100-250 of these rows are being created, updated or deleted. While those rows are being updated another table will be read from to retrieve additional information.

 

How taxing do you think that description would be? I'm not sure how to really estimate it. The server running it is decent but not overly powerful.

Link to comment
Share on other sites

If you're going to be using an alternative with fopen (Using the w+ flag) You can easily iterate the number (Or log something useful to handle expireys) and display it with the complementing functions. But if you're wanting to compare perf. aspects on the writes, They are negligablly similar. A file read/write and a compiled program will handle it in a negligable speed, but if you're wanting to save resources (Assuming your queries were actually too taxing) by any means fopen would be an suitable option.

 

I'm just not sure why you're wanting a "global" persistance of something alternate to what you have already.

Link to comment
Share on other sites

Hmm I might take that route then. Thanks oni.

 

It's more the fact that the server has ample memory and not as much processing power. So I'm trying to keep running processes to a minimum while making the most use of memory. Hence why I thought of using global variables to store everything and only using the processor to set the values from already existing $_SESSION values.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.