tags:

views:

74

answers:

8

This may be a silly question, but how do I save variables that are not specific to a particular session. An simple example of why you might want to do this would be a visitor counter - a number that increases by one each time someone visits a web page (note - I'm not actually doing that, my application is different, but that is the functionality I need). The only ways I can think of doing this are either writing the variables to a file, or putting the variables into a database. Both seem a bit inelegant. Is there a better way to to this kind of thing?

+5  A: 

If you need to save global state, you need to save global state. This is typically done in either a file or a database as you already noted.

Matthew Scharley
Hmmmm. In asp.net there is the notion of application state, which is basically an in memory cache which is shared by all reqests. This would be a perfect place to store the number of currently online users if you ask me.
klausbyskov
Problems: a) ASP is sadly not PHP. b) Application state is volatile. This may or may not be an issue. In the case of a hit counter, this would be a large issue. Unfortunately there is nothing that is a direct analog of application state in PHP, so the conversation is somewhat moot anyway.
Matthew Scharley
A: 

Given that PHP is stateless and that each pageload is essentially re-running your page anew, if you're going to be saving variables that will increment over multiple pageloads (e.g., number of distinct users), you'll have to use some form of server-end storage - file-based, database, whatever - to save the variable.

eykanal
+2  A: 

It's not "inelegant" at all. If you need to save something (semi-)permanently, you put it in a database. That's what databases are for.

msergeant
+1  A: 

Have a look at the serialize() function in PHP http://uk3.php.net/serialize where you'll be able to write an array or such to a file and re-retrieve:

<?php
  // Save contents
  $var = array('pageCounter' => 1);
  file_put_contents('counter.txt', serialize($var));

  // Retrieve it
  $var = unserialize(file_get_contents('counter.txt'));
?>

Otherwise save the value to a database.

Alistair
Yes, thanks I am already doing something very similar.
james.bcn
A: 

You could try installing APC (Alternative PHP Cache) which has cool features for sharing data between all PHP scripts, you could try using shared memory too or like you said, use a file or database

Serty Oan
Thats an interesting option thanks.
james.bcn
A: 

If you want it to be permanent, database and files are really your only two choices.

If you only want to temporarily store these values in memory, if APC is installed, you can do this:

// Fetch counter value back from memory
$success = false;
$counter = apc_fetch('counter', &$success);
if ($success) {
    // fetch succeeded
} else {
    // fetch failed
    $counter = 0;
}

// Increment the counter and store again
// Note that nothing stops another request/page from changing this value
// between the fetch and store calls.
$counter++;
apc_store('counter', $counter);

That was just an example. For a counter, you're better off using apc_inc('counter') / apc_dec('counter').

Presumably other opcode caches have similar methods. If you're not running an opcode cache... really? You want PHP to recompile a page every time its requested?

R. Bemrose
Great thanks. Very interesting. I'll consider that.
james.bcn
Sorry, I forgot to link to the manual pages for these pages. Here's the main APC functions manual page on php.net: http://www.php.net/manual/en/ref.apc.php
R. Bemrose
A: 

I think I've found the answer - session_name('whatever') can be used to have a fixed name for a session, I can refer to that data as well as the session specific session.

james.bcn
A: 

Elegant, no database and no file ?

Store it in your server memory with shmop and hope your server does not reboot !

Arkh