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:
- Caching via files in system temp folders - I could use sys_get_temp_dir() (or some home rolled similar function on PHP4) to help find such a folder. The disadvantage here, is it probably wouldn't work on hosts using the openbase_dir restriction
- Some website I looked at mentioned tricking PHP into making all user sessions share the same session state thereby forcing session variables to act like session variables. Of course I can't find that post now... This seems scary anyway. (I'll update this question with the link to this post once I find it again)
- Use a third party data store like Amazon's Simple Storage Service - seems like overkill
- Cache the data on a server I control, and have the client download new data from there on each hit.
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.