Sistem ini masih sederhana, yaitu user akan memasukkan username dan password, yang kemudian akan dicocokkan dengan username dan password pada database. Jika data user cocok maka akan masuk ke halaman home, jika tidak maka akan kembali ke halaman login.
note: untuk password saya enkripsi menggunakan md5.
Ok.. lets try, enjoy coding.. *menghibur diri*
1. Pastikan konfigurasi CI sudah benar, dan database sudah tersedia
2. Buat halaman login.php di application/view/login.php
Insert script berikut diantara tag <body> </body>
<?php
if ($this->session->flashdata('message')){
?>
<div class="msg"><?php echo $this->session->flashdata('message'); ?></div>
<?php } ?>
<?php echo form_open('login/process_login') . "\n"; ?>
<p>
<label for="username">Username </label><?php echo form_input($username); ?>
</p>
<p><label for="password">Password</label><?php echo form_password($password); ?>
</p>
<p>
<?php echo form_submit($login); ?>
</p>
<?php echo form_close(); ?>
3. buat halaman login.php di application/controllers/login.php
<?php
class Login extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->helper('url', 'form');
$this->load->library('form_validation', 'encrypt');
}
function index()
{
if ($this->session->userdata('logged_in') == TRUE)
{
redirect('home');
}
$data['title'] = 'Halaman login';
$data['username'] = array('id' => 'username', 'name' => 'username');
$data['password'] = array('id' => 'password', 'name' => 'password');
$data['login'] = array('class' => 'button', 'name' => 'login');
$this->load->view('login', $data);
}
function process_login()
{
$username = mysql_real_escape_string($this->input->post('username')); //get username
$password = mysql_real_escape_string($this->input->post('password')); //get password
$password = md5($password);
//the query is to matching the username+password user with username+password from database
$q = $this->db->query("SELECT username, privilege FROM cms_users WHERE username='$username' AND password='$password'");
if ($q->num_rows() == 1)
{
$username =$q->row()->username;
$privilege = $q ->row()->privilege;
//if the query is TRUE, then save the username into session and load the welcome view
$data_session = array(
'username' => $username,
'privilege' => $privilege,
'logged_in' => TRUE
);
$this->session->set_userdata($data_session);
redirect('home');
}
else
{
$this->session->set_flashdata('message', 'username or password is incorrect, please try again.');
redirect('login/index');
}
}
function logout()
{
$this->session->sess_destroy();
redirect('login/index');
}
}
?>
4. Ops, belum nyiapin table di mysql-nya, create dulu..
CREATE TABLE `users` (
`id` int(10) NOT NULL auto_increment,
`username` varchar(50) default NULL,
`password` varchar(50) default NULL,
`privilege` varchar(20) default NULL,
PRIMARY KEY (`id`)
)
insert into users (username, password, privilege) values ('admin', md5('admin'), 'admin')5. Jalankan dibrowser
http://localhost/testing
username-password: admin-admin6. Jika login berhasil, anda akan masuk ke halaman home " redirect('home');". sehingga kita perlu satu halaman lagi. Buat home.php di applications/view
Insert script berikut diantara <body></body>
<p align="center">
<?php
if(isset($welcome))
echo "<br /><span style='color:red;' size='2' >$welcome</span> <br />";
echo "<span style='color:white;' size='2' >".$date;
echo anchor("login/logout", 'Logout')
?>
</p>
Untuk point 2, silakan dimodifikasi agar terlihat bagus tampilannya :) Semoga bermanfaat.
kok error process_login not found....
BalasHapusknp ya kira2?
fungsi process_login blm ada..
Hapusfunction process_login()
{
//your code here
}
Bos ko error ya "Fatal error: Call to a member function userdata() on a non-object in C:\xampp\htdocs\Tugasweb2\application\controllers\login.php on line 11"
BalasHapustolong bantuannya bos msh newbie jg g ngerti.
thanks
Kodingannya udah sama kaya contoh diatas bos?
Hapuskemungkinan error saat penulisan:
$this->session->set_userdata($data_session);
pengen nanya nih,
BalasHapuskalo misalnya kita mau login dengan level user administrator bagaimana ya?
apa aja yang harus di tambahkan . .?