Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to save and extract session data in codeigniter
    text
    copied!<p>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</p> <p>VerifyLogin controller:</p> <pre><code>&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); session_start(); class VerifyLogin extends CI_Controller { function __construct() { parent::__construct(); $this-&gt;load-&gt;model('user','',TRUE); $this-&gt;load-&gt;model('user_activity','',TRUE); } function index() { //This method will have the credentials validation $this-&gt;load-&gt;library('form_validation'); $this-&gt;form_validation-&gt;set_rules('username', 'Username', 'trim|required|xss_clean'); $this-&gt;form_validation-&gt;set_rules('password', 'Password', 'trim|required|xss_clean|callback_check_database'); if($this-&gt;form_validation-&gt;run() == FALSE) { //Field validation failed. User redirected to login page $this-&gt;load-&gt;view('login_view'); } else { //Go to private area redirect('home', 'refresh'); } } function check_database($password) { //Field validation succeeded. Validate against database $username = $this-&gt;input-&gt;post('username'); $vercode = $this-&gt;input-&gt;post('vercode'); //query the database $result = $this-&gt;user-&gt;login($username, $password); // ip address $ip_address= $this-&gt;user_activity-&gt;get_client_ip(); //Retrieving session data and other data $captcha_code=$_SESSION['captcha']; $user_agent=$_SERVER['HTTP_USER_AGENT']; if($result &amp;&amp; $captcha_code == $vercode) { $sess_array = array(); foreach($result as $row) { $sess_array = array( 'username' =&gt; $row-&gt;username, 'user_agent' =&gt; $row-&gt;user_agent, 'ip_address' =&gt; $row-&gt;ip_address, ); $this-&gt;session-&gt;set_userdata('logged_in', $sess_array); //insert user activity $this-&gt;user_activity-&gt;activity(); } return TRUE; } else { $this-&gt;form_validation-&gt;set_message('check_database', 'Invalid username or password'); return false; } } } ?&gt; </code></pre> <p>user_activity model:</p> <pre><code> &lt;?php Class User_activity extends CI_Model { function activity() { if($this-&gt;session-&gt;userdata('logged_in')) { $session_data = $this-&gt;session-&gt;userdata('logged_in'); // $data['username'] = $session_data['username']; $data = array( 'session_id'=&gt;"", 'ip_address'=&gt;$session_data['ip_address'], 'user_agent'=&gt;$session_data['user_agent'], 'username'=&gt;$session_data['username'], 'time_stmp'=&gt;Now(), 'user_data'=&gt;$session_data['username']."Logged in Account" ); $this-&gt;db-&gt;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; } } } ?&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload