LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-20-2011, 02:16 AM   #1
pawelch
LQ Newbie
 
Registered: Jul 2010
Posts: 26

Rep: Reputation: 0
[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!
 
Old 05-20-2011, 02:26 AM   #2
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 447Reputation: 447Reputation: 447Reputation: 447Reputation: 447
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.

Last edited by Guttorm; 05-20-2011 at 02:28 AM.
 
Old 05-20-2011, 04:13 AM   #3
pawelch
LQ Newbie
 
Registered: Jul 2010
Posts: 26

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Guttorm View Post
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.

Last edited by pawelch; 05-20-2011 at 04:36 AM.
 
Old 05-20-2011, 05:00 AM   #4
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 447Reputation: 447Reputation: 447Reputation: 447Reputation: 447
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Server array (mysql, php) -> gboolean array in c client kalleanka Programming 1 07-27-2010 06:50 AM
PHP: Converting a PHP array into an HTML array koosha Programming 4 08-11-2009 08:47 AM
create a global associative array variable inside a function of a bash script konsolebox Programming 3 07-14-2009 06:08 AM
php UG for global betterworld07 Linux User Groups (LUG) 0 04-02-2006 06:11 PM
global variables in php champ Programming 2 10-13-2002 01:31 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:20 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration