Kategori
PHP

Simple MD5 Login System (+Logout) with CodeIgniter

Case Study : Create Simple MD5 Login System (+Logout) with CodeIgniter

Requirements: Webserver Package, already installed. CodeIgniter bundle.

Login (and Logout) is almost exist in any application that developed with certain programming language, in this case is PHP with framework CodeIgniter. The very basic concept of login is matching the username and password from user with the ones that are in the databases. With a security reason, usually the password is encoded using MD5. MD5 is one of hash function (one-way) that quite popular in cryptography, usually for user autentification.

On the other hand, logout has the simplest basic concept, that is with destroy the user’s session.

Anyway… just let trying it 🙂

Step 1 : CI Configuration (always start with it)

  1. Open the config.php file located in the system-application-config
  2. Change base_url, adjust according where your ci folder are. FOr example: your ci folder located in www/ci folder
    so change the line $config['base_url']="http://example.com/";
    with
    $config['base_url']="http://localhost/ci";
  3. Setting the database. Open file database.php located in the same folder as config.php. Change hostname, username, password, and the database name according to your mysql setting. For example :
    $db['default']['hostname'] = "localhost";
    $db['default']['username'] = "root";
    $db['default']['password'] = "";
    $db['default']['database'] = "db_tutorial";

Step 2 : Prepare the Database

  1. Make a database named db_tutorial.
  2. Prepare a table named tb_book (for table’s structure, see picture below)
  3. Insert some sample data, for example insert some sample data just like the picture below (REMEMBER : when you inserting some sample data, use MD5 function in to password field —> see the red circle on the picture)
  4. This is the result of sample data (with MD5 password)
  5. Ok, we’re done with database !

Step 3 : Create the Login Form (view)

  1. Type the following script,
    [sourcecode language=”php”]
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html><head>
    <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
    <title>Login with CI</title>
    <center>
    <h2> <b> Login with CI </b> <h2>
    <form action="<?=base_url();?>login/proseslogin" method="post">
    <table border="0" align="center">
    <tr>
    <td> Username</td>
    <td> <input name="username" type="text"> </td>
    </tr>
    <tr>
    <td> Password</td>
    <td> <input name="password" type="password"> </td>
    </tr>
    <tr>
    <td> &nbsp; </td>
    <td> <input name="submit" type="submit" value="login"> </td>
    </tr>
    </table>
    </form>
    <?php if(isset($error)) echo "<b><span style=’color:red;’>$error</span></b>";
    if(isset($logout)) echo "<b><span style=’color:red;’>$logout</span></b>"; ?>
    </center>
    </body></html>
    [/sourcecode]
  2. Save with the name login_view.php in the system-application-views folder

Step 4 : Create the Processing Controller for Login+Logout

  1. Type the following script,
    [sourcecode language=”php”]
    <?php
    class Login extends Controller {
    //constructor
    function login() {
    parent::Controller();
    $this->load->helper(‘url’);
    $this->load->helper(‘form’);
    $this->load->library(‘form_validation’);
    $this->load->library(‘session’);
    }
    //index for showing the login form
    function index() {
    $this->load->view(‘login_view’);
    }
    //this function will do the login process
    function proseslogin() {
    $username = $this->input->post(‘username’); //read the username that filled by the user
    $password = $this->input->post(‘password’); //read the password that filled by the user
    $passwordx = md5($password); //this is for change $password into MD5 form
    //the query is to matching the username+password user with username+password from database
    $q = $this->db->query("SELECT * FROM tb_user WHERE username=’$username’ AND userpass=’$passwordx’");
    if ($q->num_rows() == 1) {
    // if the query is TRUE, then save the username into session and load the welcome view
    $nama = $q->row()->username;
    $this->session->set_userdata(‘username’,$nama);
    $data[‘welcome’] = "Welcome $nama";
    $this->load->view(‘welcome_view’, $data);
    }
    else {
    // query error
    $data[‘error’]=’!! Wrong Username or Password !!’;
    $this->load->view(‘login_view’, $data);
    }
    }
    //to do logout process
    function logout() {
    $this->session->sess_destroy();
    $data[‘logout’] = ‘You have been logged out.’;
    $this->load->view(‘login_view’, $data);
    }
    }
    ?>
    [/sourcecode]
  2. Save with the name login.php in the system-application-controllers folder
  3. Penjelasan :See at the script’s comment.

Step 4 : Create the Success View

  1. Type the following script,
    [sourcecode language=”php”]
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html><head>
    <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
    <title>Login with CI</title>
    <center>
    <?php if(isset($welcome)) echo "<h2><span style=’color:red;’>$welcome</span></h2>";
    echo "<br/>";
    echo anchor("login/logout", ‘Logout’) ?>
    </center>
    </body></html>
    [/sourcecode]
  2. Save with the name welcome_view.php in the system-application-views folder

Step 7 : Testing the Code

  1. Go to http://localhost/namaFolderCIKamu/login
  2. You will see something like this,
  3. Insert the correct username+password, that is the one in the database (username : june, password : june)
  4. You will be redirected into success login page, that is welcome page like in the picture below,
  5. Click logout to logout from welcome page.
  6. Now insert the wrong username+password (for example username : admin, password : admin)
  7. The warning will shown like below.

Okay, Happy Coding 🙂

Oleh littleflow3r

obviously, a girl. with sweet smile, off course. turning her 21 years trapped on Informatics departement and just started to having some crush with it lately. she uses wordpress. she loves php. she loves to satisfied her shopping hobbies just via looking around in online shop. oh yeah, she got an A for her english subject^^

4 replies on “Simple MD5 Login System (+Logout) with CodeIgniter”

numpang tanya 😀
saya juga sedang buat form login dengan CI.

nah masalah saya ialah. ketika saya load form login dia tetep muncul padahal udah login..

gimana ya pemecahannya..?…

Login with CI

Login with CI
<form action="login/proseslogin” method=”post”>

Username

Password

 

<?php if(isset($error)) echo "$error“;
if(isset($logout)) echo “$logout“; ?>

*saya copas knp yg keluar hanya “Login With CI”??
yg lain nya ga keluar??

Tinggalkan Balasan ke sunupf Batalkan balasan

Alamat email Anda tidak akan dipublikasikan. Ruas yang wajib ditandai *

This site uses Akismet to reduce spam. Learn how your comment data is processed.