echo user in view from sessions code igniter

孤者浪人 提交于 2019-12-02 20:08:40

问题


I am new in codeigniter. I have implemented a simple login system. I want to print out a username on my view page which is stored in sessions. here is my controller

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();
    }
    }
    ?>

sessionController

<?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;

    }
}


       }
  ?>

how can i stored a username in sessions and then print out in a page ... i dont want to save username in every controller

if any one have a better suggestions to implement then please share it to me ..


回答1:


$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;



回答2:


controller file $data = array( 'username' => $result->name, );

view file

           <?php echo $this->session->userdata('username')?> 



回答3:


Just store the user name in the userdata (you've already done this!)

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

And retrieve it just like that

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


来源:https://stackoverflow.com/questions/14336122/echo-user-in-view-from-sessions-code-igniter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!