CodeIgniter Forums

Full Version: Login
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2

El Forum

[eluser]Felipe Deitos[/eluser]
Hi everyone!

First of all, i am from Brazil and my english is actually bad... and i am not using the translator cuz i think it is worst than me hehe

I am a developer for some years, and i was reading about all frameworks, and i decided come├žar a usar o CI, it seems better, simple, seems to have a great community!

So i was reading the user guide and a lot of other stuffs, it will take a time until i get used to all features...

I have watched some tutorials too(like this http://net.tutsplus.com/articles/news/co...y-6-login/)
A great tutorial teaches how create a "template", login form, sign up...

So here we go, my question is... i want this same login but i want to show the fields to login in every page of my website in the header... (like this forum, show the fields to login, and after login u can see your informations "Hello, Felipe")...

Someone can help me, show me a good tutorial, or give me the right way to do it?

Thanks for your attention!!
Hope that i can help someone in a next future.

El Forum

[eluser]CroNiX[/eluser]
What I do is create a Template library that gets autoloaded. I won't write the whole thing out, but you can simply do things like:

Code:
class Template {
  private $CI;

  function __construct()
  {
    $this->CI =& get_instance();  //bring CI into the library
  }

  function make_page($title = '', $content = '')
  {
    //First load the header and menu, pass page title to header
    $data['title'] = $title;  //pass the page title to the header
    $header = $this->CI->load->view('header', $data, TRUE);

    $menu = $this->CI->load->view('menu', NULL, TRUE);

    //footer
    $footer = $this->CI->load->view('footer', NULL, TRUE);

    //set final output (uses CI buffering, allows for caching), just pass $content through
    $this->CI->output->set_output($header . $menu . $content . $footer);
  }
}

Then in your controllers, all you have to do is pass the template the main view you are using for the content and it will add the header, menu and footer every time. you can also create various methods to display different types of pages. This keeps your controllers clean and DRY. If you have a login system, for instance, where it displays the users info and some links if they are logged in and shows the login form if they aren't, the library can do all of that stuff for you so you don't have to pollute your controllers, and then there is only one place to update it if it needs updating.

Controller:

Code:
//...blah, do some work, manipulate some data...

//now only load the view for this content and send to the template
$content = $this->load->view('content', $some_data, TRUE);
$this->template->make_page('my page title', $content);

This is obviously a very simple example, which can be infinitely more complex and do a whole lot more.

El Forum

[eluser]Felipe Deitos[/eluser]
Ok i got it, its working perfectly (really really thanks for the help)

Getting that, brings me another question..
I already got the login form working in the header...
So the user type his username and password and press enter...
What is the action i should call in form_open('????Class/function????');

Thanks again for the attention! sorry about the newbie questions and bad english hehe!

El Forum

[eluser]CroNiX[/eluser]
Whatever controller/method does your login checking. I pass along the last url visited when logging in, so that if they get logged in it will redirect them back to the last page they were on, making it look like they never left.

El Forum

[eluser]Felipe Deitos[/eluser]
hehehe Last one... here we go!
I got my header and footer views inside a folder called includes...
On submit the form it calls 'includes/validate_user'

This is my includes controller:
Code:
<?php

class Includes extends CI_Controller {

public function validate_user()
{
  $this->load->model('users_model');
  $query = $this->users_model->validate();
  
  if($query)
  {
   $data = array(
    'usuario' => $this->input->post('usuario'),
    'is_logged' => true
   );
  
   $this->session->set_userdata($data);
   redirect('?????'); //WHERE
  }
  else
  {
   redirect('?????'); //WHERE
  }
}

}

/* End */

This is my users_model:
Code:
<?php

class Users_model extends CI_Model {

public function validate()
{
  $this->db->where('usuario', $this->input->post('usuario'));
  $this->db->where('senha', md5($this->input->post('senha')));
  $query = $this->db->get('usuarios');
  
  if($query->num_rows == 1)
  {
   return true;
  }
}

}

/* End */


How can i use this redirect to return to the page that i was viewing?

El Forum

[eluser]CroNiX[/eluser]
Should be easy enough. In your login form, create a "last_url" hidden form field that has the value of current_url(). When they successfully log in, send them back to the last_url.

Or, you could use the value of $this->input->server('REDIRECT_URL'), which may not be as accurate.

Is there a reason why your validate() function doesn't return FALSE if they don't successfully login?

El Forum

[eluser]Felipe Deitos[/eluser]
Thats it, got it... actually you must feel sorry for me hehehe cuz i am a *** newbie...
I dont even know that this current_url exists...
Like i said before it makes 2 days since i am working with CI

I dont know if its right or not but i guess i didnt returned false in model
because in the controller check if its true... if it is true make something... else other thing

Its working, but i guess this is a not good practice, right?

El Forum

[eluser]CroNiX[/eluser]
I would just
Code:
return ($query->num_rows == 1);  //which will return TRUE if it is and FALSE if it isn't 1.

El Forum

[eluser]Felipe Deitos[/eluser]
The king of the north!
hehehehe its always nice improve my coding style!
I Will use this always possible!

El Forum

[eluser]InsiteFX[/eluser]
Code:
<?php

class Users_model extends CI_Model {

public function validate()
{
  $this->db->where('usuario', $this->input->post('usuario'));
  $this->db->where('senha', md5($this->input->post('senha')));
  $query = $this->db->get('usuarios');
  
  if($query->num_rows() == 1)
  {
   return TRUE;
  }

  return FALSE;
}

}

/* End */
Pages: 1 2