简体   繁体   English

Codeigniter中的会话无法正常运行

[英]Session in codeigniter not working well

I can't figure it out what's the actual error with session in Codeigniter. 我无法弄清楚Codeigniter中会话的实际错误是什么。 My other project are working well with this code. 我的其他项目在此代码下运行良好。 But in this case, I got problem. 但是在这种情况下,我遇到了问题。

For login session my code is: 对于登录会话,我的代码是:

 $data = array(
               'username' => $this->input->post('username'),
               'admin_logged_in' => true,
               'logged_in' =>true

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

And I check session as: 我将会话检查为:

 if ($this->session->userdata('admin_logged_in') == true) { 
     // code
    }

Before going to controller I am checking session like above and if session is not true then redirecting to login page. 在进入控制器之前,我正在像上面那样检查会话,如果会话不正确,则重定向到登录页面。

For logout: 对于注销:

 function logout() {
        if ($this->session->userdata('admin_logged_in') == TRUE) {
            $useremail = $this->session->userdata('username');
        $data = array(
                'username' => $useremail,
                'admin_logged_in' => true,
                'logged_in' =>true
            );
        $this->session->unset_userdata($data);
        $this->session->sess_destroy();
        redirect('login');
     }
  }

But above code is not working for me. 但是上面的代码对我不起作用。 When I logout, page is successfully redirected to logout page but session is not destroyed since I can access all the page that needs session set. 注销时,页面已成功重定向到注销页面,但会话未销毁,因为我可以访问需要会话集的所有页面。 Please help me. 请帮我。

Updated 更新

I got an strange problem. 我有一个奇怪的问题。 When I log into the system and logout. 当我登录系统并注销时。 And trying to access the method that needs session. 并尝试访问需要会话的方法。 I can access if I wrote method name without index.php. 如果我编写的方法名称没有index.php,则可以访问。 And I cannot access method that needs session if I wrote method name with index.php. 如果我使用index.php编写方法名称,则无法访问需要会话的方法。 Example: BnW needs session to be set in order to go furthur. 示例:BnW需要设置会话才能继续。 If I login and logout then, If I write in borwser URL following: 如果我登录然后注销,那么,如果我在浏览器URL中写以下内容:

 example.com/index.php/bnw      //it redirects to login page.
 example.com/bnw                //it logs in directly.
  • Does removing index.php from .htaccess gives problem with session? 从.htaccess中删除index.php会给会话带来问题吗?

Put this inside your logout function, it works for me 将其inside your logout function,对我inside your logout function,

$user_data = $this->session->all_userdata();
        foreach ($user_data as $key => $value) {
            if ($key != 'session_id' && $key != 'ip_address' && $key != 'user_agent' && $key != 'last_activity') {
                $this->session->unset_userdata($key);
            }
        }
    $this->session->sess_destroy();

In config/autoload.php config/autoload.php

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

Your .htaccess should be (should place outside of application folder) 您的.htaccess应该是(应该放在应用程序文件夹之外)

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L] 
</IfModule>

in config/config.php config/config.php

if 2.0(line 247) 如果2.0(第247行)

$config['sess_cookie_name']     = 'ci_session';
$config['sess_expiration']      = 7200;
$config['sess_expire_on_close'] = FALSE;
$config['sess_encrypt_cookie']  = FALSE;
$config['sess_use_database']    = FALSE;
$config['sess_table_name']      = 'ci_sessions';
$config['sess_match_ip']        = FALSE;
$config['sess_match_useragent'] = TRUE;
$config['sess_time_to_update']  = 0;

if 3.0(line 358) 如果3.0(358行)

$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'] = 0;
$config['sess_regenerate_destroy'] = FALSE;

In controller 在控制器中

function logout()
{
    $result = $this->model_name->check_login();
    if ( $result == TRUE )
    {

        if(!empty($log))
        {
            $this->session->sess_destroy();
            $this->index();//your logging should be here
        }
        else
        {
            //some code here
        }
    }
    else
    {
        $this->index();
    }
}

In Model 在模型中

public function check_login()
{
    $log = $this->session->all_userdata();

    if (isset($log['admin_logged_in']))
    {
        if($log['admin_logged_in'] == true)
        {
            return true;
        }
        else
        {
            return false;
        }
    } else
    {
        return false;
    }
}

and each and every controller check whether session is set. 每个控制器检查是否设置了会话。 if set load the view, else direct to index function. 如果设置为加载视图,则直接指向索引函数。 So this will prevent user from direct access through URL 因此,这将阻止用户通过URL直接访问

sample code to check log in 检查登录的示例代码

$result = $this->model_name->check_login();
if($result==true)
    {
        //load your views as usual
    }
else
    {
        //load index function
        $this->index();
    }

Index function(in controller) 索引功能(在控制器中)

public function index()
    {
        $this->load->view('admin/logging');//load your loginf view here
    }

I think that in your logout() function, it doesn't go inside the if block so the session doesn't get destroyed at all 我认为在您的logout()函数中,它不会进入if块内,因此会话根本不会被破坏

in the documentation it says 在说明文件中

Note: The function returns FALSE (boolean) if the item you are trying to access does not exist. 注意:如果您尝试访问的项目不存在,则该函数返回FALSE(布尔值)。

it doesn't say it returns TRUE 它没有说它返回TRUE

so change 所以改变

if ($this->session->userdata('admin_logged_in') == TRUE)

to

if ($this->session->userdata('admin_logged_in') != FALSE)

and check your session if it is really destroyed after sess_destroy() 并在sess_destroy()之后检查会话是否真的被破坏了

 $this->session->sess_destroy();
 $admin_logged_in = $this->session->userdata('admin_logged_in');
 echo "-----------------> $admin_logged_in";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM