我在登录仪表板后重定向用户,在仪表板标题上,我想显示我在会话中存储的用户名。但是在标题上,它让我注意到了,
Undefined index userData
我在我的应用程序中设置了模板结构。
控制器:
<?php
class Admin extends CI_Controller {
private $template;
private $header;
private $footer;
private $sidebar;
private $content;
public $userData;
function __construct() {
parent::__construct(); //call to parent constructor
$this->data = "";
$this->header = $this->load->view('admin/header', $this->userData, TRUE);
$this->template = $this->load->view('admin/template', $this->userData, TRUE);
$this->footer = $this->load->view('admin/footer', $this->userData, TRUE);
$this->load->helper('url');
// $this->loginModel = $this->load->model('admin/loginModel');
session_start();
}
/**
* will show the dashborad
* index function default function of Admin Controller
*/
public function index() {
if ($this->session->userdata('id')) {
$this->userData = $this->session->userdata;
$data['userData'] = $this->userData;;
$data['header'] = $this->header;
$data['footer'] = $this->footer;
$data['content'] = $this->load->view('admin/content', $data, TRUE);
$data['sidebar'] = $this->load->view('admin/sidebar', $data, TRUE);
$this->load->view('admin/template', $data);
} else {
$this->login();
}
}
/**
* This is the login function of a controller
*/
public function login() {
$data = '';
if (isset($_POST['submit'])) {
$username = '';
$password = '';
$errors = '';
$username = mysql_real_escape_string($this->input->post('username'));
$password = $this->input->post('password');
if ($username == '') {
//echo 'dasd';
$data['username_error'] = 'Username is Required';
}
if ($password == '') {
//echo 'dasd';
$data['password_error'] = 'Password is Required';
}
if ($username == '' && $password == '') {
//echo 'dasd';
$data['password_error'] = '';
$data['username_error'] = '';
$data['general_error'] = 'Please enter Username and Password to proceed';
}
if ($username != '' && $password != '') {
//varify username password
$hpassword = $password;
$this->load->model('admin/loginModel');
$row = $this->loginModel->verifyUser($username, $password);
echo "<pre>";
print_r($row);
if (!empty($row)) {
$adminSessionData = '';
foreach($row as $adminData) {
$adminSessionData['id'] = $adminData['pk_admin_id'];
$adminSessionData['username'] = $adminData['admin_username'];
$adminSessionData['email'] = $adminData['admin_email'];
$adminSessionData['status'] = $adminData['admin_status'];
}
//saving data in session
$this->session->set_userdata($adminSessionData);
redirect(base_url('admin'));
} else {
$data['general_error'] = 'Invalid Credentials';
}
}
}
$data['title'] = "Admin Panel Login";
$this->load->view('admin/login', $data);
}
/**
* will destroy session n logout user
*
*/
function logout() {
$userSessionData = array(
'user_id' => '',
'username' => '',
'email' => ''
);
$this->session->unset_userdata($userSessionData);
$this->session->sess_destroy();
session_destroy();
redirect(base_url('admin/login'));
exit;
}
}
?>
header.php :
<div class="btn-group user-menu">
<button type="button" class="btn btn-sm dropdown-toggle" data-toggle="dropdown"> <span class="glyphicons glyphicons-user"></span> <b><?php echo $userData['userData']['username']?></b> </button>
<button type="button" class="btn btn-sm dropdown-toggle padding-none" data-toggle="dropdown"> </button>
<ul class="dropdown-menu checkbox-persist" role="menu">
<li class="menu-arrow">
<div class="menu-arrow-up"></div>
</li>
<li class="dropdown-header">Your Account <span class="pull-right glyphicons glyphicons-user"></span></li>
<li>
<ul class="dropdown-items">
<li>
<div class="item-icon"><i class="fa fa-envelope-o"></i> </div>
<a class="item-message" href="messages.html">Messages</a> </li>
<li>
<div class="item-icon"><i class="fa fa-calendar"></i> </div>
<a class="item-message" href="calendar.html">Calendar</a> </li>
<li class="border-bottom-none">
<div class="item-icon"><i class="fa fa-cog"></i> </div>
<a class="item-message" href="customizer.html">Settings</a> </li>
<li class="padding-none">
<div class="dropdown-lockout"><i class="fa fa-lock"></i> <a href="screen-lock.html">Screen Lock</a></div>
<div class="dropdown-signout"><i class="fa fa-sign-out"></i> <a href="<?php echo base_url('admin/logout'); ?>">Sign Out</a></div>
</li>
</ul>
</li>
</ul>
</div>
答案 0 :(得分:1)
要获取视图中的会话数据,请尝试使用
if($this->session->userdata('username')!="")
{
echo $this->session->userdata('username');
}
答案 1 :(得分:0)
print_r($this->session->userdata);
OR
print_r($this->session->all_userdata());
OR
$this->session->userdata('first_name');
请参阅以下网址