无法在代码编写器的第二个函数中获取会话内容


Unable to get session contents in second function in codeigniter

我在codeigniter中有会话问题。

我已经使用会话库。所以我把username存储在session中。在Login函数中,我能够存储和检索会话数据。但是在这个函数中的第二个函数是GetUserName,我无法从会话中检索userName

有人知道我在这里做错了什么吗?

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
header('Access-Control-Allow-Origin: *');
class LoginController extends CI_Controller {
    function __construct() {
        parent::__construct();
        $this->load->helper('url');
        $this->load->library('session');
    }
    public function index() {
        //$this->load->view('welcome_message');
            $this->load->view('login');
    }
    public function Login() {   
        $UserName=$this->input->post('UserName');
        $Password=$this->input->post('Password');
        $this->session->set_userdata('UserName',$UserName);
        echo $Name=$this->session->userdata('UserName');
    }
    public function Log() {
        $this->load->view('welcome_message');
    }
    public function GetUserName() {
        echo $U_Name=$this->session->userdata('UserName');
    }
}

尝试以下操作:

设置会话:

$UserName=$this->input->post('UserName');
$newdata = array('username'  => $UserName);
$this->session->set_userdata('login_user_data',$newdata);

从会话中检索值:

$this->session->userdata['login_user_data']['username'];