title: codeigniter session lost when refresh (F5)
body: <p>First of all i'm new for codeigniter. All i want to ask is, why every refresh my session data is lost. i redirect to login.</p>
  
  <p>Here is my code.</p>
  
  <p>login controller</p>
  
  <pre><code>&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  
  class Login extends CI_Controller {
  
      function __construct()
      {
          parent::__construct();
      }
  
      function index()
      {
          $this-&gt;load-&gt;helper('form');
          $this-&gt;load-&gt;view('login');
      }
  
  }
  
  ?&gt;
  </code></pre>
  
  <p>verifylogin</p>
  
  <pre><code>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('headerx');
      //redirect('login', 'refresh'); 
      $this-&gt;load-&gt;view('login');
    }
    else {
      //Go to private area
      redirect('home');
    }
  
  }
  
  function check_database($password) {
    //Field validation succeeded.  Validate against database
    $username = $this-&gt;input-&gt;post('username');
  
    //query the database
    $result = $this-&gt;user-&gt;login($username, $password);
  
    if($result) {
      $sess_array = array();
      foreach($result as $row) {
        $sess_array = array(
          'id' =&gt; $row-&gt;id,
          'username' =&gt; $row-&gt;username
        );
        $this-&gt;session-&gt;set_userdata('logged_in', $sess_array);
      }
      return TRUE;
    }
    else {
      $this-&gt;form_validation-&gt;set_message('check_database', 'Invalid username or password');
      return false;
    }
  }
  ?&gt;
  </code></pre>
  
  <p>home controller</p>
  
  <pre><code>function index()
  {
    if($this-&gt;session-&gt;userdata('logged_in')) {
      $session_data = $this-&gt;session-&gt;userdata('logged_in');
      $data['username'] = $session_data['username'];
      $this-&gt;load-&gt;view('home', $data);
    }
    else {
      //If no session, redirect to login page
      // print_r($this-&gt;session-&gt;all_userdata()); 
      //die();
      redirect('login');
    }
  }
  </code></pre>
  
  <p>first login it is success, but when i refresh (f5) it is back to login form.
  my opinion its because session is lost.
  thanks for any help... </p>
  
  <p><strong>EDIT AND SOLVED :</strong></p>
  
  <p>sorry for inconvenience, this problem happen because </p>
  
  <pre><code>&lt;script&gt;
      function goOut(){
          &lt;?php $this-&gt;session-&gt;unset_userdata('logged_in');?&gt;   
          window.location="&lt;?php echo base_url()?&gt;";    
      }
  &lt;/script&gt;
  </code></pre>
  
  <p>my purpose it is when click button logout, then session is unset, but if i input in the header, session will be lost again, so i will find another way. thank you for your attentions and help.</p>