Simple CodeIgniter signup/signin project. Needs of course some config and a db table 'membership' with columns 'id', 'username', 'last_name', 'first_name', 'password'.
// in /views/includes
<?php $this->load->view('includes/header'); ?>
<?php $this->load->view($main_content); ?>
<?php $this->load->view('includes/footer'); ?>
// in /controllers
<?php
class Site extends CI_Controller {
function __construct() {
parent::__construct();
$this->is_logged_in();
}
function members_area() {
$this->load->view('members_area');
}
function is_logged_in() {
$is_logged_in = $this->session->userdata('is_logged_in');
if (!isset($is_logged_in) || $is_logged_in !== true) {
echo 'You don\'t have permission to access this page. <a href="../login">Login</a>';
die();
}
}
}
// in /views
<h1>Congrats!</h1>
<p>Your account has been created. <?php echo anchor('login', 'Login Now'); ?></p>
// in /views
<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>
<legend>Login Info</legend>
<?php
echo form_input('username', set_value('username', 'Username'));
echo form_input('password', 'Password');
echo form_input('password2', 'Confirm Password');
echo form_submit('submit', 'Create Account');
?>
<?php echo validation_errors('<p class="error">') ?>
</fieldset>
// in /models
<?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 create_member() {
$new_member_insert_data = array(
'first_name' => $this->input->post('first_name'),
'last_name' => $this->input->post('last_name'),
'email_address' => $this->input->post('email_address'),
'username' => $this->input->post('username'),
'password' => md5($this->input->post('password'))
);
$insert = $this->db->insert('membership', $new_member_insert_data);
return $insert;
}
}
// in /views
members area only
// in /views
<div id="login form">
<h1>Login!</h1>
<?php
echo form_open('login/validate_credentials');
echo form_input('username', 'Username');
echo form_password('password', 'Password');
echo form_submit('submit', 'Login');
echo anchor('login/signup', 'Create Account');
?>
</div>
// in /controllers
<?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) {
$data = array(
'username' => $this->input->post('username'),
'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');
$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', 'trim|required|valid_email');
$this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[4]');
$this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[32]');
$this->form_validation->set_rules('password2', 'Confirm Password', 'trim|required|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_successful';
$this->load->view('includes/template', $data);
} else {
$this->signup();
}
}
}
}