gpt4 book ai didi

php - 使用 xampp 时,Codeigniter session 无法在另一个 Controller 中工作

转载 作者:行者123 更新时间:2023-12-01 15:16:05 24 4
gpt4 key购买 nike

我创建了一个用于测试的 Codeignier Controller ,我的 Controller 使用 codeigniter session 库:

测试.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'));

}

}

session 库在 Codeigniter 的自动加载中加载。

$autoload['libraries'] = array('session');

这段代码在我使用 Apache + PHP 7.1 和 MySQL 的网络服务器中运行良好,但在 Windows 中不适用于 xampp 7.1.1。使用 xampp 时,Codeignitor session 在 getSession 函数中不起作用。

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

最佳答案

您不需要加载 config/config.php 文件,因为它已准备就绪。

确保你已经设置了你的 session ,我自己在系统中创建了一个 session 文件夹,所以它更 protected 。设置文件夹权限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');

Controller

<?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

关于php - 使用 xampp 时,Codeigniter session 无法在另一个 Controller 中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42779660/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com