Acá mi controlador (login.php)
Código PHP:
<?php
class Login extends CI_Controller {
function index()
{
$data['main_content'] = 'login_form';
$this->load->view('includes/template', $data);
}
function validate_credentials()
{
$this->load->model('membership_model');
$query = $this->membership_model->validate();
if ($query) //Existe el usuario
{
$rol = $this->membership_model->roles($this->input->post('username')); //Busco el rol en el modelo
$data = array(
'username' => $this->input->post('username'),
'rol' => $rol,
'is_logged_in' => true
);
$this->session->set_userdata($data);
redirect('site/members_area');
}
else
{
$this->index();
}
}
function signup()
{
$data['main_content'] = 'signup_form';
$this->load->view('includes/template', $data);
}
function create_member()
{
$this->load->library('form_validation');
//field name, error message, validation rules
$this->form_validation->set_rules('first_name', 'First Name', 'trim|required');
$this->form_validation->set_rules('last_name', 'Last Name', 'trim|required');
$this->form_validation->set_rules('email_address', 'Email Address', 'trim|required|valid_email');
$this->form_validation->set_rules('username', 'Username', 'trim|required|min_lenght[4]');
$this->form_validation->set_rules('password', 'Password', 'trim|required|min_lenght[4]|max_lenght[32]');
$this->form_validation->set_rules('password2', 'Password Confirmation', 'trim|required|min_lenght[4]|max_lenght[32]|matches[password]');
if (($this->form_validation->run()) == FALSE)
{
$this->signup();
}
else
{
$this->load->model('membership_model');
if ($query = $this->membership_model->create_member)
{
$data['main_content'] = 'signup_succesful';
$this->load->view('includes/template', $data);
}
else
{
$this->load->view('signup_form');
}
}
}
}
?>
Código PHP:
<?php
class Membership_model extends CI_Model {
function validate()
{
$this->db->where('username', $this->input->post('username'));
$this->db->where('password', md5($this->input->post('password')));
$query = $this->db->get('membership');
if($query->num_rows == 1)
{
return true;
}
}
function roles($usuario)
{
$sql = "SELECT roles FROM membership WHERE username = ? LIMIT 1";
$q = $this->db->query($sql, $usuario);
if ($q->num_rows() > 0)
{
foreach($q->result() as $row) {
$data = $row;
}
return $data;
}
}
function create_member()
{
$new_member_insert_data = array(
'first_name' => $this->input->post('first_name'),
'last_name' => $this->input->post('last_name'),
'username' => $this->input->post('username'),
'password' => md5($this->input->post('password')),
'email_address' => $this->input->post('email_address'),
'roles' => '1'
);
$insert = $this->db->insert('membership', $new_member_insert_data);
return $insert;
}
}
?>
Código PHP:
<h1>Create an Account</h1>
<fieldset>
<legend>Personal Information</legend>
<?php
echo form_open('login/create_member');
echo form_input('first_name', set_value('first_name', 'First Name'));
echo form_input('last_name', set_value('last_name', 'Last Name'));
echo form_input('email_address', set_value('email_address', 'Email Address'));
?>
</fieldset>
<fieldset>
<legend>Login Info</legend>
<?php
echo form_input('username', set_value('username', 'Username'));
echo form_input('password', set_value('password', 'Password'));
echo form_input('password2', set_value('password2', 'Password Confirm'));
echo form_submit('submit','Create Account');
?>
<?php echo validation_errors("<p class='error'>"); ?>
</fieldset>