Search code examples
phpcachingfile-permissionsapplication-variables

PHP Caching without messing with file permissions (Also, How to emulate ASP Application variables in PHP)


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.


Solution

  • 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:

    1. 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()

    2. 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();
    ?>