CodeIgniter 会话用户日期似乎不存在

发布于 2024-12-29 10:59:56 字数 2499 浏览 1 评论 0原文

我正在编写我的第一个 CI 项目并尝试编写登录脚本。一切工作几乎正常,除了会话用户数据不可用(即使我在 Firefox 中检查我的 cookie/会话也不可用)。

我不明白为什么会话用户数据仅在登录后可用,但如果我再次加载同一页面(不是刷新,而是新加载),我希望我仍然会登录,但我已注销?即使我尝试读取会话用户数据,它也不存在。

我将脚本简化为 stackoverflow 的示例版本。谁能告诉我如何解决这个会话问题?

问候, 吉多

<?php
class Test extends CI_Controller
{   

    function index()
    {
        $logged_in = $this->is_logged_in();
        if($logged_in) { 
            echo "You are logged in. <a href='test/logout'>Logout</a> | <a href='../'>Return to index</a>"; 
        }
        else { 
            echo "You are logged out";
            echo form_open('test/check_login');
            echo "Email: ".form_input('email', set_value('email'));
            echo "Password: ".form_password('password', set_value('password'));
            echo form_submit('submit','Login');
            echo form_close();
        }
    }

    function is_logged_in() // check if user has logged in
    {
        // AUTOLOAD SESSIONS HAS SET in autoload.php-config >> $autoload['libraries'] = arra y('database', 'session', 'email');
        $is_logged_in = $this->session->userdata('is_logged_in');       
        if($is_logged_in) { 
            return TRUE; 
        }
        else { 
            return FALSE; 
        }
    }

    function check_login()
    {
        $client_id = $this->validate();
        if(is_numeric($client_id)) // if the user's credentials validated then user exists
        {
            $data = array(
                'client_id' => $client_id,
                'is_logged_in' => true
            );
            $this->session->set_userdata($data);
        }       
        $this->index();
    }

    // normally we put this function in a model, but for this example we put it here.
    function validate() // check if user exists in database
    {
        $this->db->where('email', $this->input->post('email'));
        $this->db->where('password', md5($this->input->post('password')));
        $query = $this->db->get('client_users'); // this is our user table

        if($query->num_rows == 1) // user exists
        {
            $row = $query->row();
            return $row->id_client;
        }
        else
        {
            return false;
        }
    }

    function logout()
    {
        $this->session->sess_destroy(); // kill session, so user will be logged out.
        redirect('/test');
    }

}
?>

I'm coding my first CI project and try to write a loginscript. Everything works almost fine, except that the session userdata is not available (even not if i check my cookies / sessions in Firefox).

I don't understand why the session userdata only are available after login, but if i load the same page again (not a refresh, but a new load) i would expect i still will be logged in, but i'm logged out ? Even if i try to read the session userdata it doesn't exists.

I simplified my script to an example version for stackoverflow. Who can tell me how this session issue can be solved?

Regards,
Guido

<?php
class Test extends CI_Controller
{   

    function index()
    {
        $logged_in = $this->is_logged_in();
        if($logged_in) { 
            echo "You are logged in. <a href='test/logout'>Logout</a> | <a href='../'>Return to index</a>"; 
        }
        else { 
            echo "You are logged out";
            echo form_open('test/check_login');
            echo "Email: ".form_input('email', set_value('email'));
            echo "Password: ".form_password('password', set_value('password'));
            echo form_submit('submit','Login');
            echo form_close();
        }
    }

    function is_logged_in() // check if user has logged in
    {
        // AUTOLOAD SESSIONS HAS SET in autoload.php-config >> $autoload['libraries'] = arra y('database', 'session', 'email');
        $is_logged_in = $this->session->userdata('is_logged_in');       
        if($is_logged_in) { 
            return TRUE; 
        }
        else { 
            return FALSE; 
        }
    }

    function check_login()
    {
        $client_id = $this->validate();
        if(is_numeric($client_id)) // if the user's credentials validated then user exists
        {
            $data = array(
                'client_id' => $client_id,
                'is_logged_in' => true
            );
            $this->session->set_userdata($data);
        }       
        $this->index();
    }

    // normally we put this function in a model, but for this example we put it here.
    function validate() // check if user exists in database
    {
        $this->db->where('email', $this->input->post('email'));
        $this->db->where('password', md5($this->input->post('password')));
        $query = $this->db->get('client_users'); // this is our user table

        if($query->num_rows == 1) // user exists
        {
            $row = $query->row();
            return $row->id_client;
        }
        else
        {
            return false;
        }
    }

    function logout()
    {
        $this->session->sess_destroy(); // kill session, so user will be logged out.
        redirect('/test');
    }

}
?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你的呼吸 2025-01-05 10:59:56

您的验证函数的 if 语句应为:

if($query->num_rows() == 1) // user exists

您在 num_rows 之后遗漏了 ()。

编辑:经过进一步审查,这应该不重要。我唯一能告诉你的另一件事是,也许你的结果不等于 1。要么找不到用户,要么你得到多个结果,两者都会导致 !== 1。

Your validate function's if statement should read:

if($query->num_rows() == 1) // user exists

You left out the () after num_rows.

Edit: After further review, that shouldn't matter. The only other thing I can tell is maybe your result is not equal to 1. Either the user isn't found or you're getting more than one result, both resulting in !== 1.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文