为什么呼叫会话不工作? (码信号3) [英] Why call session not working? (codeigniter 3)

查看:133
本文介绍了为什么呼叫会话不工作? (码信号3)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的配置如下:

  $ config ['sess_driver'] ='database'; // select database driver 
$ config ['sess_save_path'] ='ci_sessions'; // mysql表的名称
$ config ['sess_cookie_name'] ='ci_session';
$ config ['sess_expiration'] = 7200;
$ config ['sess_expire_on_close'] = FALSE;
$ config ['sess_encrypt_cookie'] = FALSE;
$ config ['sess_use_database'] = TRUE;
$ config ['sess_table_name'] ='ci_sessions';
$ config ['sess_match_ip'] = FALSE;
$ config ['sess_match_useragent'] = FALSE;
$ config ['sess_time_to_update'] = 300;

我的自动载入如下:

  $ autoload ['libraries'] = array('database','session','form_validation','functions'); 

在控制器 login.php 中:

  $ this-> session-> set_userdata('club','chelsea'); 

在控制器 dashboard.php 中:

  echo $ this-> session-> userdata('club'); 

如果使用不同的控制器,未能成功呼叫会话俱乐部



但是,当我在同一个控制器(控制器 login.php )中调用会话俱乐部时,它工作



Dashboard.php 的构造函数中的 $ this-> library-> load('session'); 。但它不成功



任何解决方案可以解决我的问题?



谢谢

$ b $从@Ali的回答建议什么工作或加载会话库在 Dashboard.php

c> like so ...

  class Dashboard extends CI_Controller 
{
public function __construct b $ b {
parent :: __ construct();
$ this-> library-> load('session');
}

//控制器代码的其余部分
}


$ b b

或者如果您只需要在控制器中的一个特定函数中加载会话,则代替该函数中的库。



关键是库必须在某个地方加载,才能调用它的函数。



在控制器中选择自动加载或显式加载会话可能取决于您的网站需要使用会话的频率。如果每个控制器都使用它们,然后选择自动加载。如果仅在少数地方需要会话,那么将它们加载到未使用的地方是浪费资源。


my config is like this :

$config['sess_driver'] = 'database';  // select database driver
$config['sess_save_path'] = 'ci_sessions';  // name of the mysql table
$config['sess_cookie_name']     = 'ci_session';
$config['sess_expiration']      = 7200;
$config['sess_expire_on_close'] = FALSE;
$config['sess_encrypt_cookie']  = FALSE;
$config['sess_use_database']    = TRUE;
$config['sess_table_name']      = 'ci_sessions';
$config['sess_match_ip']        = FALSE;
$config['sess_match_useragent'] = FALSE;
$config['sess_time_to_update']  = 300;

My autoload is like this :

$autoload['libraries'] = array('database', 'session', 'form_validation', 'functions');

In controller login.php :

$this->session->set_userdata('club', 'chelsea');

In controller dashboard.php :

echo $this->session->userdata('club');

It did not succeed in calling the session club if different controller

But, when I call session club in the same controller(controller login.php), it's working

I try $this->library->load('session'); in the constructor of Dashboard.php. But it's not success

Any solution to solve my problem?

Thank you

解决方案

What the answer from @Ali suggests will work or load the session library in the constructor of Dashboard.php like so...

class Dashboard extends CI_Controller
{
  public function __construct()
  {
    parent::__construct();
    $this->library->load('session');
  }

  //the rest of controller's code
}

Or if you only need sessions in one particular function inside the controller load the library in that function instead.

The point is that the library has to be loaded somewhere before you can make calls to its functions.

The choice of autoloading or explicitly loading session in a controller might depend on how often your site needs to use sessions. If every controller uses them then chose autoload. If sessions are needed in only a few places then it is a waste of resources to load them where they are not used.

这篇关于为什么呼叫会话不工作? (码信号3)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆