Skip to main content

Membuat Login dan Logout dengan Mudah

October 5, 2009 by muhginanjar

muhginanjar's picture
  • Pertama-tama, aturlah config didalam application/config/config.php. Sebagai contoh : $config['base_url'] = "http://andhana2/coid/";.
  • Lalu rubah juga file application/config/routes.php. Sebagai contoh : $route['default_controller'] = "welcome";. Ini berfungsi untuk menjadikan controller welcome sebagai halaman utama pada web coid yang telah kita buat.
  • Tambahkan satu file pada application/libraries. Sebagai contoh : saya menambahkan file adn_otentikasi.php. Yang berisi :
    <br>
    <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
     
    class Adn_otentikasi
    {
        function Adn_otentikasi()
        {
            $this->obj =& get_instance();
        }
     
        function is_logged_in()
        {
            if ($this->obj->session) {
     
                if ($this->obj->session->userdata('logged_in'))
                {
                    return TRUE;
                }
                else
                {
                    return FALSE;
                }
            }
            else
            {
                return FALSE;
            }
        }
    }
     
    /* Akhir dari adn_otentikasi.php */
    ?>
    <br>
  • Lalu tambahkan satu file lagi ke application/models. Sebagai contoh : saya menambahkan satu file bernama app_model.php. dengan isi nya yaitu :
    <?php
    class App_model extends Model
    {
        function App_model()
        {
            parent::Model();
    		$this->load->library('session');
        }   
        function proses_login()
        {
            /* mengambil data post */
            $user           = $this->input->post('txtusername');
            $pass           = $this->input->post('txtpassword');
    		$hasil_login	= '';
            if ($user != 'admin' and $pass != 'admin')
            {
                $hasil_login = FALSE;
            }
            else
            {
                $hasil_login     = TRUE;
                $credentials = array('logged_in'=> $hasil_login);
            }
            $this->session->set_userdata($credentials);
     
        return $hasil_login;
    	}
    }
    ?>
  • Setelah itu lengkapilah controllers/welcome.php. Sehingga isi dari file welcome.php adalah sebagai berikut :
    <?php
    class Welcome extends Controller {
     
    	function Welcome()
    	{
    		parent::Controller();
    		$this->load->helper('url');
    	}
     
    	function index()
    	{
    		$data['pesan']	= "";
    		$this->load->view('welcome_message',$data);
    	}
     
    	function submit()
        {
            $this->load->model('app_model');
            $login              = $this->app_model->proses_login();
            if (!$login)
            {
                $data['pesan']  = 'Maaf, anda tidak berhak masuk!';
                $this->load->view('welcome',$data);
            }
            else
            {
                redirect('welcome/home');
            }
        }
        function logout()
        {
            $this->session->sess_destroy();
            redirect('welcome');
        }
    	function home()
    	{
    		$this->load->view('home');
    	}
    }
     
    /* End of file welcome.php */
    /* Location: ./system/application/controllers/welcome.php */
    ?>
  • Setelah itu editlah file welcome_messages.php dalam application/views menjadi :
    <?php
    echo '<h1>Silahkan login untuk meneruskan....</h1>';
    echo '<form method="POST" action="index.php/welcome/submit">
    <table>
    	<tr>
    		<td>User Name</td>
    		<td> : </td>
    		<td><input type="text" name="txtusername" maxlength="10"></td>
    	</tr>
    	<tr>
    		<td>Password</td>
    		<td> : </td>
    		<td><input type="password" name="txtpassword" maxlength="10"></td>
    	</tr>
    	<tr>
    		<td></td>
    		<td></td>
    		<td><input type="submit" name="submit" value="Login"></td>
    	</tr>
    </table>
    </form>
    <font color="red">'.$pesan.'</font>';
    ?>
  • Lalu setelah itu buatlah file home.php di application/views dengan isinya sebagai berikut :
    <?php 
    echo '<h1>Selamat Datang</h1>';
    echo '<code>Selamat Datang Pemenang .....<span style="float:right;"><a href="welcome/logout"><input type="submit" value="Log Out"></a></span>
    ';
    ?>
  • Lalu lihatlah pada browser anda....
  • Jika ada kesalahan, silahkan komentar...

Comments

Bingung Juga

March 25, 2011 by Syeehab, 27 weeks 1 day ago
Comment: 7840

Syeehab's picture

nah mas klow databasenya gmn tuh....kan diatas g dcantumin...

nah, klo masalah database,

March 25, 2011 by muhginanjar, 27 weeks 1 day ago
Comment: 7842

muhginanjar's picture

nah, klo masalah database, terserah masnya...
klo misal mau pake username sebagai primary key, berarti setiap username gak boleh sama...
terus klo mau passwordnya pake enkripsi juga mangga....

Bingung

March 25, 2011 by Syeehab, 27 weeks 1 day ago
Comment: 7839

Syeehab's picture

mas kalo welcome_messages.php itukan dah ada dari CI-nya...
nah trus code welcome_messages diatas ditambahin atau diubah keseluruhannya....

gapapa bro, tapi klo ane sih

March 25, 2011 by muhginanjar, 27 weeks 1 day ago
Comment: 7841

muhginanjar's picture

gapapa bro, tapi klo ane sih dibiarin aja teronggok disitu...
tapi klo bikin ribet mah bisa dihapus, diubah, dll...

kagak jalan

June 4, 2010 by errich, 1 year 16 weeks ago
Comment: 6883

errich's picture

bro,gw masih newbi di CI ni.
gw nyoba ngimplementasiin script yang lo buat kok keluarna di gw kayak gini ya bro...

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: pesan

Filename: views/welcome_message.php

Line Number: 82

credentials

December 11, 2009 by spysat, 1 year 42 weeks ago
Comment: 5750

spysat's picture

dari kode berikut di bawah ini yang anda tulis di atas..

<?php
// code terpotong di sini....
function proses_login()
    {
        /* mengambil data post */
        $user           = $this->input->post('txtusername');
        $pass           = $this->input->post('txtpassword');
        $hasil_login    = '';
        if ($user != 'admin' and $pass != 'admin')
        {
            $hasil_login = FALSE;
        }
        else
        {
            $hasil_login     = TRUE;
            $credentials = array('logged_in'=> $hasil_login);
        }
        $this->session->set_userdata($credentials);
 
    return $hasil_login;
    }
?>

jika user mengetik userdan password dengan benar, maka nantinnya variabel $credential tidak di kenali atau tak punya nilai value.

Best Article

October 9, 2009 by herupermana, 1 year 50 weeks ago
Comment: 5655

herupermana's picture

Thanks infonya yang dah di sharring dan di translate dari http://net.tutsplus.com/tutorials/php/how-to-update-your-twitter-status-with-codeigniter/<code>

re : Best Article

October 11, 2009 by muhginanjar, 1 year 50 weeks ago
Comment: 5660

muhginanjar's picture

Nah... kalo yang ini, buatan sendiri mas...
Yuk....

Premium Drupal Themes by Adaptivethemes