我在库中的类中设置了会话。然后,我尝试从库中的另一个类中获取此会话的值。该值为空。
我查看了CodeIgniter库,以找出问题所在以及StackOverflow的一些类似主题,但最终发现没有什么问题。也许我缺少了一些东西。请帮忙。
// IN THIS CLASS I SET THE SESSION 'fr_phone'
class Form_submit_fr {
var $CI;
protected $fr_id;
protected $fr_data = array();
public function __construct() {
$this->CI =& get_instance();
if (strpos($this->CI->uri->segment(1),'frid') !== false){
$this->fr_id = (int)str_replace('frid','',$this->CI->uri->segment(1));
$this->fr_data = $this->get_fr_data();
// add the phone to session
if (isset($this->fr_data->city_toll) && $this->fr_data->city_toll != '') {
$fr_phone = $this->fr_data->city_toll;
} else {
$fr_phone = $this->fr_data->city_phone;
}
// here the session is set
$this->CI->session->set_userdata('fr_phone', $fr_phone);
//the result is correct
echo $this->CI->session->userdata('fr_phone') . ' - fr_phone<br>';
}
}
....
}
// IN THIS CLASS I NEED TO GET THIS SESSION 'fr_phone'
class P_details {
protected $page_link;
protected $fr_data;
var $CI;
public function __construct(){
$this->CI =& get_instance();
if ($this->CI->uri->segment(2) == false && $this->CI->uri->segment(3) == false) {
$this->page_link = $this->uri->segment(1)."/";
} else if ($this->CI->uri->segment(1) != false && $this->CI->uri->segment(2) != false && $this->CI->uri->segment(3) == false) {
$this->page_link = $this->CI->uri->segment(1)."/".$this->CI->uri->segment(2)."/";
} else if ($this->CI->uri->segment(1) != false && $this->CI->uri->segment(2) != false && $this->CI->uri->segment(3) != false) {
$this->page_link = $this->CI->uri->segment(1)."/".$this->CI->uri->segment(2)."/";
$this->fr_data = $this->get_fr_data($this->CI->uri->segment(3));
}
$this->CI->load->model('getDetails_model');
}
public function get_p_id($type = '') {
$details_array = $this->CI->getDetails_model->all_details($this->page_link);
if (empty($details_array)) {
return FALSE;
}
else {
$data['fr_phone'] = '';
$data['detail'] = $details_array;
// get phone from session
// here the session is empty
echo $this->CI->session->userdata('fr_phone') . ' - fr_phone<br>';
if ($this->CI->session->userdata('fr_phone')) {
$data['fr_phone'] = $this->CI->session->userdata('fr_phone');
}
if ($this->fr_data != '') {
$data['fr_detail'] = $this->fr_data;
}
return $data;
}
}
...
}
这是我的配置文件:
$config['sess_driver'] = 'files';
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 7200;
$config['sess_save_path'] = NULL;
$config['sess_match_ip'] = FALSE;
$config['sess_time_to_update'] = 300;
$config['sess_regenerate_destroy'] = FALSE;
$config['cookie_prefix'] = '';
$config['cookie_domain'] = '';
$config['cookie_path'] = '/';
$config['cookie_secure'] = FALSE;
$config['cookie_httponly'] = FALSE;
这是我的自动加载文件:
$autoload['libraries'] = array('database','session');
我已经在$ config ['sess_save_path']的配置文件中进行了建议的更改。非常感谢。通过这种方式检查时,我也可以看到可写文件夹中的文件和数据库中的条目:
$config['sess_driver'] = 'files';
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 43200;
$config['sess_save_path'] = APPPATH.'writable';
$config['sess_match_ip'] = FALSE;
$config['sess_time_to_update'] = 300;
$config['sess_regenerate_destroy'] = FALSE;
我也使用数据库进行会话:
$config['sess_driver'] = 'database';
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 43200;
$config['sess_save_path'] = 'ci_sessions';
$config['sess_match_ip'] = FALSE;
$config['sess_time_to_update'] = 300;
$config['sess_regenerate_destroy'] = FALSE;
在两种情况下,如果我在构造函数或索引函数中的Controller中定义会话,则该会话均有效。我可以在View文件和其他类中获取会话,但是如果我在特定链接的条件下设置了会话,则当我想从库文件夹或视图文件中的类中获取该会话时,该会话始终为空:
//in controller
if (strpos($this->uri->segment(1),'frid') !== false){
...
$fr_phone = '111-222-3333';
$this->session->set_userdata('fr_phone', $fr_phone);
echo $this->session->userdata('fr_phone') // works
}
当我在“ libraries”文件夹中的“ Class”中调用此会话时,该会话为空:上面的P_details类示例。而且,无法从Librarys文件夹获取在“类”中设置的会话,也无法从Librarys文件夹获取在“另一个类”中的会话。这很奇怪,我无法理解。另外,我尝试在其他控制器上进行会话,但没有成功。
答案 0 :(得分:0)
我在一个项目中遇到了类似的问题。经过长时间的尝试,我可以通过以下方式解决问题
1)在config.php中
import { Component, OnInit } from '@angular/core';
import {MatDatepickerModule} from '@angular/material/datepicker';
@Component({
selector: 'app-example',
templateUrl: './example.component.html',
styleUrls: ['./example.component.css']
})
export class ExampleComponent implements OnInit {
constructor() {
}
ngOnInit() {
}
}
希望这会有所帮助
答案 1 :(得分:0)
这很奇怪,但是我从自动加载中删除了该会话: $ autoload ['libraries'] = array('database'); 我添加了session_start();在控制器中,我以一种通用方式设置会话:
$ _ SESSION ['fr_phone'] = $ fr_phone;
然后,我可以在任何文件中获取此值。这是错误吗? 我尝试通过添加$ this-> load-> library('session');来不使用session_start();它没有用。 谁能找到这个问题的答案?
我正在使用CodeIgniter 3.0.2