Skip to content
Advertisement

CI4 on live server not reflecting changes made, it shows old data. Dynamic contents and other changes are not reflected

I created a website using CI4 and it was working fine. On WAMP server it’s working fine. Now if change anything and update the server it shows the old data and session not working properly sometime. I think browser is caching the webpage. Is there any way to disable in CI4? Is it cache issue or session?

“Old data” means if I change a css or a section of html it wont reflect the change, and old html shows. Same for dynamic data.

Cannot login, session not holding login details. For session I am using database. Everything works fine on local server, issue only on live server. I am using plesk hosting.

Anyone have this issue?

When tried on a new pc it works fine. And if any update made and try again issue comes.

Initiated session in base controller:

$this->session = ConfigServices::session();

I use this in the controller for cache control:

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. 

App configuration for session:

public $sessionDriver   = 'CodeIgniterSessionHandlersDatabaseHandler';
public $sessionCookieName  = 'ci_session';
public $sessionSavePath = 'ci_sessions';
public $sessionExpiration        = 7200;
//public $sessionSavePath          = WRITEPATH . 'session';
public $sessionMatchIP           = false;
public $sessionTimeToUpdate      = 300;
public $sessionRegenerateDestroy = false;

Tried Empty/Cache and hard reload. No caching is enable in the code.

Edit: I checked the browser as mentioned in comment and it is caching:

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

I don’t know how this is caching.

The base controller:

<?php
namespace AppControllers;

/**
 * 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 CodeIgniterController;
use ConfigServices; 
 
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(CodeIgniterHTTPRequestInterface $request, CodeIgniterHTTPResponseInterface $response, PsrLogLoggerInterface $logger)
    {
        // Do Not Edit This Line
        parent::initController($request, $response, $logger);

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

}
}

This is the login function:

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 CodeIgniterExceptionsPageNotFoundException($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 CodeIgniterExceptionsPageNotFoundException($page);
        }
        $data['title'] = ucfirst($page);
        echo view('home/login_page', $data);
    }

Advertisement

Answer

i just got it working about 90% by doing as Dont Panic mentioned, added header() to controllers and added $response->noCache(); to the base controller.

Now login and other functionality working fine. Thanks all for helping

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement