Skip to content
Derek Jones edited this page Jul 5, 2012 · 4 revisions

Modify the Output class' function _write_cache() to prevent your flashdata from getting stuck in a cache file. Save as system/application/libraries/MY_Output.php.

Lines added:

    // Na-ah-ah! no flashing allowed in the cache;
        $userdata = $CI->session->all_userdata();
        foreach ($userdata as $k => $v)
            {
                if (strpos($k,'flash:') !== FALSE) return;
            }

[size=4]MY_Output[/size]

<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * No Flash Cache
 *
 * @package        CodeIgniter
 * @subpackage    Libraries
 * @hacked-by    Bradford Mar
 */
class MY_Output extends CI_Output {
    
    /**
     * Write a Cache File
     *
     * @access    public
     * @return    void
     */    
    function _write_cache($output)
    {
        $CI =& get_instance();

    // Na-ah-ah! no flashing allowed in the cache;
        $userdata = $CI->session->all_userdata();
        foreach ($userdata as $k => $v)
            {
                if (strpos($k,'flash:') !== FALSE) return;
            }

        $path = $CI->config->item('cache_path');
    
        $cache_path = ($path == '') ? BASEPATH.'cache/' : $path;
        
        if ( ! is_dir($cache_path) OR ! is_really_writable($cache_path))
        {
            return;
        }
        
        $uri =    $CI->config->item('base_url').
                $CI->config->item('index_page').
                $CI->uri->uri_string();
        
        $cache_path .= md5($uri);

        if ( ! $fp = @fopen($cache_path, 'wb'))
        {
            log_message('error', "Unable to write cache file: ".$cache_path);
            return;
        }
        
        $expire = time() + ($this->cache_expiration * 60);
        
        flock($fp, LOCK_EX);
        fwrite($fp, $expire.'TS--->'.$output);
        flock($fp, LOCK_UN);
        fclose($fp);
        @chmod($cache_path, DIR_WRITE_MODE);

        log_message('debug', "Cache file written: ".$cache_path);
    }
}
Clone this wiki locally