douzhu6149 2013-04-17 11:35
浏览 42

会话数据自动丢失而无需在codeigniter中单击注销

I have a web application using codeigniter with users about 200. If about 100 users login to the system at the same time, sometimes the user logout automatically or redirect to the login page without click logout before.

My Code.

Login Helper (Load in Construst to check login or not)

function is_logged_in()
{
    //Get Codeigniter Instance
    $obj =& get_instance();

    $is_logged_in = $obj->session->userdata('LOGGED_IN');

    if(!isset($is_logged_in) || $is_logged_in != true)
    {   
        $obj->session->set_flashdata('message','<div class="error_login"><b>ERROR:</b> Silahkan login terlebih dahulu.</div>');

        //If no session, redirect to login page
        redirect('login', 'refresh');
    }

 }

Controller.

public function verify()
{
    $username = $this->input->post('username');
    $password = $this->input->post('password');

    //Query the database
    $row = $this->Loginmodel->verify($username,$password);

    if (count($row)) {

        $dataSession = array(
            'userid' => $row->userid,
            'role' => $row->role,
            'prov_cd' => $row->prov_cd,
            'kab_cd' => $row->kab_cd,
            'LOGGED_IN'=> true,
            'MODAL'=> true
        );

        $this->session->set_userdata($dataSession);

        redirect('dashboard','refresh');

    } 
    else
    {
         $this->session->set_flashdata('message','<div class="error_login"><b>ERROR:</b> Username atau password salah.</div>');
         redirect('login','refresh');
    }

}

I think it's because I store many parameters in session data. Thanks.

  • 写回答

1条回答 默认 最新

  • dongtun3328 2013-04-17 11:43
    关注

    Look at your count($row) can you please specify it to be

    COUNT($row) == 1
    

    So I sorta understand what you are trying to do, if the $row returns a count of 0 then the user name password didn't verify, if it returns a single user example

    == 1
    

    then the user name is correct and did verify. Try this solution.

    as per php documentation it does not return a Boolean true or false. Try that please. And also what errors are you getting?

    Also for further implementation and I quote "Counts all elements in an array, or something in an object." so just calling

    COUNT($row) { // If it is true or not will not work
    

    I suspect you have to explicitly state if the count is == 1 then yes we did get a return on the query let the person proceed.

    Alright I will also expand on this topic a bit more but from a CI standpoint!

    I suspect that you are not checking if you session is set either... and what I recommend is to create a MY_Controller situation. It is very easy and will reduce trouble for for...

    take a look at this code please :

    <?php
    if (!defined('BASEPATH'))
        exit('No direct script access allowed');
    
    class MY_Controller extends CI_Controller { // Create your custom controller
        public function __construct() {
            parent::__construct();
        }
    }
    

    Then in the same file do the following :

    class MY_logincontroller extends MY_Controller { // Extend the class you created above

    public function __construct() { 
        parent::__construct();
        $this -> load -> model('login_model'); // Load your login model
        $loggedin = $this -> session -> userdata('loggedin'); // set your user information
        $username = $this -> session -> userdata('username'); // Set your user information
    
        if ((!isset($loggedin)) || ($loggedin != TRUE)) { // This will check if the the session variable is set or if it is equal to logged in, if not it will send to a access denied page
            $this -> session -> unset_userdata('loggedin'); // If access denied happens unset user data logged in (just to be sure) and destroy all other session stuff
            $this -> session -> sess_destroy();
            $this -> load -> view('messages/accessdenied_view');
            $this -> output -> _display();
            die();
        }
        $this -> output -> set_header('Last-Modified: ' . gmdate("D, d M Y H:i:s") . ' GMT');
        $this -> output -> set_header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
        $this -> output -> set_header('Pragma: no-cache');
        $this -> output -> set_header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
    }
    

    Hopefully this clears some stuff up for you

    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效