• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Maintaining the same controller

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

[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
  class User extends Controller {
    function Users() {

    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
      // The user wasn't logged in - so let's present a login form
      $rules['username'] = 'trim|required|callback__login_check';
      $rules['password'] = 'trim|required';
      $fields['username'] = 'username';
      $fields['password'] = 'password';

      if ($this->validation->run()) {
        // They logged in and passed
      } else {
        // Bad 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
      $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
  <?= $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;

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.