[英]php - Session are lost after redirect in CodeIgniter 3
我想在 CodeIgniter 得到一些帮助 3. 每次我登录并重定向到索引页面时,session 都丢失了。
这是我的代码:
Controller:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Secretariat extends CI_Controller {
public function __construct(){
parent::__construct();
$this->load->helper(array('form', 'url'));
$this->load->model('SecretariatModel');
$this->load->model('IndiRegModel');
$this->load->model('RoomModel');
$this->load->model('BuildingModel');
$this->load->model('BilletModel');
$this->load->model('BatchRegModel');
$this->load->library('form_validation');
$this->load->library('session');
}
public function secretariatLogin(){
if ($this->session->isSecretariat){
redirect('secretariat/index', $data);
} else{
$data['title'] = 'PACSA - Philippine Association of Campus Student Adviser';
$this->load->view('include/toppage', $data);
$this->load->view('include/defaultnavbar', $data);
$this->load->view('pacsa/slider');
$this->load->view('secretariat/secretariatLogin', $data);
$this->load->view('include/bottompage');
}
}
public function signin(){
$secretariat = array(
'sec_email' => $this->input->post('sec_email'),
'sec_password' => sha1($this->input->post('sec_password'))
);
$user = $this->SecretariatModel->getSecretariat($secretariat);
//print_r($user->name);die();
if(!$user == null){
$newdata = array(
'sec_id' => $user->sec_id,
'sec_name' => $user->sec_name,
'sec_lastname' => $user->sec_lastname,
'sec_email' => $user->sec_email,
'sec_password' => $user->sec_password,
'sec_status' => $user->sec_status,
'sec_address' => $user->sec_address,
'logged_in' => TRUE,
'isSecretariat' => TRUE
);
$this->session->set_userdata($newdata);
redirect('secretariat/index');
} else {
$data['title'] = 'PACSA - Philippine Association of Campus Student Adviser';
$data['message'] = 'Invalid email or password';
$this->load->view('include/toppage', $data);
$this->load->view('include/defaultnavbar', $data);
$this->load->view('pacsa/slider');
$this->load->view('secretariat/secretariatLogin', $data);
$this->load->view('include/bottompage');
}
}
public function index(){
$data['title'] = 'PACSA - Philippine Association of Campus Student Adviser';
$id = $this->session->sec_id;
var_dump($id);
echo die();
$this->load->view('include/toppage', $data);
$this->load->view('include/secretariatnavbar', $data);
$this->load->view('pacsa/slider');
$this->load->view('secretariat/index', $data);
$this->load->view('include/bottompage');
}
}
所以在重定向到index页面后,我想验证是否涉及到session。 我试图回应用户的 ID 和名称,但我得到一个 null 值。
还要检查您的 php.ini 文件是否有此选项:
session.auto_start=1
在此链接中:
https://php.developreference.com/article/23484402/Codeigniter+session+data+lost+after+redirect
如果您在 CI 3.x 中工作并且刚刚将您的服务器 php 版本升级到 php 7.x 转到第 281 行的system/libraries/Session/session.php
并替换ini_set('session.name', $params['cookie_name']);
通过ini_set('session.id', $params['cookie_name']);
前往
系统/库/会话/session.php
在第 281 行并替换
ini_set('session.name', $params['cookie_name']);
由
ini_set('session.id', $params['cookie_name']);
这个问题在升级PHP以后版本到7.3+时正常出现
你有没有加载你的会话库
this->load->library('session')
或者通过自动加载
$autoload['libraries'] = array('session');
我刚刚解决了我的问题,结果我有旧版本的 codeigniter 3 我将我的 CI 升级到网站上提供的最新版本。 谢谢大家的帮助
我在将$config['sess_driver']
设置为'database'
时遇到了这个问题。
我使用的是有效的 MySQL 用户,所以 CodeIgniter 3 没有抱怨 sql 连接,但用户没有从表php_sessions
(或$config['sess_save_path']
设置的表)读取/写入的所有权限。
就我而言,我在我的数据库中使用默认用户“debian-sys-maint”。php。
您的问题不是来自会话本身。
尝试:
注释重定向方法并添加一个var_dump($user)
来查看您的$user
是否设置正确。 问题可能来自您的$user
对象,它包含id
和name
空值。
将if(!$user == null){
更改为if ($user != null) {
或if ($user) {
。
如果您使用的是 PHP 7.1+/7.2,则会发生此问题。 将 PHP 版本更改为 down(以检查它是否有效)。
我们也遇到同样的问题。 我们的规范:InvoicePlane,PHP 7.0,代码点火器 v.3.1.11。 每个使用 redirect() 助手的控制器都从浏览器获得 err_connection_refused 。
经过几个小时的详尽调试。 我们by change IP Address base_url to domain name and set certificate from letsencrypt.
解决此问题by change IP Address base_url to domain name and set certificate from letsencrypt.
希望这会有所帮助,我正在使用 Homestead - Vagrant - php7.4,我让它工作的唯一方法是在 Github 上遵循这个答案
将此添加到您的
项目/应用/配置/config.php
/*
|--------------------------------------------------------------------------
| Cookie Related Variables
|--------------------------------------------------------------------------
|
| 'cookie_prefix' = Set a cookie name prefix if you need to avoid collisions
| 'cookie_domain' = Set to .your-domain.com for site-wide cookies
| 'cookie_path' = Typically will be a forward slash
| 'cookie_secure' = Cookie will only be set if a secure HTTPS connection exists.
| 'cookie_httponly' = Cookie will only be accessible via HTTP(S) (no javascript)
|
| Note: These settings (with the exception of 'cookie_prefix' and
| 'cookie_httponly') will also affect sessions.
|
*/
$config['cookie_prefix'] = '';
$config['cookie_domain'] = '';
$config['cookie_path'] = '/';
$config['cookie_secure'] = FALSE;
$config['cookie_httponly'] = FALSE;
还将行从 289 更改为 352
项目/系统/核心/输入.php
/**
* Set cookie
*
* Accepts an arbitrary number of parameters (up to 7) or an associative
* array in the first parameter containing all the values.
*
* @param string|mixed[] $name Cookie name or an array containing parameters
* @param string $value Cookie value
* @param int $expire Cookie expiration time in seconds
* @param string $domain Cookie domain (e.g.: '.yourdomain.com')
* @param string $path Cookie path (default: '/')
* @param string $prefix Cookie name prefix
* @param bool $secure Whether to only transfer cookies via SSL
* @param bool $httponly Whether to only makes the cookie accessible via HTTP (no javascript)
* @return void
*/
public function set_cookie($name, $value = '', $expire = 0, $domain = '', $path = '/', $prefix = '', $secure = NULL, $httponly = NULL)
{
if (is_array($name))
{
// always leave 'name' in last place, as the loop will break otherwise, due to $$item
foreach (array('value', 'expire', 'domain', 'path', 'prefix', 'secure', 'httponly', 'name') as $item)
{
if (isset($name[$item]))
{
$$item = $name[$item];
}
}
}
if ($prefix === '' && config_item('cookie_prefix') !== '')
{
$prefix = config_item('cookie_prefix');
}
if ($domain == '' && config_item('cookie_domain') != '')
{
$domain = config_item('cookie_domain');
}
if ($path === '/' && config_item('cookie_path') !== '/')
{
$path = config_item('cookie_path');
}
$secure = ($secure === NULL && config_item('cookie_secure') !== NULL)
? (bool) config_item('cookie_secure')
: (bool) $secure;
$httponly = ($httponly === NULL && config_item('cookie_httponly') !== NULL)
? (bool) config_item('cookie_httponly')
: (bool) $httponly;
if ( ! is_numeric($expire) OR $expire < 0)
{
$expire = 1;
}
else
{
$expire = ($expire > 0) ? time() + $expire : 0;
}
setcookie($prefix.$name, $value, $expire, $path, $domain, $secure, $httponly);
}
试试这个
$this->output->profiler(true);
查看会话是否真的设置并检查库是否已加载
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.