Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
209 views
in Technique[技术] by (71.8m points)

php - How to save and extract session data in codeigniter

I save some data in session on my verify controller then I extract this session data into user_activity model and insert session data into activity table. My problem is only username data saved in session and I can get and insert only username data after extracting session on model. I am new in Codeigniter. For this reason It’s very difficult to find out the problem. I am trying several days finding the problem. But unfortunately I can’t. So please, anyone help me. Thanks

VerifyLogin controller:

<?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 model:

    <?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;
       }
      }
    }
    ?>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can set data to session simply like this in 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
));

and you can get it like this:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...