php 从会话代码点火器中回显用户

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14336122/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:05:33  来源:igfitidea点击:

echo user in view from sessions code igniter

phpcodeignitersessionsession-variablesloginview

提问by user1972143

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

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

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 ..

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

回答by cartina

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

回答by channasmcs

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

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

view file

查看文件

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

回答by Jo?o Dias

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

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

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

And retrieve it just like that

并像那样取回它

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