Sistem registrasi menggunakan codeigniter ion auth

Sistem login untuk aplikasi simplegis sudah kita buat sebelumnya, untuk admin kan sudah default terinput ke database pada saat mengimport file .sql. Karena sebelumnya telah kita siapkan halaman untuk member maka kita harus membuat sistem registrasi agar pengunjung bisa mengakses halaman member tersebut.

Masih sama menggunakan codeigniter-ion-auth kita akan membuat sistem registrasi. Pada controller Login kita tambahkan function registrasi() dan function prosesregistrasi(). Tapi sebelumnya edit formlogin.php terlebih dahulu dan tambahkan link untuk mengarah ke halaman registrasi.

formlogin.php

<div class="container">
    <div class="row">
        <div class="col-md-4 col-sm-4">
            <div class="panel panel-default">
    			<div class="panel-heading"><span class="glyphicon glyphicon-log-in"></span> Login</div>
    			<div class="panel-body">
    				<?php echo $this->session->flashdata('message');?>
    				<form action="<?php echo site_url('login'); ?>" method="post">
    					<div class="form-group">
    						<?php echo form_error('email'); ?>
    						<label for="email">Email</label>
    						<input type="email" class="form-control" id="email" name="email">
    					</div>
    					<div class="form-group">
    						<?php echo form_error('password'); ?>
    						<label for="password">Password</label>
    						<input type="password" class="form-control" id="password" name="password">
    					</div>
    					<div class="form-group">
    						<label>
    							<?php echo form_checkbox('remember','1',FALSE);?> Remember me
    						</label>
    					</div>
    					<div class="form-group">
    						<button class="btn-primary btn form-control" id="loginbutton">Login</button>
                            <p>
                                Belum mempunyai akun? <a href="<?php echo site_url('login/registrasi');?>">Daftar disini</a>
                            </p>
    					</div>
    				</form>
    			</div>
    		</div>
        </div>
    </div>
</div>

Sehingga ketika diakses akan tampak seperti berikut ini.

form login
Form Login

Kemudian tambahkan file formregistrasi.php ke folder views/home.

formregistrasi.php

<div class="container">
    <div class="row">
        <div class="col-md-4 col-sm-4">
            <div class="panel panel-default">
              <div class="panel-heading">
                <h3 class="panel-title"><span class="glyphicon glyphicon-list"></span> Form Registrasi</h3>
              </div>
              <div class="panel-body">
                  <form action="<?php echo site_url('login/prosesregistrasi');?>" method="post">
                      <div class="form-group">
                        <label for="nama">Nama Depan</label>
                        <div class="row">
                            <div class="col-md-6 col-sm-6">
                              <input type="text" class="form-control" id="first_name" name="first_name" placeholder="Nama Depan">
                            </div>
                            <div class="col-md-6 col-sm-6">
                              <input type="text" class="form-control" id="last_name" name="last_name" placeholder="Nama Belakang">
                            </div>
                        </div>
                      </div>
                      <div class="form-group">
                        <label for="phone">No Tlp</label>
                        <input type="text" class="form-control" id="phone" name="phone" placeholder="nomer tlp">
                      </div>
                      <hr>
                      <div class="form-group">
                        <label for="email">Email</label>
                        <input type="text" class="form-control" id="email" name="email" placeholder="email">
                      </div>
                      <div class="form-group">
                        <label for="password">Password</label>
                        <input type="password" class="form-control" id="password" name="password" placeholder="password">
                      </div>
                      <div class="form-group">
                        <input type="submit" name="submit" class="btn btn-primary" value="Daftar">
                      </div>
                  </form>
                  <?php if ($message!=null): ?>
                      <div class="alert alert-warning"><?php echo $message;?></div>
                  <?php endif; ?>
              </div>
            </div>
        </div>
    </div>
</div>

Dan terakhir pada file controller Login.php tambahkan 2 function yaitu registrasi.php dan prosesregistrasi.php

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

class Login extends CI_Controller {

	public function __construct()
	{
		parent::__construct();
		$this->load->library('session');
		$this->load->helper('form');
		$this->load->helper('url');
		$this->load->library('ion_auth');

	}
	public function index()
	{
		if ($this->input->post()) {
			$this->load->library('form_validation');
			$this->form_validation->set_rules('email', 'Email', 'required');
			$this->form_validation->set_rules('remember', 'Remember me', 'integer');
			if ($this->form_validation->run()==TRUE) {
				$remember = (bool) $this->input->post('remember');
				if ($this->ion_auth->login($this->input->post('email'),$this->input->post('password'),$remember)) {
					redirect('admin','refresh');
				}else{
					$this->session->flashdata('message',$this->ion_auth->errors());
					redirect('login','refresh');
				}
			}
		}
		if (!$this->ion_auth->logged_in()) {
			$data = array('content' => 'home/formlogin',
				'title'=>'Login Page',
				'description'=>'Login page');
			$this->load->view('templates/template-home',$data);
		}else{
            if ($this->ion_auth->in_group('admin')) {
                redirect('admin','refresh');
            }elseif ($this->ion_auth->in_group('members')) {
            	redirect('members','refresh');
            }else{
				redirect('login','refresh');
			}
        }
	}
	public function logout(){
		$this->ion_auth->logout();
  		redirect('login', 'refresh');
	}

	public function registrasi(){
		$data = array('content' => 'home/formregistrasi',
		'message'=>null);
		$this->load->view('templates/template-home', $data);
	}
	public function prosesregistrasi(){
		if ($this->input->post()) {
			$this->load->library('form_validation');
			$this->load->helper(array('security'));
			$this->form_validation->set_rules('first_name', 'Nama Depan', 'trim|required|xss_clean');
			$this->form_validation->set_rules('last_name', 'Nama Belakang', 'trim|required|xss_clean');
			$this->form_validation->set_rules('phone', 'No Tlp', 'trim|required|xss_clean');
			$this->form_validation->set_rules('email', 'Email', 'trim|required|xss_clean');
			$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean');
			if ($this->form_validation->run()==false) {
				$data = array('content' => 'home/formregistrasi',
				'message'=>validation_errors());
				$this->load->view('templates/template-home', $data);
			}else{
				$password = $this->input->post('password');
				$email = $this->input->post('email');
				$additional_data = array(
										'first_name' => $this->input->post('first_name'),
										'last_name' => $this->input->post('last_name'),
										'phone'=>$this->input->post('phone')
										);
				if (!$this->ion_auth->email_check($email))//cek email sudah terdaftar apa belum
				{
					$group = array('2'); // Sets user ke grup member
					if ($this->ion_auth->register($username, $password, $email, $additional_data, $group)) {
						redirect('login');
					}else{
						$data = array('content' => 'home/formregistrasi',
						'message'=>'Proses registrasi gagal diproses');
						$this->load->view('templates/template-home', $data);
					}
				}else{
					$data = array('content' => 'home/formregistrasi',
					'message'=>'Email sudah terdaftar');
					$this->load->view('templates/template-home', $data);
				}
			}
		}
	}

}

/* End of file Login.php */
/* Location: .//var/www/fadlur/apps/controllers/Login.php */

Selamat mencoba 😀

Tinggalkan Balasan

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