CodeIgniter Forums

Full Version: session data issues
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]john.weland[/eluser]
I pulled this from my post over at phpacademy and poseted it here as well as I seemingly am getting no hits over there.


So I am following along with the phpacademy - codeigniter video tutorials for creating a user registration & login. I am currently on video 7 which deals with session data.

here is the link to the video => http://www.youtube.com/watch?v=wpUqyTRxb...0E6A01B1E0

All was going well until I set the session data to display in the members page, at that point my data displayed just as it does in the video minus the email and is_logged_in.

in the video it shows as

Array(
[session_id] => ---------------------
[ip_address]=> ------------------
[user_agent]=> ---------------------
[last_activity] => -------------------
[user_data] =>
[email] => -------------------------
[is_logged_in] => 1
)


mine has shown as

Array(
[session_id] => ---------------------
[ip_address]=> ------------------
[user_agent]=> ---------------------
[last_activity] => -------------------
[user_data] =>
)


right then I knew something fishy was going on but I continued anyway with the video doing the restricted.php so now I login (with valid credentials) but still receive the redirect to the restricted page. I can only assume this is because my in my session data my [is_logged_in] is not => 1

I have gone over my code a hundred times and from what I see it matches up with the video tutorial but something obviously is incorrect.

from my main controller
Code:
public function members() {
  if ($this->session->userdata('is_logged_in')) {
   $this->load->view('members');
  } else {
   redirect('main/restricted');
  }
}

public function restricted() {
  $this->load->view('restricted');
}

public function login_validation() {
  $this->load->library('form_validation');
  
  $this->form_validation->set_rules('email', 'Email', 'required|trim|xss_clean|callback_validate_credentials');
  $this->form_validation->set_rules('password', 'Password', 'required|md5|trim');
  
  if ($this->form_validation->run()){
   $data = array(
    'email' => $this->input->post('email'),
    'is_logged_in' => 1
   );
  
   $this->session->set_userdata($data);
   redirect('main/members');
  } else {
   $this->load->view('login');
  }

}


from my members page
Code:
<!DOCTYPE html>
&lt;html lang="en"&gt;
&lt;head&gt;
&lt;meta charset="utf-8"&gt;
&lt;title&gt;Members Page&lt;/title&gt;

&lt;/head&gt;
&lt;body&gt;

<div id="container">
<h1>Members Page</h1>

&lt;?php

echo "<pre>";
print_r($this->session->all_userdata());
echo "</pre>";

?&gt;

</div>

&lt;/body&gt;
&lt;/html&gt;




As always any help is appreciated (im sure its something obvious but bare with me I 'm a noob. ) Thanks in advance

El Forum

[eluser]adamck[/eluser]
In my Login Script.. i set the following user data.

Login Controller Function
Code:
$data = array(
  'username' => $this->input->post('username'),
  'is_logged_in' => true
  );
$this->session->set_userdata($data);

Then in the other controllers i have this

Code:
function index()
{
  $this->is_logged_in();
  // DO YOUR STUFF HERE, IT WILL ONLY RUN IF THE USER IS LOGGED IN
}

function is_logged_in()
{
  $is_logged_in = $this->session->userdata('is_logged_in');
  
                // IF USER IS NOT LOGGED IN REDIRECT TO ANOTHER PAGE
  if(!isset($is_logged_in) || $is_logged_in != TRUE)
  {
   redirect('error/not_logged_in');
  }
}

If you want an entire controller to only work for logged in users you can put the $is_logged_in function at the bottom and then the the $this->is_logged_in(); can go in the page construct
Code:
function __construct()
{
  parent::__construct();
                $this->is_logged_in();
}

hope it helps

El Forum

[eluser]john.weland[/eluser]
Well I couldn't figure it out so I again scrapped the project and started over. I got all the way back to this section about session data sending it and displaying it. It showed the same as before

where is should show

Array
(
[session_id] => e34d75caa00ad6c34890c6ac118904ff
[ip_address] => browsing clients IP address
[user_agent] => Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.96 Safari/537.4
[last_activity] => 1352976703
[user_data] =>
[email] => [email protected]
[is_logged_in] => 1
)



it instead shows

Array
(
[session_id] => e34d75caa00ad6c34890c6ac118904ff
[ip_address] => 192.168.6.1
[user_agent] => Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.96 Safari/537.4
[last_activity] => 1352976703
[user_data] =>
)




I am on a crazy network here at home my main router is 192.168.1.1 connected to that wirelessly is a router 192.168.2.1 the server is on my PC as a virtual machine running a hypervisor running a virtual server.


I noticed rather than showing [ip_address] => 192.168.2.120 (the system I am accessing from ) ts shows the [ip_address] =>192.168.6.1 this ip address is the default gateway of my virtual host running the virtual server.

unfortunately I haven't a clue how to fix this issue.

El Forum

[eluser]skunkbad[/eluser]
What browser are you using, and what are your session/cookie related config settings?

Also, a large percentage of session related issues that I see here in the forum involve setting session data immediately before a redirect. I don't know if that's your problem, but it's an observation.

El Forum

[eluser]john.weland[/eluser]
I tested my hypothisis out on one ofmy old junker PCs here and I was right something about my virtual server being on a different network has made the session show from the default gateway rather than my actual IP when I set this up on my junker on the non-virtual is ran through fine and even gave the correct IP



My browser is Chrome

cookie/session settings. I am not sure, whatever codeigniter has set as default.

El Forum

[eluser]john.weland[/eluser]
update. I got my linux server on the same routing at my main machine and my windows server.... using the same files copied from one box to the next. windows box works flawlessly, linux box still doesn't show the session data its like its not carrying the data ...