【问题标题】:To clear cache after logout注销后清除缓存
【发布时间】:2022-03-02 00:01:00
【问题描述】:

我已经为控制器登录放置了codeigniter代码。我想在注销后清除缓存。

function logout()
    {
        $this->session->sess_destroy();

        $this->index();

    }

【问题讨论】:

    标签: php codeigniter


    【解决方案1】:

    clearstatcache — 清除文件状态缓存。这个函数缓存了特定文件名的信息,所以你只需要调用clearstatcache(),php内置函数。

    清除浏览器缓存页面:

    header ("Expires: ".gmdate("D, d M Y H:i:s", time())." GMT");  
    header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");  
    header ("Cache-Control: no-cache, must-revalidate");  
    header ("Pragma: no-cache");
    

    【讨论】:

    • 我已将此代码放入注销时显示错误 Call to undefined method Login::clearstatcache()
    【解决方案2】:

    试试这个

    function delete_cache($uri_string=null)
    {
        $CI =& get_instance();
        $path = $CI->config->item('cache_path');
        $path = rtrim($path, DIRECTORY_SEPARATOR);
    
        $cache_path = ($path == '') ? APPPATH.'cache/' : $path;
    
        $uri =  $CI->config->item('base_url').
                $CI->config->item('index_page').
                $uri_string;
    
        $cache_path .= md5($uri);
    
        return unlink($cache_path);
    }
    

    如果不行

    参考这些

    Lib

    manual

    【讨论】:

      【解决方案3】:

      使用这个

      function logout()
      {
              $this->session->sess_destroy();
              $this->cache->clean();
              $this->index();
      }
      

      清除控制器结构中的浏览器缓存

      header("Expires: Thu, 19 Nov 1981 08:52:00 GMT");
      header("Cache-Control: no-store, no-cache, must-revalidate");
      

      还有check this

      【讨论】:

        【解决方案4】:

        如果您想获取数组 cache_item_id 虎钳,那么您可以执行以下操作。

        $wildcard = 'latest';
        $all_cache = $this->cache->cache_info();
        foreach ($all_cache as $cache_id => $cache) :
           if (strpos($cache_id, $wildcard) !== false) :
              $this->cache->delete($cache_id);
           endif;
        endforeach;
        

        或者试试这个

        函数注销(){

                 $this->CI =& get_instance();   
                 $this->CI->session->sess_destroy();
                 $this->cache->clean();
                 redirect(base_url());
        }
        

        【讨论】:

        • 我尝试了第二次,但在我点击返回按钮后注销后显示页面
        【解决方案5】:

        您只需在logout 函数中添加这行代码:

        $this->cache->clean();
        

        有关 codeigniter 中缓存的更多信息,请参阅this

        【讨论】:

        • 显示对非对象的成员函数 clean() 的调用
        • 你加载缓存驱动了吗?? $this->load->driver('cache');
        • 很好,但是当我点击返回按钮退出后,页面正在显示
        • 您需要在那里编写代码以检查会话是否处于活动状态。如果会话为空,则应重定向到索引页面。例如: if($this->session- >userdata('abc')!=""){ //继续 } else{ redirect('index.php'); } 将此代码写入调用您的主页的控制器中。
        【解决方案6】:

        在您的代码中添加以下脚本

        history.pushState(null, null, location.href);
        window.onpopstate = function () {
            history.go(1);
        };
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-11-21
          • 1970-01-01
          相关资源
          最近更新 更多