【问题标题】:how to store id or any value in session variable and call session in all pages using ci?如何在会话变量中存储 id 或任何值并使用 ci 在所有页面中调用会话?
【发布时间】:2017-07-04 06:47:48
【问题描述】:

控制器: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');
        }
    }

查看: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 存储到会话变量中,通过它我可以在我的所有页面上发出欢迎消息。那么,我该怎么做呢?请帮帮我。

谢谢

【问题讨论】:

标签: php codeigniter


【解决方案1】:

改变这一行

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

【讨论】:

  • 如何在另一个页面中调用 $student_id @poonam
  • 在视图中传递$data。直接使用$student_id
  • 它显示错误文件名:student-dashboard/index.php 行号:2 回溯:文件:/var/www/html/collegescan_ci/application/views/student-dashboard/index.php 行: 2 函数:_error_handler 文件:/var/www/html/collegescan_ci/application/controllers/Test.php 行:711 函数:查看 文件:/var/www/html/collegescan_ci/index.php 行:315 函数:require_once跨度>
  • 您正在使用redirect('/test/student', $data); 重定向页面。
  • 你应该在/test/student获取会话数据
【解决方案2】:

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

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

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

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

$this-&gt;load-&gt;library('session');

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

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

【讨论】:

    【解决方案3】:

    您可以根据需要将数据存储为像此更改字段一样的数组

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

    并检索数据,如

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-03-12
      • 2020-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-02
      • 1970-01-01
      相关资源
      最近更新 更多