【问题标题】:Codeigniter Session is working on localhost but not on web serverCodeigniter 会话在本地主机上工作,但不在 Web 服务器上
【发布时间】:2023-03-21 08:51:01
【问题描述】:

我正在尝试在我的项目中使用会话。它适用于本地主机,但不适用于 Web 服务器。我在 stackoverflow 和 ellislab 中阅读了一些关于此的文章,但它不适用于我的项目。可能问题在于创建辅助类。

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

class Memberlogin extends CI_Controller {

//protected $CI;

function __construct()
{
    $CI =& get_instance();

    parent::__construct();

    $CI->load->library('session');
    //$this->load->library('form_validation');
    //$this->load->helper(array('form', 'url'));
    $CI->load->helper('date');
    $CI->load->helper('ip_address');
    $CI->load->model('Sql_model');
}

现在的错误是:

A PHP Error was encountered

Severity: Notice

Message: Trying to get property of non-object

Line Number: 14

和

A PHP Error was encountered

Severity: Error

Message: Call to a member function library() on a non-object

Line Number: 14

【问题讨论】:

  • 是controllers目录还是libraries目录类?
  • 控制器目录中的这个类

标签: php codeigniter session


【解决方案1】:

试试这个代码,

function __construct()
{
    parent::__construct();

    $this->load->library('session');
    $this->load->helper('date');
    $this->load->helper('ip_address');
    $this->load->model('Sql_model');
}

如果class在controllers目录下,可以通过扩展CI_Controller直接获取CI实例,使用$this。

【讨论】:

  • 一开始我正在尝试那个,但它只适用于本地主机。
  • 但这应该对您在本地主机和实时服务器都有帮助,不需要这个$CI =&amp; get_instance(); 和使用$CI-&gt;
  • ellislab.com/codeigniter/user-guide/general/… 我读了那篇文章,我认为我的问题与该指南有关
  • 见第一行,apart from controllers。
  • 我应该将每个会话保存到数据库吗?
【解决方案2】:

然后,尝试不调用 get_instance() 函数。就像文档说的:

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

class Memberlogin extends CI_Controller {

//protected $CI;

function __construct()
{
    parent::__construct();

    $this->load->library('session');
    //$this->load->library('form_validation');
    //$this->load->helper(array('form', 'url'));
    $this->load->helper('date');
    $this->load->helper('ip_address');
    $this->load->model('Sql_model');
}

【讨论】:

  • 我试过了。但是当我使用它时会话不起作用。当我开始使用 $CI 时,它至少会尝试在 Sql_model 中访问登录功能
【解决方案3】:

这会让你发疯。但它有效。

在autoload.php

$autoload['libraries'] = array('database', 'session');
$autoload['helper'] = array('url','date','ip_address');

和在__constructor中加载模型

public function  __construct()
{
    parent::__construct();
    $this->load->model('Sql_model');
}

【讨论】:

    猜你喜欢
    • 2020-12-07
    • 2014-02-13
    • 2014-01-15
    • 1970-01-01
    • 1970-01-01
    • 2016-03-05
    • 2013-01-22
    • 2011-07-25
    • 2018-03-02
    相关资源
    最近更新 更多