【问题标题】:Codeigniter session not working in another controller when using xampp使用 xampp 时,Codeigniter 会话在另一个控制器中不起作用
【发布时间】:2017-08-04 09:57:54
【问题描述】:

我创建了一个 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');

这段代码在我的使用 Apache + PHP 7.1 和 MySQL 的 Web 服务器上运行良好,但在 Windows 中无法使用 xampp 7.1.1。使用 xampp 时,Codeignitor 会话在 getSession 函数中不起作用。

我的 Codeigniter 配置文件是默认的,我检查了 PHP 的 TimeZone。

【问题讨论】:

  • 您可以尝试在 config.php 文件第 317 行更改加密密钥。您必须在 $config['encryption_key'] = 'your key'; 中提及一些字符串。其用户定义

标签: php codeigniter session xampp


【解决方案1】:

您不需要加载 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

自动加载.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;?>

使用redirect() https://www.codeigniter.com/user_guide/helpers/url_helper.html#redirect

【讨论】:

  • Tks wolfgang,但我正在尝试从另一个函数获取会话数据。就我而言,我从 index() 函数在会话中设置了一些数据,我想在 testsession() 中使用这些数据。类似于:stackoverflow.com/questions/26843009/…,它在我的网络服务器中工作,但在 xampp (localhost) 中不工作
猜你喜欢
  • 2017-08-03
  • 1970-01-01
  • 1970-01-01
  • 2020-05-24
  • 2020-04-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-25
相关资源
最近更新 更多