LOGIN

1. models --->users_model.php
2. libraries-->access.php
3. controller-->users_model.php
4. view----->admin_area.php,login.php
5. core--->MY_Controller.php

jo lali config.php $config['encryption_key'] = '0987';

$autoload['libraries'] = array('database','session');


/*
| -------------------------------------------------------------------
|  Auto-load Helper Files
| -------------------------------------------------------------------
| Prototype:
|
| $autoload['helper'] = array('url', 'file');
*/

$autoload['helper'] = array('url');


database
CREATE TABLE `users` (                                   
          `id` INT(11) NOT NULL AUTO_INCREMENT,                  
          `username` VARCHAR(255) NOT NULL,                      
          `email` VARCHAR(255) NOT NULL,                         
          `password` VARCHAR(255) NOT NULL,                      
          PRIMARY KEY (`id`)                                     
        ) ENGINE=MYISAM AUTO_INCREMENT=3 DEFAULT CHARSET=latin1

users_model.php

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
 class Users_model extends CI_Model
 {

 public $table = 'users';
 public $primary_key = 'id';

 function __construct()
 {
 parent::__construct();
 }

 function get_login_info($username)
 {
 $this->db->where('username', $username);
 $this->db->limit(1);
 $query = $this->db->get($this->table);
 return ($query->num_rows() > 0) ? $query->row() : FALSE;
 }

 }

access.php

<?php
class Access
 {
 public $user;

/**
8 * Constructor
9. */
 function __construct()
 {
 $this->CI =& get_instance();
 $auth = $this->CI->config->item('auth');

 $this->CI->load->helper('cookie');
 $this->CI->load->model('users_model');
 $this->users_model =& $this->CI->users_model;
  }

 /**
22. * Cek login user
23. */

 function login($username, $password) {

$result = $this->users_model->get_login_info($username);

 if ($result) // Result Found
 {
 $password = md5($password);
 if ($password === $result->password){
 // Start session
 $this->CI->session->set_userdata('id', $result->id);
 return TRUE;
 }
 }  
 return FALSE; 
 }


 /**
46. * cek apakah udah login
47. */
function is_login (){ 
    return (($this->CI->session->userdata('id')) ? TRUE : FALSE);
 }

 /**
54. * Logout
55. *
56. */
 function logout ()
 {
 $this->CI->session->unset_userdata('id');
 }

 }

MY_Controller.php

<?php 
if (!defined('BASEPATH')) exit('No direct script access allowed');
class Member_Controller extends CI_Controller {

 function __construct(){
 parent::__construct();
 $this->load->library('access');
 if (!$this->access->is_login())
 {
 redirect('member/login');
 }
 //bisa dtambahi fungsionalitas lain

 }
 function is_login()
 {
 return $this->access->is_login();
 }

 }

 class MY_Controller extends CI_Controller {
 function __construct()
 {
 parent::__construct();
 }
}

Member.php

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

 class Member extends CI_Controller
 {
 function __construct()
 {
 parent::__construct();
 $this->load->library('access');
 }

 function index()
 {

 $this->access->logout();
 $this->login();
 }

 function login()
 {
    $this->load->library('form_validation');
 $this->load->helper('form');

$this->form_validation->set_rules('username', 'Username','trim|required|strip_tags');
$this->form_validation->set_rules('password', 'Password','trim|required');
$this->form_validation->set_rules('token', 'token','callback_check_login');
 //$this->output->enable_profiler(1);
 if ($this->form_validation->run() == FALSE)
 {
$this->load->view('member/login');

 }
 else
 {
 redirect('adminarea');
 }
 }

 function logout()
 {
 $this->access->logout();
 redirect('member/login');
 }

 function check_login()
 {

 $username = $this->input->post('username',TRUE);
 $password = $this->input->post('password',TRUE);

 $login = $this->access->login($username, $password);
 if($login){
    return TRUE;
 }else
 {
 $this->form_validation->set_message('check_login','Username atau password anda salah.');
 return FALSE;
 }
 }
 }

Adminarea.php

<?php
class Adminarea extends Member_Controller
{
function __construct()
 {
 parent::__construct();
 }

 function index(){
    
    $this->load->view('admin_area');
 }
 function love(){
    
    echo "iyaaa love";
 }
 function you(){
    echo "iyaaaa kamuuuuu iyaaa kamuu pokoknya..?? gak mau yang lain";
    
 }
 }

login.php

<?php $v =& $this->form_validation ?>

 <h1> Sign On</h1>
<p>Welcome. Please fill your username and password.</p>
 <?php if(validation_errors()){ ?>
 <div class="fail">
 <?php echo validation_errors(); ?>
 </div>

 <?php } ?>

<form name="loginform" method="post" target="<?php site_url('member/login') ?>" style="margin:0px;">

 <p><label>Username</label>
<input name="username" id="username" value="<?php echo @$v->username?>" class="input large" type="text" />
 </p>
 <p><label>Password</label>
 <input name="password" id="password" width="100px" type="password" class="input large" />
 </p>
 <p><input name="submit" type="submit" value="Submit "
class="button"/></p>
 </form>

admin_area.php

<h1>Selemat datang di halan admin</h1>
<br />
<a href="<?php echo site_url('member/logout'); ?>">logout</a>

0 Komentar untuk "LOGIN"

Back To Top