LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   [newbie][php] global array (https://www.linuxquestions.org/questions/programming-9/%5Bnewbie%5D%5Bphp%5D-global-array-881741/)

pawelch 05-20-2011 02:16 AM

[newbie][php] global array
 
Dear all,

I have a general question regarding php.
Suppose that I want to have an array that stores connection-specific data, that can be anything IP address or something similar. However, whenever I use global or server variables (i.e. GLOBALS or _SERVER) the array created in this way is kind of connection-oriented, so that for each connection there is a seperate instance of this array. Maybe it has something to do with the MPM of Apache, but I am not sure.

Is it possible to implement an array that stores information about all connections that are at a system? In this way, each connection can chip in and upload it with data? Also, I would rather not use cookie's..

Thank you for all your suggestions,

Cheers!

Guttorm 05-20-2011 02:26 AM

You should look at session variables:

http://www.php.net/manual/en/book.session.php

It normally uses a cookie to separate the sessions. But you can override it:

http://www.php.net/manual/en/session.idpassing.php

Edit:
If you want the data to be truly global (shared between all), you can store it a file or a database.

pawelch 05-20-2011 04:13 AM

Quote:

Originally Posted by Guttorm (Post 4361585)
Edit:
If you want the data to be truly global (shared between all), you can store it a file or a database.

What do you think about reading/writing to the same file by multiple connections ? Is it efficient given the server might be faced up against multiple reasonable fast connections?Therefore I thought, what if I stick it in a global array that is available for apache process and whenever one connections does something, I just update that array. RAM memory should (in general) be quicker than r/w file operations or database queries. That is why I thought about the global, connection independent array.

Guttorm 05-20-2011 05:00 AM

So it doesn't need to be persistent? I think you need to elaborate more on what you need. If you think files are slow, what about the PHP file itself? And fast connections are usually not the problem with the database. Slow connections are worse because they keep the connection open, and the database will get problems if there are too many open at once.

Anyway, if you don't use a database, you will need to think of locking and multiple updates happening at the same time. You could use semaphores or shared memory, but to me it smells like premature optimization.

http://www.php.net/manual/en/book.sem.php

http://www.php.net/manual/en/book.shmop.php


All times are GMT -5. The time now is 10:14 PM.