简体   繁体   English

Codeigniter-PHP会话有问题

[英]Codeigniter - PHP something wrong with Session

I was surfing the net and found official page : 我正在网上冲浪,找到了官方页面:

With Adding Custom Session Data paragraph 带有添加自定义会话数据段落

https://www.codeigniter.com/user_guide/libraries/sessions.html https://www.codeigniter.com/user_guide/libraries/sessions.html

I tried implement the code to mine but it still doesnt work: 我尝试实现该代码以进行挖掘,但仍然无法正常工作:

public function login_validation(){
        $data = new stdClass();
        $this->load->database(); // load database
        $this->load->library('form_validation');
        $this->load->helper('url');
        $this->load->library('session');

        $this->form_validation->set_rules('email', 'email', 'required|trim|callback_validate_credentials');
        $this->form_validation->set_rules('password', 'password', 'required|trim', array(
            'required'      => 'Password field is empty'));

        $this->load->model("model_users");
        $email = $this->input->post('email');
        $user_id = $this->model_users->get_user_id($email);
        $user    = $this->model_users->get_user($user_id);

        if ($this->form_validation->run()){
            $newdata = array(
                   'username'  => 'johndoe',
                   'email'     => 'johndoe@some-site.com',
                   'logged_in' => TRUE
               );
            // $_SESSION['user_id']      = (bool)true;
            // $_SESSION['email']      = $this->input->post('email');
            // $_SESSION['logged_in']    = (bool)true;
            // $_SESSION['username']     = (string)$user->priv;
        $this->session->set_userdata($newdata);
        $this->members();
        } else{
            $this->login();
        }
    }

Im getting this error in this line - <?php echo $_SESSION['username']; ?> 我在此行收到此错误- <?php echo $_SESSION['username']; ?> <?php echo $_SESSION['username']; ?> : <?php echo $_SESSION['username']; ?>

A PHP Error was encountered

Severity: Notice

Message: Undefined index: username

Filename: views/catalog.php

Line Number: 19

Backtrace:

I dont see any mistake and Im really anxious right now , because nothing is working. 我没有看到任何错误,而且我现在真的很着急,因为没有任何反应。 Sorry for bad formulation of question. 很抱歉提出问题的方式不好。 If you need extra code lines just say I will paste it in pastebin.com 如果您需要额外的代码行,请说我将其粘贴在pastebin.com中

Setting new session 设置新会话

$newdata = array(
   'username'  => 'johndoe',
   'email'     => 'johndoe@some-site.com',
   'logged_in' => TRUE
);

$this->session->set_userdata($newdata); # setting session data

Retrieving the session 检索会话

$username = $this->session->userdata('username');

echo $username;

In view 视野中

echo $this->session->userdata('username');

Note: 注意:

If you want to passing the session data to view you can bind it with $data[] or in view can call it directly . 如果要传递会话数据以查看, 可以将其与$data[]绑定,或者在view中可以直接调用它

Since PHP native session are not implemented in CI:2 so you can't use $_SESSION in codeigniter 2 rather you can use 由于PHP本机会话未在CI:2中实现,因此您不能在codeigniter 2中使用$ _SESSION,而可以使用

$this->session->set_userdata('key','data');
//to fetch this session you can do
$this->session->userdata('key');

But due to some reason you need to implement PHP native session you can add this as a library Check library here 但是由于某种原因,您需要实现PHP本机会话,因此可以将其添加为库Check库(此处)

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

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