I have issue with session in codeigniter.
I have used session library. So I have stored username
in session. In Login
function I am able to store and retrieve session data. but in second function which is GetUserName
in this function I am not able to retrieve userName
from the session.
Does anyone have any ideas what I am doing wrong here?
<?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');
}
}
try the following:
to set the session use this:
$UserName=$this->input->post('UserName');
$newdata = array('username' => $UserName);
$this->session->set_userdata('login_user_data',$newdata);
to retrieve values from session use:
$this->session->userdata['login_user_data']['username'];