Welcome Guest, Not a member yet? Register   Sign In
Maintaining the same controller
#1

[eluser]ReSTe[/eluser]
Hi guys,

i've a little problem. In the index function of my controller i show the view page that permits to do the login. After the login the parameters are passed to a controller function that saves the user in a session etc.etc. The problem is when i call others function of the controller it seems that codeigniter will create another istance of the controller and all the previous declared data (objects and session variables) are lost. why?

for example in a simply login form, the action of the form is verificalogin/

verificalogin is a function of my controller...

where i'm wrong?

thanks a lot, and forgive my poor english Smile
#2

[eluser]Michael Wales[/eluser]
Make your form processor your index controller and add in all of your validation there. If the validation is good - add the session information, if not, make them try again. Review the code below (not a terribly secure solution, but just for learning):

Controller: user.php
Code:
<?php
  class User extends Controller {
    function Users() {
      parent::Controller();
    }

    function index() {
      // Check if the user is already logged in
      if ($this->session->userdata('username')) {
        // They are - they need to be at their control panel
        redirect('user/cpanel');
      }
      // The user wasn't logged in - so let's present a login form
      $this->load->library('validation');
      $rules['username'] = 'trim|required|callback__login_check';
      $rules['password'] = 'trim|required';
      $this->validation->set_rules($rules);
      
      $fields['username'] = 'username';
      $fields['password'] = 'password';
      $this->validation->set_fields($fields);

      if ($this->validation->run()) {
        // They logged in and passed
        $this->session->set_userdata(array('username'=>$this->input->post('username')));
        redirect('user/cpanel');
      } else {
        // Bad login
        $this->load->view('login');
      }
    }

    function _login_check($username) {
      // Check to see if the username/password combination are correct
      // Our password are stored encrypted in the database thanks to CI's security class
      // We'll use that same class for comparison
      $this->load->helper('security');
      $password = dohash($this->validation->password);
      $query = $this->db->getwhere('users', array('username'=>$username, 'password'=>$password), 1, 0);
      if ($query->num_rows() > 0) {
        // That's a good username/password combo
        return TRUE;
      } else {
        $this->validation->set_message('_login_check', 'Incorrect username/password');
        return FALSE;
      }
    }
  }
?>

View: login.php
Code:
<html>
<body>
  <?= $this->validation->error_string; ?>
  <?= form_open('user'); ?>
    <label for="username">Username:</label><br />
    &lt;input type="text" name="username" id="username" /&gt;<br />
    <label for="password"Password:</label><br />
    &lt;input type="password" name="password" id="password" /&gt;<br />
    &lt;input type="submit" value="Login" /&gt;
  &lt;?= form_close(); ?&gt;
&lt;/body&gt;
&lt;/html&gt;




Theme © iAndrew 2016 - Forum software by © MyBB