如何使用codeigniter回显会话数据以查看配置文件或帮助我通过会话传递id以获取用户配置文件
控制器
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class User extends CI_Controller
{
function __construct()
{
// Call the Model constructor
parent::__construct();
$this->load->database();
$this->load->library('session');
$this->load->helper('form');
$this->load->library('form_validation');
$this->load->model('User_Model');
$this->load->helper('url');
$this->load->library('parser');
}
public function index()
{
$this->load->view('login');
}
public function register()
{
$this->load->view('Register');
}
public function Save()
{
$this->form_validation->set_rules('email', 'email', 'required');
if ($this->form_validation->run() == false)
{
echo 'Please enter correct email.';
exit;
}
$this->form_validation->set_rules('password', 'password', 'required');
if ($this->form_validation->run() == false)
{
echo 'Please enter password.';
exit;
}
if ($this->form_validation->run() == true)
{
$email = $this->input->post('email');
$password = md5(trim($this->input->post('password')));
$res = $this->User_Model->Save($email,$password);
exit;
}
}
public function Login()
{
$this->form_validation->set_rules('email', 'email', 'required');
if ($this->form_validation->run() == false)
{
echo 'Please enter correct email.';
exit;
}
$this->form_validation->set_rules('password', 'password', 'required');
if ($this->form_validation->run() == false)
{
echo 'Please enter password.';
exit;
}
if ($this->form_validation->run() == true)
{
$email = $this->input->post('email');
$password = md5(trim($this->input->post('password')));
$res = $this->User_Model->login($email,$password);
echo $res;
exit;
}
}
public function dashboard()
{
$this->load->view('welcome');
}
public function logout()
{
$this->session->sess_destroy();
redirect(base_url().'index.php/User/');
}
}
?>
答案 0 :(得分:0)
使用原始php,您可以使用$_SESSION
在codeigniter内,您可以使用print_r($this->session->all_userdata());
答案 1 :(得分:0)
Codeigniter遵循MVC设计模型,这意味着数据首先由模块生成然后传递给控制器,最后控制器将数据膨胀到某个视图。
$this->load->view('view_name', $data, true/false);
在本例中,您将看到如何加载向其传递一些数据的视图。
假设您的视图文件夹中有一个名为show_session.php
的视图
我们可以在控制器中创建以下方法,以显示您希望用户能够看到的会话数据。
public function index()
{
$data['info'] = $this->session->all_userdata();
$this->load->view('show_session',$data);
}
如果您没有该视图,请使用以下内容创建该视图:
<!DOCTYPE html>
<html>
<head>
<title>Simple data</title>
</head>
<body>
<?php
foreach ($info as $key => $value) {
echo $key." => ".$value."<br>";
}
?>
</body>
</html>
最后将其保存到/application/views
文件夹中。
信用及更多信息:here