2013-03-05 85 views
-1

我有一个会话,我正在使用数据库进行会话。我在会话中使用userdata来存储用户的名称和“登录”标志。我是自动加载会话库,所以我不必显式加载它。预期的CodeIgniter会话行为?

我在控制器,检查“已登录”和视图中使用它来显示用户的名称,这导致CI_Session被加载两次并导致会话被销毁。两次CI_Session加载它试图更新会话和数据库,第一个成功,第二个失败并销毁会话。

我认为我违反了控制器和视图之间的分离,我只用在控制器中的会话库,并通过与用户名变量的视图解决了这个问题。

但我的问题是:分析我做了正确的吗?我是否违反了控制器和视图之间的分离,只要我不再这样做,我应该没问题,或者这可能会在其他情况下再次出现?

注:我想仔细问这个问题,我在寻找技术解答不是的意见,我不希望这成为本VS,一个讨论等

添加代码要求:

段从控制器admin.php的

class Admin extends CI_Controller { 
    function __construct() { 
    parent::__construct(); 
    $this->load->model('users'); 
    $this->id = $this->session->userdata('id'); 
    $this->authorized = array('waccess' => $this->users->authorizedUser($this->id, 'waccess'), 
           'ceditor' => $this->users->authorizedUser($this->id, 'ceditor'), 
           'uadmin' => $this->users->authorizedUser($this->id, 'uadmin'), 
           'forms' => $this->users->authorizedUser($this->id, 'forms')); 

    } 
    public function index() { 
    log_message('debug', 'Admin->index'); 
    $this->load->view('framework', array(
     'head' => $this->load->view('head', array('title' => 'Administrator', 'stylesheet' => 'admin.css', 'javascript' => 'jquery-ui-1.8.16.custom.min.js'), true), 
     'header' => $this->load->view('headerAdmin', array('active' => 'Home', 'authorized' => $this->authorized), true), 
     'body' => $this->load->view('adminHome', '', true), 
     'midBody' => $this->load->view('blankMid', '', true), 
     'footer' => $this->load->view('footer', '', true) 
    )); 
    } 

片段从View adminHome.php

<div id="main"> 
    <div class="content"> 
    <h1>Employee Interface</h1> 
<? if(!$this->session->userdata('loggedin')): ?> 
.... 
<? else: ?> 
    <p>Welcome <?=$this->session->userdata('fname')?> <?=$this->session->userdata('lname')?></p> 
<? endif; ?> 
    <p>Use the menu above to select the various employee and administrative options available to you</p> 
    </div> 
</div> 

我加了很多调试代码,但没有其他变化的CodeIgniter代码,看看发生了什么事情,这里生成的日志条目:

DEBUG - 2013-03-04 19:54:31 --> Config Class Initialized 
DEBUG - 2013-03-04 19:54:31 --> Hooks Class Initialized 
DEBUG - 2013-03-04 19:54:31 --> Utf8 Class Initialized 
DEBUG - 2013-03-04 19:54:31 --> UTF-8 Support Enabled 
DEBUG - 2013-03-04 19:54:31 --> URI Class Initialized 
DEBUG - 2013-03-04 19:54:31 --> Router Class Initialized 
DEBUG - 2013-03-04 19:54:31 --> Output Class Initialized 
DEBUG - 2013-03-04 19:54:31 --> Security Class Initialized 
DEBUG - 2013-03-04 19:54:31 --> Input Class Initialized 
DEBUG - 2013-03-04 19:54:31 --> Global POST and COOKIE data sanitized 
DEBUG - 2013-03-04 19:54:31 --> Language Class Initialized 
DEBUG - 2013-03-04 19:54:31 --> Loader Class Initialized 
DEBUG - 2013-03-04 19:54:31 --> Helper loaded: url_helper 
DEBUG - 2013-03-04 19:54:31 --> loading: session 
DEBUG - 2013-03-04 19:54:31 --> Session Class Initialized 
DEBUG - 2013-03-04 19:54:31 --> Helper loaded: string_helper 
DEBUG - 2013-03-04 19:54:31 --> Database Driver Class Initialized 
DEBUG - 2013-03-04 19:54:31 --> Session using database 
DEBUG - 2013-03-04 19:54:31 --> Session matching on [session_id]: b791b771c776ca4166a73424315d1110 
DEBUG - 2013-03-04 19:54:31 --> Session matching on [user_agent]: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.97 Safari/537.22 
DEBUG - 2013-03-04 19:54:31 --> Session Updating 
DEBUG - 2013-03-04 19:54:31 --> Session Data: [session_id] => b791b771c776ca4166a73424315d1110 
DEBUG - 2013-03-04 19:54:31 --> Session Data: [ip_address] => 184.4.66.94 
DEBUG - 2013-03-04 19:54:31 --> Session Data: [user_agent] => Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.97 Safari/537.22 
DEBUG - 2013-03-04 19:54:31 --> Session Data: [last_activity] => 1362444838 
DEBUG - 2013-03-04 19:54:31 --> Session Data: [user_data] => 
DEBUG - 2013-03-04 19:54:31 --> Session Data: [id] => 15 
DEBUG - 2013-03-04 19:54:31 --> Session Data: [fname] => Test 
DEBUG - 2013-03-04 19:54:31 --> Session Data: [lname] => Test 
DEBUG - 2013-03-04 19:54:31 --> Session Data: [email] => t 
DEBUG - 2013-03-04 19:54:31 --> Session Data: [loggedin] => 1 
DEBUG - 2013-03-04 19:54:31 --> Session Update Completed 
DEBUG - 2013-03-04 19:54:31 --> Session Data: [session_id] => 7875df72dc94ca7bd149debe69865a2e 
DEBUG - 2013-03-04 19:54:31 --> Session Data: [ip_address] => 184.4.66.94 
DEBUG - 2013-03-04 19:54:31 --> Session Data: [user_agent] => Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.97 Safari/537.22 
DEBUG - 2013-03-04 19:54:31 --> Session Data: [last_activity] => 1362444871 
DEBUG - 2013-03-04 19:54:31 --> Session Data: [user_data] => 
DEBUG - 2013-03-04 19:54:31 --> Session Data: [id] => 15 
DEBUG - 2013-03-04 19:54:31 --> Session Data: [fname] => Test 
DEBUG - 2013-03-04 19:54:31 --> Session Data: [lname] => Test 
DEBUG - 2013-03-04 19:54:31 --> Session Data: [email] => t 
DEBUG - 2013-03-04 19:54:31 --> Session Data: [loggedin] => 1 
DEBUG - 2013-03-04 19:54:31 --> Session routines successfully run 
DEBUG - 2013-03-04 19:54:31 --> Controller Class Initialized 
DEBUG - 2013-03-04 19:54:31 --> Model Class Initialized 
DEBUG - 2013-03-04 19:54:31 --> loading: session 
DEBUG - 2013-03-04 19:54:31 --> Session Class Initialized 
DEBUG - 2013-03-04 19:54:31 --> Database Driver Class Initialized 
DEBUG - 2013-03-04 19:54:31 --> Session using database 
DEBUG - 2013-03-04 19:54:31 --> Session matching on [session_id]: b791b771c776ca4166a73424315d1110 
DEBUG - 2013-03-04 19:54:31 --> Session matching on [user_agent]: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.97 Safari/537.22 
DEBUG - 2013-03-04 19:54:31 --> Session not found, destroying instance 
DEBUG - 2013-03-04 19:54:31 --> Session routines successfully run 
DEBUG - 2013-03-04 19:54:31 --> Controller Class Initialized 
DEBUG - 2013-03-04 19:54:31 --> Admin->index 
DEBUG - 2013-03-04 19:54:31 --> File loaded: application/views/head.php 
DEBUG - 2013-03-04 19:54:31 --> File loaded: application/views/headerAdmin.php 
DEBUG - 2013-03-04 19:54:31 --> File loaded: application/views/adminHome.php 
DEBUG - 2013-03-04 19:54:31 --> File loaded: application/views/blankMid.php 
DEBUG - 2013-03-04 19:54:31 --> File loaded: application/views/footer.php 
DEBUG - 2013-03-04 19:54:31 --> File loaded: application/views/framework.php 
DEBUG - 2013-03-04 19:54:31 --> Final output sent to browser 
DEBUG - 2013-03-04 19:54:31 --> Total execution time: 0.0793 
+1

无似乎还有其他错误。你能告诉我们一些代码吗?你如何使用会话方法?应该同时使用的控制器和视图会议通话没问题.. – jtheman 2013-03-05 00:38:05

+0

我想补充一点,大的原因,我要问这个问题,一个是因为我找不到任何答案的问题的原因,当我首先开始使用它,并且我花了很多时间去深入了解Codeigniter代码,以弄清楚我做错了什么。 – TheDavidFactor 2013-03-05 00:41:05

+0

@jtheman当然,给我几分钟 – TheDavidFactor 2013-03-05 00:42:41

回答

0

尝试更改配置设置:

$config['sess_match_useragent'] = FALSE; 

我的猜测是,你有某种无论是代码的在您的视图或其他脚本或浏览器插件,搅乱用户代理检查。这会导致会话匹配不匹配,导致您看到结果Session not found, destroying instance,导致您的错误。

当然,这一变化的结果是不太安全的会议,因此,如果安全是一个大问题,你可以深入了解一下是什么导致课程的问题。

您可以找到其他职位信息...

Googlechrome框架是原因(由引导模板的默认部分): Codeigniter sessions being destroyed in IE 10 when changing pages

FirePHP扩展的原因: http://blog.tiger-workshop.com/firephp-firefox-extension-causing-codeigniter-session-lost/

+0

它似乎没有用useragent的问题,它实际上似乎与ajax问题更密切相关,但没有cookie问题:[found here](http://stackoverflow.com/questions/7980193/ codeigniter-session-bugging-out-with-ajax-calls) – TheDavidFactor 2013-03-06 18:30:07

+0

那么你向我们展示或告诉我们任何内部的AJAX调用。然后这个问题不像我们初次看到的那样是线性的......寻求帮助的好开始是添加与问题相关的所有事情。 – jtheman 2013-03-06 20:05:59