0

我是codeigniter的新手。我已经实现了一个简单的登录系统。我想在我的视图页面上打印出一个用户名,该用户名存储在会话中。这是我的控制器

class LoginController extends CI_Controller {

function index(){           
        $new['main_content'] = 'loginView';
        $this->load->view('loginTemplate/template', $new); 

    }   

    function verifyUser(){          
        //getting parameters from view 
        $data = array(
                'username' => $this->input->post('username'),
                'password' => $this->input->post('password')
        );          

        $this->load->model('loginModel'); 
        $query = $this->loginModel->validate($data);   

              if ($query){             
            //if the user c validated
            //data variable is created becx we want to put username in session
                $data = array(
                    'username' => $this->input->post('username'),
                     'is_logged_in' => true 
                );

               $this->session->set_userdata($data);
             redirect('sessionController/dashboard_area');
        }
        else
         {
            $this->index();
        }
    }
    function logout()
    {
        $this->session->sess_destroy();
        $this->index();
    }
    }
    ?>

会话控制器

<?php

          class SessionController extends CI_Controller {

function __construct()
{
    parent::__construct();
    $this->is_logged_in();

}



function dashboard_area(){


    $data['main_content'] = 'dashboardView';
    $this->load->view('dashboardTemplate/template', $data);

}

function is_logged_in()
{
    $is_logged_in = $this->session->userdata('is_logged_in');
    if(!isset($is_logged_in) || $is_logged_in != true)
    {
        echo 'You don\'t have permission to access this page.';
        die();
        //$this->load->view('login_form');
    }else {

    return true;

    }
}


       }
  ?>

我如何在会话中存储用户名,然后在页面中打印出来......我不想在每个控制器中保存用户名

如果有人有更好的实施建议,请分享给我..

4

3 回答 3

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

//Pass it in an array to your view like
$data['username']=$username;   
$this->load->view('test',$data);

//Then in you view you can display it as:
 echo $username;
于 2013-01-15T11:01:28.467 回答
1

控制器文件 $data = array('username' => $result->name, );

查看文件

           <?php echo $this->session->userdata('username')?> 
于 2014-01-17T12:17:08.297 回答
0

只需将用户名存储在用户数据中(您已经这样做了!)

$this->session->set_userdata('username') = 'John Doe';

并像那样检索它

$username = $this->session->userdata('username');
于 2013-01-15T10:57:41.270 回答