EllisLab text mark
Advanced Search
Maintaining the same controller
Posted: 25 July 2007 12:19 PM
Joined: 2007-07-18
9 posts

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

Posted: 31 July 2007 01:46 PM   [ # 1 ]   [ Rating: 0 ]
Joined: 2007-07-30
2144 posts

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

      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), 10);
      if (
$query->num_rows() > 0{
// That's a good username/password combo
return TRUE;
else {
->validation->set_message('_login_check''Incorrect username/password');

View: login.php

form_open('user'); ?>
<label for="username">Username:</label><br />
input type="text" name="username" id="username" /><br />
label for="password"Password:</label><br />
input type="password" name="password" id="password" /><br />
input type="submit" value="Login" />
<?form_close(); ?>

Follow me on twitter here.
MichaelWales.com | MichaelWales.info