Popular Post

Aplikasi Open Source Berbasis CodeIgniter

By : Unknown

CodeIgniter adalah framework yang sangat mudah dipelajari dan cukup ringan. Jika anda tertarik untuk untuk mempelajari CodeIgniter lebih dalam, membaca source code aplikasi open source berbasis CodeIgniter mungkin akan memperkaya pengetahuan anda tentang CodeIgniter.








Berikut beberapa aplikasi open source berbasis CodeIgniter yang bisa anda pelajari :
Tag : ,

4 Simple MD5 Login System (+Logout) with CodeIgniter

By : Unknown
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 hostnameusernamepassword, 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,
    <!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>
    
  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,
    <?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);
    }
    }
    ?>
    
  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,
    <!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>
    
  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.

Source : http://en.itx.web.id/php-en/simple-md5-login-system-logout-with-codeigniter/

Tag : ,

102 Tutorial Searching / Pencarian Data dengan PHP dan Mysql

By : Unknown
Studi Kasus : Membuat script untuk pencarian data dalam database
Kebutuhan : Webserver Packages, already installed.
Searching, merupakan fasilitas yang hampir selalu ada dalam setiap aplikasi, baik aplikasi berbasis web ataupun desktop. Setiap halaman web/ blog juga akan ditemukan fasilitas ini. Tutorial berikut akan memberikan konsep dasar dari searching, dengan melakukan pencarian pada data terstruktu (database).
Ok, langsung praktik saja.
Step 1 : Persiapkan Database
  1. Buat database dengan nama db_tutorial
  2. Siapkan tabel dengan nama tb_student, dengan struktur tabel seperti gambar dibawah ini.
  3. Insert-kan beberapa sample data, misal seperti gambar dibawah,
Step 2 : Persiapkan Folder Kerja
  1. Buat folder dengan nama searching dalam document root anda
  2. Simpan semua file dalam praktikum ini dalam folder tersebut.
Step 3 : Membuat script koneksi ke DB
  1. Ketikkan script berikut,
    <?php
    $host = "localhost";
    $user = "root";
    $pass = "";
    $dbName = "db_tutorial";
    mysql_connect($host, $user, $pass);
    mysql_select_db($dbName)
    or die ("Connect Failed !! : ".mysql_error());
    ?>
    
  2. simpan dengan nama connect.php
  3. Penjelasan : Script ini akan digunakan untuk menghubungan aplikasi kita dengan database db_tutorial. Sesuaikan username dan password dengan setting-an mysql anda.
Step 4 : Membuat form pencarian
  1. Ketikkan script berikut,
    <html>
    <head>
    <title> Halaman Pencarian </title>
    </head>
    <body>
    <form name="formcari" method="post" action="search_exe.php">
    <table width="330" border="0" align="center" cellpadding="0">
    <tr bgcolor="orange">
    <td height="25" colspan="3">
    <strong> Student Searching ! </strong>
    </td>
    </tr>
    <tr> <td>  Name </td>
    <td> <input type="text" name="name"> </td>
    </tr>
    <td></td>
    <td> <input type="SUBMIT" name="SUBMIT" id="SUBMIT" value="search" > </td>
    </table>
    </form>
    </body>
    </html>
    
  2. simpan dengan nama formsearching.php
  3. Penjelasan : script diatas adalah HTML script biasa yang akan menampilkan form. Perhatian bagian action="search_exe.php", bagian ini dimaksudkan bahwa form akan diproses oleh script search_exe.php
Step 5 : Membuat script pemrosesan dan menampilkan hasil searching
  1. Ketikkan script berikut,
    <?php
    include "connect.php";
    $name= $_POST['name']; //get the nama value from form
    $q = "SELECT * from tb_student where name like '%$name%' "; //query to get the search result
    $result = mysql_query($q); //execute the query $q
    echo "<center>";
    echo "<h2> Hasil Searching </h2>";
    echo "<table border='1' cellpadding='5' cellspacing='8'>";
    echo "
    <tr bgcolor='orange'>
    <td>No</td>
    <td>Nama Mahasiswa</td>
    <td>Alamat</td>
    </tr>";
    while ($data = mysql_fetch_array($result)) {  //fetch the result from query into an array
    echo "
    <tr>
    <td>".$data['no']."</td>
    <td>".$data['name']."</td>
    <td>".$data['address']."</td>
    </tr>";
    }
    echo "</table>";
    ?>
    
  2. simpan dengan nama search_exe.php
  3. Penjelasan dapat dilihat di script comment
Step 6 : Testing Code
  1. Pergi ke http://localhost/searching/formsearching.php. Anda akan melihat form seperti dibawah,
  2. Masukkan nama pada field nama (misal nama yang telah diinsert ke DB, yaitu june)
  3. Klik tombol search dan anda akan melihat hasilnya
Tag : ,

Validasi Form dalam CodeIgniter

By : Unknown
Studi Kasus : Membuat form beserta validasinya dengan CodeIgniter.
Kebutuhan Sistem : CodeIgniter PHP Framework, bisa didownload di sini. Dan Webserver Package.
Kebutuhan User : Kemampuan PHP, kemampuan Mysql, Kemampuan HTML, dan (sedikit saja cukuplah) kemampuan CodeIgniter.
Dalam men-develop sebuah aplikasi berbasis web, mau tidak mau pasti kita akan sering menggunakan form. Form sangat berguna untuk membuat antarmuka penginputan data ke dalam database. Untuk mem-preventagar data input yang masuk ke database benar, atau sesuai dengan yang diinginkan, maka diperlukan suatu validasi. Pada bahasa pemrograman PHP, pada umumnya validasi dibuat dengan menggunakan fungsi ereg, dengan memanfaatkan regular expression (regex). Dan percayalah, kalau regex ini sangat rumit. Hahahah
Dengan menggunakan CodeIgniter, validasi jauh lebih mudah dibuat apabila dibandingkan dengan php biasa.
Ohya, ini adalah sebuah validasi server-side. Jelasnya, validasi dilakukan dari sisi server sehingga secara otomatis sistem validasi ini akan lebih aman dibandingkan dengan client-side validation (biasanya dibuat dengan javascript)
Okay, langsung praktikum…
Step 1 : Konfigurasi CI
  1. Buka file config.php yang berada dalam folder system-application-config-config.php
  2. Ubah base url, sesuaikan dengan lokasi dimana folder CI-mu berada (tempat ekstrak-an tadi). Contoh : Folder CI anda berada dalam folder www/ci maka ubah baris$config['base_url']="http://example.com/";
    dengan
    $config['base_url'] = "http://localhost/ci/";
  3. Setting database. Buka file database.php yang berada dalam folder yang sama dengan config.php. Ubah hostnameusernamepassword, dan nama database. Sesuaikan dengan pengaturan mysql anda. Contoh : $db['default']['hostname'] = "localhost";
    $db['default']['username'] = "root";
    $db['default']['password'] = "";
    $db['default']['database'] = "db_code";
Step 2 : Siapkan database
  1. Buat database dengan nama db_code (via phpmyadmin)
  2. Siapkan tabel dengan nama tb_student (untuk strukturnya, lihat gambar dibawah ini)

    tb_student's structure
  3. Ok, we’re done with database !
Step 3 : Membuat Controller
  1. Ketikkan script dibawah ini dengan teks editor kesukaan anda, <?php
    class Student extends Controller {function Student(){
    parent::Controller();
    $this->load->library(array('table','validation'));
    $this->load->helper('url');
    }
    function index() {
    $this->_set_fields();
    $data['title'] = 'Add New Student';
    $data['message'] = '';
    $data['action'] = site_url('student/addStudent');
    $this->load->view('addStudent_view', $data);
    }
    function addStudent(){
    $data1['title'] = 'Add New Student';
    $data1['action'] = site_url('student/addStudent');
    $this->_set_fields();
    $this->_set_rules();
    // menjalankan validasi
    if ($this->validation->run() == FALSE){
    $data['message'] = '';
    }else{
    // menyimpan data
    $data= array('id' => $this->input->post('id'),
    'nama' => $this->input->post('nama'),
    'email' => $this->input->post('email')
    );
    $this->db->insert('tb_student', $data);
    $data1['message'] = 'Sukses';
    }
    // load view
    $this->load->view('addStudent_view', $data1);
    }
    function _set_fields(){
    $fields['id'] = 'id';
    $fields['nama'] = 'nama';
    $fields['email'] = 'email';
    $this->validation->set_fields($fields);
    }
    // berikut adalah rule-rule untuk validasi
    function _set_rules(){
    $rules['id'] = 'trim|required|numeric';
    $rules['nama'] = 'trim|required';
    $rules['email'] = 'trim|required|valid_email';
    $this->validation->set_rules($rules);
    $this->validation->set_message('required', '* harus diisi');
    $this->validation->set_message('numeric', '* hanya boleh diisi dengan angka');
    $this->validation->set_message('valid_email', '* email tidak valid');
    $this->validation->set_error_delimiters('<p>', '</p>');
    }
    }
    ?>
  2. Simpan dalam folder system-application-controllers, dengan nama student.php
Step 4 : Membuat View
  1. Ketikkan script dibawah ini, <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Form Validation CI</title>
    </head>
    <body>
    <h1><?php echo $title; ?></h1>
    <?php if (isset($message)) echo $message; ?>
    <form method="post" action="<?php echo $action; ?>">
    <table>
    <tr>
    <td valign="top">ID*</td>
    <td><input type="text" name="id" value="<?php echo $this->validation->id; ?>"/>
    <span style="color:red;"> <?php echo $this->validation->id_error; ?></span></td>
    </tr>
    <tr>
    <td valign="top">Nama<span style="color:red;">*</span></td>
    <td><input type="text" name="nama" value="<?php echo $this->validation->nama; ?>"/>
    <span style="color:red;"> <?php echo $this->validation->nama_error; ?></span></td>
    </tr>
    <tr>
    <td valign="top">Email<span style="color:red;">*</span></td>
    <td><input type="text" name="email" value="<?php echo $this->validation->email; ?>"/>
    <span style="color:red;"> <?php echo $this->validation->email_error; ?></span></td>
    </tr>
    <tr>
    <td>&nbsp;</td>
    <td><input type="submit" value="Save"/></td>
    </tr>
    </table>
    </form>
    </body>
    </html>
  2. Simpan dalam folder system-application-views, dengan nama addStudent_view.php
Step 5 : Testing Code
  1. Buka browser, pergi ke http://localhost/namaFolderCIKamu/student
  2. Anda akan melihat tampilan seperti berikut,

    Input Form
  3. Coba isi form dengan input salah (contoh, ID : AAA; Nama : (kosongi saja); Email : AAA)

    Wrong Input (example)
  4. Pilih Save. Maka anda akan melihat eror ditampilkan seperti gambar dibawah,

    Validation Confirmation
  5. Sekarang coba dengan isian benar, seperti pada gambar dibawah, lalu Save. Anda akan melihat konfirmasi sukses :D Cek juga database anda, seharusnya anda akan melihat data yang baru saja anda masukkan di dalam tb_student :D

    Correct Input (example)
    [caption id="attachment_842" align="aligncenter" width="233" caption="Success Confirmation"][/caption]
p.s. Mungkin akan ada yang tanya, ini coding CI yang katanya MVC kok gak pakai model?
Jawabannya adalah : dalam CI, kita tidak selalu diharuskan memakai model. Menggunakan code seperti $this->db->insert('tb_student', $data); adalah sudah cukup, sesuai dengan tingkat kerumitan code anda.

http://itx.web.id/php/validasi-form-dalam-codeigniter/
Tag : ,

Bagaimana Menggunakan Helper Pada Codeigniter

By : Unknown
Menggunakan Helper pada codeigniter sangat mudah. Cukup Meload Helper Lalu gunakan fungsinya seperti menggunakan fungsi pada umumnya. Contoh :


<?php
      //load
      $this->load- > helper("url");

     echo base_url();
?>

Kode ini meload file url_helper.php lalu menjalankan fungsi base_url();

Tag : ,

PHP Encrypt / Decrypt : Simple Class untuk Enkripsi dan Dekripsi pada Codeigniter

By : Unknown
Ada saat saat tertentu ketika data di URL harus di amankan,  dengan menggunakan enkripsi pada data URL tersebut. Pada website codeigniter anda membuat sebuah library, letakkan pada application/libraries/Encryption.php

class Encryption {
 var $skey  = "SuPerEncKey2010"; // you can change it
 
    public  function safe_b64encode($string) {
 
        $data = base64_encode($string);
        $data = str_replace(array('+','/','='),array('-','_',''),$data);
        return $data;
    }

 public function safe_b64decode($string) {
        $data = str_replace(array('-','_'),array('+','/'),$string);
        $mod4 = strlen($data) % 4;
        if ($mod4) {
            $data .= substr('====', $mod4);
        }
        return base64_decode($data);
    }
 
    public  function encode($value){ 
  
     if(!$value){return false;}
        $text = $value;
        $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
        $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
        $crypttext = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $this->skey, $text, MCRYPT_MODE_ECB, $iv);
        return trim($this->safe_b64encode($crypttext)); 
    }
    
    public function decode($value){
  
        if(!$value){return false;}
        $crypttext = $this->safe_b64decode($value); 
        $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
        $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
        $decrypttext = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $this->skey, $crypttext, MCRYPT_MODE_ECB, $iv);
        return trim($decrypttext);
    }
}

pada file application/libraries/autoload.php, tambahkan load library encrption 

$autoload['libraries'] = array('Encryption'); 


pada proses enkripsi dan dekripsi
$this->encryption->encode('Your data');
$this->encryption->decode('Your encrypted data');

Source




Tag : ,

- Copyright © Acehsourcecode - Date A Live - Powered by Blogger - Designed by Johanes Djogan -