我创建了一个用于测试的Codeignier控制器,我的控制器使用codeigniter会话库:
test.php的
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Test extends CI_Controller {
function __construct()
{
parent::__construct();
$this->config->load('config');
$this->load->helper("url");
}
function index()
{
$newdata = array(
'username' => 'uname',
'email' => 'uname@some-site.com'
);
$this->session->set_userdata('testsession', $newdata);
redirect("https://mysite/index.php/test/getSession");
}
function getSession()
{
var_dump($this->session->userdata('testsession'));
}
}
会话库在Codeigniter的自动加载中加载。
$autoload['libraries'] = array('session');
此代码在我的Web服务器上运行良好,使用Apache + PHP 7.1和MySQL,但不能在Windows中使用xampp 7.1.1。使用xampp时,Codeignitor会话在getSession函数中不起作用。
我的Codeigniter配置文件是默认的,我检查了PHP的TimeZone。
答案 0 :(得分:0)
您无需加载config / config.php文件,因为它已准备就绪。
确保您已将会话设置为此类我自己在系统中创建了一个会话文件夹,因此它受到更多保护。设置文件夹权限0700
$config['sess_driver'] = 'files';
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 1440;
$config['sess_save_path'] = BASEPATH . 'storage/session/';
$config['sess_match_ip'] = TRUE;
$config['sess_time_to_update'] = 300;
$config['sess_regenerate_destroy'] = TRUE;
请注意
EXT: The PHP file extension
FCPATH: Path to the front controller (this file) (root of CI)
SELF: The name of THIS file (index.php)
BASEPATH: Path to the system folder
APPPATH: The path to the "application" folder
Autoload.php
$autoload['libraries'] = array('session');
$autoload['helper'] = array('form', 'url');
控制器
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Test extends CI_Controller {
function __construct() {
parent::__construct();
// Autoload the url helper in config/autoload.php
$this->load->helper("url");
}
function index() {
$newdata = array(
'username' => 'uname',
'email' => 'uname@some-site.com'
);
$this->session->set_userdata('testsession', $newdata);
$session_data = $this->getSession();
$data['username'] = $session_data['username'];
$data['email'] = $session_data['email'];
$this->load->view('test_view', $data);
}
function getSession()
{
return $this->session->userdata('testsession');
}
}
然后在views / test_view.php
上<?php echo $username;?>
<?php echo $email;?>
使用重定向()https://www.codeigniter.com/user_guide/helpers/url_helper.html#redirect