如何在会话变量中存储id或任何值,并使用ci在所有页面中调用会话?

时间:2017-07-04 06:47:49

标签: php codeigniter

controller:test.php

<?php
    defined('BASEPATH') OR exit('No direct script access allowed');
    class Test extends CI_Controller 
    {
        function __construct() 
        {
            parent :: __construct();
            $this->load->helper(array('form', 'url', 'captcha', 'email'));
            $this->load->model('Fetch_data');
        }
        public function student()
        {
            $this->load->view('student-dashboard/index');
        }
    }

view:login.php

<?php
    if($this->input->post('login'))
    { 
        $email2 = $this->input->post('email2');
        $password2 = $this->input->post('password2');

        $this->db->select('email,password');
        $this->db->from('students');
        $where = "email='$email2' and password = '$password2'";
        $this->db->where($where);
        $query = $this->db->get();
        $result = $query->result_array();
        $num = $query->num_rows();
        if($num >'0')
        {
            $this->db->select('email,password,student_id');
            $this->db->from('students');
            $where = "email='$email2' and password = '$password2'";
            $this->db->where($where);
            $query = $this->db->get();
            $result = $query->result_array();

            $data['student_id'] = $this->session->set_userdata('student_id',$result);
            if($result == true)
            {
                redirect('/test/student', $data);
            }
        }
        else
        {
            echo "<p style='color: red;font-weight: bold;'>Wrong email id or password! </p>";
        }
    }
?>
<form method="post">
    <div class="form-group">
        <input type="text" name="email2" id="email2" placeholder="Enter Your Email" class="text-line"/>
    </div>
    <div class="form-group">
        <input type="password" name="password2" id="password2" placeholder="Enter Your Password" class="text-line"/>
    </div>
    <div class="form-group">
        <input type="submit" name="login" id="login" value="Login" class="btn btn-warning"/>
    </div>
</form>

我是codeigniter的新手。在这段代码中,我正在创建登录表单,它完美地运行。现在,我想将student_id存储到会话变量中,通过该变量我可以在所有页面上发送欢迎消息。那么,我该怎么办呢?请帮帮我。

谢谢

3 个答案:

答案 0 :(得分:0)

更改此行

$data['student_id'] = $this->session->set_userdata('student_id',$result['student_id']);

到这一个:

$this->session->set_userdata('student_id',$result['student_id']);
$data['student_id'] = $this->session->userdata('student_id');

您还必须加载session

$this->load->library('session');

答案 1 :(得分:0)

如果您想添加/创建会话数据:

$data = array(
        'student_id'  => 'id',
        'name'     => 'name',
        <!-- Your parameters -->
);

$this->session->set_userdata($data); 

为了使用Session库,你应该在你需要的每个控制器的构造函数方法中加载它:

$this->load->library('session');

通过执行此操作,会话变量将可用于您在此控制器中加载的所有视图。 在您的视图中,您可以通过以下方式发送欢迎信息:

<?php echo "Hello ".$this->session->userdata('student_id');?>
无论如何,我建议你将你的PHP代码从视图(login.php)移动到控制器(test.php)。它简化了你的工作

答案 2 :(得分:0)

您可以根据需要将数据存储为数组,例如此更改字段

SET autocommit = 0;
LOCK TABLES ad_banner_queue AS ad_banner_queue_w WRITE, ad_banner_queue AS ad_banner_queue_r READ;
... perform your select query on ad_banner_queue_r, then update that row in ad_banner_queue_w with is_sent = 1...
COMMIT;
UNLOCK TABLES;

并检索数据

$session = array(
                    'isLoggedIn' => TRUE,
                    'username' => $result['username'],
                    'roleid' => $result['roleid'],
                    'id' => $result['id']
                );
                $this->session->set_userdata($session);