【问题标题】:CI4 on live server not reflecting changes made, it shows old data. Dynamic contents and other changes are not reflected实时服务器上的 CI4 不反映所做的更改,它显示的是旧数据。不反映动态内容和其他更改
【发布时间】:2021-04-19 06:53:12
【问题描述】:

我使用 CI4 创建了一个网站,它运行良好。在 WAMP 服务器上它工作正常。现在,如果更改任何内容并更新服务器,它会显示旧数据和会话有时无法正常工作。我认为浏览器正在缓存网页。 CI4有什么方法可以禁用吗?是缓存问题还是会话?

“旧数据”意味着如果我更改 css 或 html 部分,它不会反映更改,旧 html 会显示。动态数据也一样。

无法登录,会话未保存登录详细信息。对于会话,我正在使用数据库。在本地服务器上一切正常,仅在实时服务器上发布。我正在使用 plesk 托管。

有人有这个问题吗?

在新电脑上试用时,它工作正常。如果有任何更新并重试问题。

在基本控制器中启动会话:

$this->session = \Config\Services::session();

我在控制器中使用它进行缓存控制:

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST, GET, PUT, DELETE, OPTIONS');
//header("Cache-Control: public, max-age=60, s-maxage=60");
header("Cache-Control: no-cache, no-store, must-revalidate"); // HTTP 1.1.
header("Pragma: no-cache"); // HTTP 1.0.
header("Expires: 0"); // Proxies. 

会话的应用配置:

public $sessionDriver   = 'CodeIgniter\Session\Handlers\DatabaseHandler';
public $sessionCookieName  = 'ci_session';
public $sessionSavePath = 'ci_sessions';
public $sessionExpiration        = 7200;
//public $sessionSavePath          = WRITEPATH . 'session';
public $sessionMatchIP           = false;
public $sessionTimeToUpdate      = 300;
public $sessionRegenerateDestroy = false;

尝试清空/缓存和硬重新加载。代码中没有启用缓存。

编辑: 我检查了评论中提到的浏览器,它正在缓存:

accept-ranges: bytes
cache-control: max-age=315360000
content-encoding: gzip
content-length: 9333
content-security-policy: upgrade-insecure-requests;
content-type: text/css
date: Thu, 14 Jan 2021 13:07:22 GMT
etag: "f0fc6fe8a1bdd61:0"
expires: Thu, 31 Dec 2037 23:55:55 GMT
last-modified: Wed, 18 Nov 2020 11:56:54 GMT
server: nginx
vary: Accept-Encoding
x-content-type-options: nosniff
x-frame-options: SAMEORIGIN
x-powered-by-plesk: PleskWin
x-sucuri-cache: HIT
x-sucuri-id: 18015
x-xss-protection: 1; mode=block

我不知道这是怎么缓存的。

基本控制器:

<?php
namespace App\Controllers;

/**
 * Class BaseController
 *
 * BaseController provides a convenient place for loading components
 * and performing functions that are needed by all your controllers.
 * Extend this class in any new controllers:
 *     class Home extends BaseController
 *
 * For security be sure to declare any new methods as protected or private.
 *
 * @package CodeIgniter
 */

use CodeIgniter\Controller;
use Config\Services; 
 
header("Cache-Control: no-cache, no-store, must-revalidate"); // HTTP 1.1.
header("Pragma: no-cache"); // HTTP 1.0.
header("Expires: 0"); // Proxies.
class BaseController extends Controller
{

    /**
     * An array of helpers to be loaded automatically upon
     * class instantiation. These helpers will be available
     * to all other controllers that extend BaseController.
     *
     * @var array
     */
    protected $helpers = ['form', 'url','master'];
        protected $session;
    /**
     * Constructor.
     */
    public function initController(\CodeIgniter\HTTP\RequestInterface $request, \CodeIgniter\HTTP\ResponseInterface $response, \Psr\Log\LoggerInterface $logger)
    {
        // Do Not Edit This Line
        parent::initController($request, $response, $logger);

        //--------------------------------------------------------------------
        // Preload any models, libraries, etc, here.
        //--------------------------------------------------------------------
        // E.g.:
         $this->session = \Config\Services::session();
         date_default_timezone_set('Asia/Dubai');
                     $this->pager = \Config\Services::pager();
                    
                    

}
}

这是登录功能:

public function login($type = 0)
    {
        $session = session();
        $model = new HomeModel();
        $model1 = new CartModel;
        if ($this->request->getMethod() == 'post') {
            if (!$this->validate([
                'email' => [
                    'label' => 'Email',
                    'rules' => 'trim|required|is_not_unique[tbl_customers.email]',
                    'errors' => ['is_not_unique' => '{value}-Email is not registered with us']
                ],
                'current-password' => ['label' => 'Password', 'rules' => 'trim|required']
            ])) {
                $data['validation'] = $this->validator;
                $page = 'login_page';
                if (!is_file(APPPATH . '/Views/home/' . $page . '.php')) {
                    throw new \CodeIgniter\Exceptions\PageNotFoundException($page);
                }
                $data['title'] = ucfirst($page);

                return view('home/login_page', $data);
            } else {
                $status = $model->checkLogin($this->request->getVar());
                if (isset($status)) {
                    $session->remove('customerData');
                    session()->set('customerData', $status);
                    $cmsrDetails = session('customerData');
                    if (!empty(cart()->contents())) {
                        $cart = cart()->contents();
                        foreach ($cart as $key => $value) {
                            $is_exist = $model1->checkItems($value['rowid'], $cmsrDetails['customerID']);
                            if (!empty($is_exist)) {
                                if ($value['qty'] != 0) {
                                    $data = [
                                        'qty' => $value['qty']
                                    ];
                                    $model1->update($is_exist['id'], $data);
                                } else {
                                    $model1->delete($is_exist['id']);
                                }
                            } else {
                                $datain = [
                                    'CMid' => $cmsrDetails['customerID'],
                                    'rowId' => $value['rowid'],
                                    'itemId' => $value['id'],
                                    'qty' => $value['qty']
                                ];
                                $model1->insert($datain);
                            }
                        }
                    }
                    $getCart = $model1->CartItems($cmsrDetails['customerID']);
                    if (!empty($getCart)) {
                        $model1->UpdateCart($getCart);
                    }
                    return redirect()->to('/');
                } else {
                    $_SESSION['error'] = 'Username or Password incorrect';
                    $session->markAsFlashdata('error');
                }
            }
        }
        if (!empty(session('customerData'))) {
            return redirect()->to('/');
        }
        //$data['validation'] = $this->validator;
        $page = 'login_page';
        if (!is_file(APPPATH . '/Views/home/' . $page . '.php')) {
            throw new \CodeIgniter\Exceptions\PageNotFoundException($page);
        }
        $data['title'] = ucfirst($page);
        echo view('home/login_page', $data);
    }

【问题讨论】:

  • 请编辑您的问题并添加一些详细信息。 it shows the old data - 什么数据?数据是如何生成的,您如何更新它? and session not working - 这是什么意思?登录抛出错误?会话变量消失?向我们展示具体的具体细节。当然,首先检查会话问题将是检查您的 sessionSavePath 的权限 - 如果您使用的是基于文件的会话(您没有告诉我们或向我们展示)。
  • @Don'tPanic :旧数据意味着如果我更改 css 或 html 的一部分,它不会反映更改。旧 html 显示。动态数据相同。无法登录,会话未保存登录详细信息。对于使用数据库的会话 iam。在本地服务器上一切正常。仅在实时服务器上发布。我正在使用 plesk 托管。
  • 请编辑您的问题并添加该信息 - 让其他人更容易看到他们需要什么来帮助您。 RE:缓存 - 好的,开始调试。使用浏览器的开发工具,网络选项卡会显示什么?选择有问题的 HTML 页面之一并检查标题。是否被缓存? RE:会话 - 好的,开始调试。记录是否写入数据库表?你用对了吗sessionDriver?您是否创建并配置了表?您可以检查很多您没有告诉我们的...开始工作! :-)
  • i think browser is caching the webpage ... if i change a css or a section of html it wont reflect the change 这确实很常见,并且是您的浏览器试图通过缓存页面的各个部分来节省带宽的副产品。当您希望浏览器确定从服务器拉取最新代码时,您需要“清空缓存并硬重新加载”以确保发生这种情况;此处的说明:hexnode.com/mobile-device-management/help/…
  • 听起来您有两个不同的问题(缓存和会话)。 1)缓存:您拥有的控制器代码不正确 - header() 代码应该在方法内。作为测试,将它们移动到您用于实际提供页面的方法之一中,并再次检查浏览器中的缓存。 2)数据库表是否存在?当您登录时,是否会在表中创建记录?

标签: php session browser-cache codeigniter-4


【解决方案1】:

按照 Dont Panic 提到的,我刚刚让它工作了大约 90%,将 header() 添加到控制器并将 $response-&gt;noCache(); 添加到基本控制器。

现在登录和其他功能工作正常。 感谢大家的帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-22
    • 1970-01-01
    • 2022-01-11
    • 2021-01-12
    相关资源
    最近更新 更多