You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Codeigniter报$session未定义及flashdata()调用null错误如何解决

CodeIgniter 调用session相关报错解决方案

报错信息

  • 错误1:Message: Undefined property: Welcome::$session
  • 错误2:Message: Call to a member function flashdata() on null
    报错截图

问题根因

两个报错由两个问题共同导致:

  1. CodeIgniter框架默认不自动加载Session类库,控制器中直接调用$this->session时,当前控制器实例不存在该属性,因此返回null
  2. 闪存数据的方法调用错误:flashdata()方法仅用于读取闪存数据,写入闪存数据需要使用set_flashdata()方法

修复步骤

1. 加载Session类库

根据使用场景二选一即可:

  • 全局自动加载(全站通用推荐):打开application/config/autoload.php文件,修改$autoload['libraries']配置项,将session加入自动加载列表:
// 示例:代码中用到了form_validation,可一并配置自动加载
$autoload['libraries'] = array('session', 'form_validation');
  • 单控制器临时加载:如果仅需要在Welcome控制器中使用session,在调用session相关逻辑前手动加载类库即可:
// 可以写到控制器构造方法,或者registerNow方法内调用session之前
$this->load->library('session');

2. 修正闪存数据写入逻辑

将原代码中错误的flashdata调用替换为正确的写入方法:

// 原错误写法
// $this->session->flashdata('success', 'Successfully User Created');
// 修正后写法
$this->session->set_flashdata('success', 'Successfully User Created');

后续在视图或其他方法中读取该条闪存提示时,使用$this->session->flashdata('success')即可获取对应内容。

补充检查项

如果完成以上步骤后session仍无法正常工作,打开application/config/config.php检查session相关配置:确认cookie作用域、session存储路径等参数匹配当前运行环境,开发环境下默认配置即可正常运行。

修正后Welcome.php代码参考

<?php

defined('BASEPATH') or exit('No direct script access allowed');

class Welcome extends CI_Controller
{
    // 如果选择单控制器加载,可在构造方法中统一加载类库
    // public function __construct()
    // {
    //     parent::__construct();
    //     $this->load->library('session');
    //     $this->load->library('form_validation');
    //     $this->load->model('user_model');
    // }

    public function index()
    {
        $this->load->view('home');
    }

    function registerNow()
    {
        if ($_SERVER['REQUEST_METHOD'] == 'POST') {
            $this->form_validation->set_rules('fname', 'First Name', 'required|alpha');
            $this->form_validation->set_rules('mname', 'Middle Name', 'required|alpha');
            $this->form_validation->set_rules('lname', 'Last Name', 'required|alpha');
            $this->form_validation->set_rules('username', 'Username', 'required');
            $this->form_validation->set_rules('password', 'Password', 'required|min_length[8]');
            $this->form_validation->set_rules('confirmpass', 'Confirm Password', 'required|min_length[8]|matches[password]');
            $this->form_validation->set_rules('bday', 'Birthday', 'required');
            $this->form_validation->set_rules('email', 'Email', 'required|valid_email');
            $this->form_validation->set_rules('contact', 'Contact Number', 'required|numeric');

            if ($this->form_validation->run() == TRUE) {
                $fname = $this->input->post('fname');
                $mname = $this->input->post('mname');
                $lname = $this->input->post('lname');
                $username = $this->input->post('username');
                $password = $this->input->post('password');
                $bday = $this->input->post('bday');
                $email = $this->input->post('email');
                $contact = $this->input->post('contact');

                $data = array(
                    'first_name' => $fname,
                    'middle_name' => $mname,
                    'last_name' => $lname,
                    'username' => $username,
                    'password' => sha1($password),
                    'bday' => $bday,
                    'email' => $email,
                    'contact_number' => $contact
                );

                $this->load->model('user_model');
                $this->user_model->insertuser($data);
                $this->session->set_flashdata('success', 'Successfully User Created');
                redirect(base_url('welcome/index'));
            }
        }
    }
}

内容的提问来源于stack exchange,提问作者kimski

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.27 09:24:18