如何在代码图中保存和提取会话数据

2022-08-30 16:51:07

我在验证控制器上保存了一些会话中的数据,然后将此会话数据提取到user_activity模型中,并将会话数据插入到活动表中。我的问题只是会话中保存的用户名数据,在模型上提取会话后,我只能获取和插入用户名数据。我是Codeigniter的新手。因此,很难找出问题。我正在尝试几天找到问题。但不幸的是,我不能。所以,请任何人帮助我。谢谢

验证登录控制器:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
session_start();
class VerifyLogin extends CI_Controller {

 function __construct()
 {
   parent::__construct();
   $this->load->model('user','',TRUE);
   $this->load->model('user_activity','',TRUE);
  }

 function index()
 {
   //This method will have the credentials validation
   $this->load->library('form_validation');

   $this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
   $this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|callback_check_database');

   if($this->form_validation->run() == FALSE)
   {
     //Field validation failed.  User redirected to login page
     $this->load->view('login_view');
   }
   else
   {
     //Go to private area
     redirect('home', 'refresh');
   }
 }

 function check_database($password)
 {
   //Field validation succeeded.  Validate against database
   $username = $this->input->post('username');
   $vercode = $this->input->post('vercode');

   //query the database
   $result = $this->user->login($username, $password);

   // ip address
   $ip_address= $this->user_activity->get_client_ip();

   //Retrieving session data and other data
   $captcha_code=$_SESSION['captcha'];
   $user_agent=$_SERVER['HTTP_USER_AGENT'];

   if($result && $captcha_code == $vercode)
   {
     $sess_array = array();
     foreach($result as $row)
     {
       $sess_array = array(
         'username' => $row->username,
           'user_agent' => $row->user_agent,
           'ip_address' => $row->ip_address,
       );
       $this->session->set_userdata('logged_in', $sess_array);

        //insert user activity
       $this->user_activity->activity();
     }
     return TRUE;
   }
   else
   {
     $this->form_validation->set_message('check_database', 'Invalid username or password');
     return false;
   }
 }
}
?>

user_activity型号:

    <?php
    Class User_activity extends CI_Model
    {
     function activity()
     {
        if($this->session->userdata('logged_in'))
       {
         $session_data = $this->session->userdata('logged_in');
       //  $data['username'] = $session_data['username'];

           $data = array(
                  'session_id'=>"",
                  'ip_address'=>$session_data['ip_address'],
                  'user_agent'=>$session_data['user_agent'],
                  'username'=>$session_data['username'],
                  'time_stmp'=>Now(),
                  'user_data'=>$session_data['username']."Logged in Account"
                );
        $this->db->insert('user_activity',$data);        
       }
       else
       {
          return  false;
       }

       // Function to get the client ip address
    function get_client_ip() {
        $ipaddress = '';
        if ($_SERVER['HTTP_CLIENT_IP'])
            $ipaddress = $_SERVER['HTTP_CLIENT_IP'];
        else if($_SERVER['HTTP_X_FORWARDED_FOR'])
            $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
        else if($_SERVER['HTTP_X_FORWARDED'])
            $ipaddress = $_SERVER['HTTP_X_FORWARDED'];
        else if($_SERVER['HTTP_FORWARDED_FOR'])
            $ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
        else if($_SERVER['HTTP_FORWARDED'])
            $ipaddress = $_SERVER['HTTP_FORWARDED'];
        else if($_SERVER['REMOTE_ADDR'])
            $ipaddress = $_SERVER['REMOTE_ADDR'];
        else
            $ipaddress = 'UNKNOWN';

        return $ipaddress;
       }
      }
    }
    ?>

答案 1

您可以在Codeigniter中像这样简单地将数据设置为会话:

$this->load->library('session');
$this->session->set_userdata(array(
    'user_id'  => $user->uid,
    'username' => $user->username,
    'groupid'  => $user->groupid,
    'date'     => $user->date_cr,
    'serial'   => $user->serial,
    'rec_id'   => $user->rec_id,
    'status'   => TRUE
));

你可以这样得到它:

$u_rec_id = $this->session->userdata('rec_id');
$serial = $this->session->userdata('serial');

答案 2

首先,您有加载会话库。

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

您可以在自动加载中加载它,我认为这更好。

设置会话

$this->session->set_userdata("SESSION_NAME","VALUE");

提取数据

$this->session->userdata("SESSION_NAME");

推荐