I'm trying to implement some kind of caching in a PHP script that will go out to many different clients/sites, which will be deployed by fairly non-technical users, using a variety of web hosts. Due to the non-technical nature of the users, I'd like to avoid asking them to tweak file permissions. The caching needs to be across sessions, so using session variables is out. If I was coding this in ASP I'd use application variables, but they don't exist in PHP (to my knowledge)
Does anyone have any suggestions on how to accomplish this?
Here are some possibilities I've considered, any comments on these would be useful:
Once again, any comments on these ideas or any new ones would be appreciated.
UPDATE: I tried using session_id() to use a shared session state, but it doesn't work. Each session is maintaining its own "GlobalCache", any ideas why?:
// This code doesn't work, but the similar code in my answer does!
function test() {
if (!$_SESSION['session_count']) $_SESSION['session_count'] = 0;
$_SESSION['session_count']++;
$count = get_cache( 'count' );
print "pre application count: $count<br>";
if ( !$count )
$count = 0;
$count++;
print "session_id: " . session_id() . "<br>";
print "post application count: $count<br>";
print "session_count: " . $_SESSION['session_count'] . "<br>";
set_cache('count', $count);
}
function set_cache( $name, $value ) {
$old_session = session_id();
print "old_session (set): $old_session<br>";
session_id("GlobalCache");
print "new_session (set): " . session_id() . "<br>";
$_SESSION[$name] = $value;
session_id( $old_session );
}
function get_cache( $name ) {
$old_session = session_id();
print "old_session (get): $old_session<br>";
session_id("GlobalCache");
print "new_session (get): " . session_id() . "<br>";
$value = $_SESSION[$name];
session_id( $old_session );
return $value;
}
session_start();
test();
UPDATE: some have suggested using memcached, which is in fact a great solution for some, but since I don't have control over the end server environment it isn't an option. The idea is to have a script that people can just FTP up to a shared hosting account that just works out of the box.
UPDATE: someone suggested creating my own cache folder with the script, but wouldn't I need to create such a folder inside a folder that already had write permissions?
UPDATE, SOLUTION FOUND: I ended up figuring out the problems in my global session script and have posted my own answer to that effect. Thanks for the help everyone.
Okay, I figured out how to do this by emulating ASP-style application variables using a shared/global session state. Two key changes from my non-working code in the relevant update:
To switch session state, we must end the current session, switch to the new one, then start it. I've encapsulated this process in switch_session()
Since we are switching session ids around we have to buffer the page's output using ob_start()/ob_end_flush() so that the session cookie isn't sent too soon.
The full working code follows (cleaned up too!). This can be easily tested by loading the page in an IE and a Firefox window, and reloading each several times to watch the counters rise:
<?php
function test() {
// Do a regular session count
print "session_id: " . session_id() . "<br>";
if (!$_SESSION['session_count']) $_SESSION['session_count'] = 0;
$_SESSION['session_count']++;
print "session count: " . $_SESSION['session_count'] . "<br>";
// Do an application count
$count = get_cache( 'count' );
if ( !$count ) $count = 0;
$count++;
print "application count: $count<br>";
set_cache('count', $count);
}
function set_cache( $name, $value ) {
$old_session = switch_session("GlobalCache");
$_SESSION[$name] = $value;
switch_session( $old_session );
}
function get_cache( $name ) {
$old_session = switch_session("GlobalCache");
$value = $_SESSION[$name];
switch_session( $old_session );
return $value;
}
function switch_session( $session_id ) {
// switch the session and return the original
$old_id = session_id();
session_write_close();
session_id($session_id);
session_start();
return $old_id;
}
ob_start();
session_start();
test();
ob_end_flush();
?>