Sistem login dan registrasi pengguna diperlukan saat ini untuk aplikasi web dinamis apa pun, Jadi hari ini di dalam tutorial ini kita akan belajar cara membuat Registrasi dan Login menggunakan MySQL Bootstrap. Dalam CodeIgniter, kita dapat menerapkan skrip sistem pendaftaran login menggunakan librari session. Jadi Tutorial ini akan memberi Anda panduan langkah demi langkah agar Anda dapat membuat skrip pendaftaran ini dengan mengikuti langkah-langkah berikut.
Dalam aplikasi ini, kami membuat 2 form untuk pendaftaran dan juga untuk login. Pengguna akan mendaftar dari form pendaftaran dan kemudian masuk ke form login yang selanjutnya di redirect ke halaman dashboard.
Cek Juga : Ajax CRUD dengan Bootstrap modals dan DataTables
Projek ScreenshootCek Juga : Ajax CRUD dengan Bootstrap modals dan DataTables
Buatlah database dan tabel
[code hl="1, 4, 7"]
--
-- Table structure for table `user`
--
CREATE TABLE `user` (
`user_id` int(11) NOT NULL,
`user_name` varchar(50) NOT NULL,
`user_email` varchar(50) NOT NULL,
`user_password` varchar(50) NOT NULL,
`user_age` int(11) NOT NULL,
`user_mobile` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
[/code]
Insert beberapa data
[code hl="1, 4, 7"]
--
-- Dumping data for table `user`
--
INSERT INTO `user` (`user_id`, `user_name`, `user_email`, `user_password`, `user_age`, `user_mobile`) VALUES
(1, 'Ehtesham', 'ehtesham@gmail.com', '123', 23, 334443333),
(2, 'Ehtesham', 'ehtesham@gmail.com', '123', 23, 2147483647),
(3, 'farrukh', 'farrukh@gmail.com', '123', 32, 232342343),
(4, 'zaid', 'zaid@gmail.com', '202cb962ac59075b964b07152d234b70', 23, 324234234);
[/code]
Selanjutnya Konfigurasi di CodeIgniter
- Pada langkah ini, kita akan melakukan beberapa konfigurasi dalam folder codeigniter. Buka file konfigurasi Anda yang terletak di aplikasi -> config -> config.php, ubah base url sesuai dengan nama folder di xampp/htdocs misalnya "http:\\localhost\belajar_login\" .
- Lalu buka file autoload Anda yang terletak di aplikasi -> config -> autoload.php dan cari kode $autoload ['libraries'] = array (); ganti dengan kode $autoload ['libraries'] = array ('session', 'database') ; Ini akan memanggil library database dan session secara otomatis.
- Sekarang buka file database yang terletak di aplikasi -> config -> database.php dan masukkan nama host, pengguna, kata sandi, dan nama database Anda, misalnya 'hostname' => 'localhost', 'username' => 'root', 'password' => '', 'database' => 'ci-login-registration'.
- Sekarang buka file routes Anda yang terletak di aplikasi -> config -> routes.php. Di sini Anda dapat mengatur Controller default yang ingin dijalankan pada saat pertama kali aplikasi dijalankan. Saya mengatur Controller defaultnya menjadi seperti ini $route ['default_controller'] = 'user';
[code hl="1, 4, 7"]
<?php
class User extends CI_Controller {
public function __construct(){
parent::__construct();
$this->load->helper('url');
$this->load->model('user_model');
$this->load->library('session');
}
public function index()
{
$this->load->view("register.php");
}
public function register_user(){
$user=array(
'user_name'=>$this->input->post('user_name'),
'user_email'=>$this->input->post('user_email'),
'user_password'=>md5($this->input->post('user_password')),
'user_age'=>$this->input->post('user_age'),
'user_mobile'=>$this->input->post('user_mobile')
);
print_r($user);
$email_check=$this->user_model->email_check($user['user_email']);
if($email_check){
$this->user_model->register_user($user);
$this->session->set_flashdata('success_msg', 'Registered successfully.Now login to your account.');
redirect('user/login_view');
}
else{
$this->session->set_flashdata('error_msg', 'Error occured,Try again.');
redirect('user');
}
}
public function login_view(){
$this->load->view("login.php");
}
function login_user(){
$user_login=array(
'user_email'=>$this->input->post('user_email'),
'user_password'=>md5($this->input->post('user_password'))
);
$data=$this->user_model->login_user($user_login['user_email'],$user_login['user_password']);
if($data)
{
$this->session->set_userdata('user_id',$data['user_id']);
$this->session->set_userdata('user_email',$data['user_email']);
$this->session->set_userdata('user_name',$data['user_name']);
$this->session->set_userdata('user_age',$data['user_age']);
$this->session->set_userdata('user_mobile',$data['user_mobile']);
$this->load->view('user_profile.php');
}
else{
$this->session->set_flashdata('error_msg', 'Error occured,Try again.');
$this->load->view("login.php");
}
}
function user_profile(){
$this->load->view('user_profile.php');
}
public function user_logout(){
$this->session->sess_destroy();
redirect('user/login_view', 'refresh');
}
}
?>
[/code]
Membuat Model : User_model.php
[code hl="1, 4, 7"]
<?php
class User_model extends CI_model{
public function register_user($user){
$this->db->insert('user', $user);
}
public function login_user($email,$pass){
$this->db->select('*');
$this->db->from('user');
$this->db->where('user_email',$email);
$this->db->where('user_password',$pass);
if($query=$this->db->get())
{
return $query->row_array();
}
else{
return false;
}
}
public function email_check($email){
$this->db->select('*');
$this->db->from('user');
$this->db->where('user_email',$email);
$query=$this->db->get();
if($query->num_rows()>0){
return false;
}else{
return true;
}
}
}
?>
[/code]
Membuat View : registrasi.php
[code hl="1, 4, 7"]
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Registration-CI Login Registration</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" media="screen" title="no title">
</head>
<body>
<span style="background-color:red;">
<div class="container"><!-- container class is used to centered the body of the browser with some decent width-->
<div class="row"><!-- row class is used for grid system in Bootstrap-->
<div class="col-md-4 col-md-offset-4"><!--col-md-4 is used to create the no of colums in the grid also use for medimum and large devices-->
<div class="login-panel panel panel-success">
<div class="panel-heading">
<h3 class="panel-title">Registration</h3>
</div>
<div class="panel-body">
<?php
$error_msg=$this->session->flashdata('error_msg');
if($error_msg){
echo $error_msg;
}
?>
<form role="form" method="post" action="<?php echo base_url('user/register_user'); ?>">
<fieldset>
<div class="form-group">
<input class="form-control" placeholder="Name" name="user_name" type="text" autofocus>
</div>
<div class="form-group">
<input class="form-control" placeholder="E-mail" name="user_email" type="email" autofocus>
</div>
<div class="form-group">
<input class="form-control" placeholder="Password" name="user_password" type="password" value="">
</div>
<div class="form-group">
<input class="form-control" placeholder="Age" name="user_age" type="number" value="">
</div>
<div class="form-group">
<input class="form-control" placeholder="Mobile No" name="user_mobile" type="number" value="">
</div>
<input class="btn btn-lg btn-success btn-block" type="submit" value="Register" name="register" >
</fieldset>
</form>
<center><b>Already registered ?</b> <br></b><a href="<?php echo base_url('user/login_view'); ?>">Login here</a></center><!--for centered text-->
</div>
</div>
</div>
</div>
</div>
</span>
</body>
</html>
[/code]
Membuat View : login.php
[code hl="1, 4, 7"]
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Login-CI Login Registration</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" media="screen" title="no title">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-4 col-md-offset-4">
<div class="login-panel panel panel-success">
<div class="panel-heading">
<h3 class="panel-title">Login</h3>
</div>
<?php
$success_msg= $this->session->flashdata('success_msg');
$error_msg= $this->session->flashdata('error_msg');
if($success_msg){
?>
<div class="alert alert-success">
<?php echo $success_msg; ?>
</div>
<?php
}
if($error_msg){
?>
<div class="alert alert-danger">
<?php echo $error_msg; ?>
</div>
<?php
}
?>
<div class="panel-body">
<form role="form" method="post" action="<?php echo base_url('user/login_user'); ?>">
<fieldset>
<div class="form-group" >
<input class="form-control" placeholder="E-mail" name="user_email" type="email" autofocus>
</div>
<div class="form-group">
<input class="form-control" placeholder="Password" name="user_password" type="password" value="">
</div>
<input class="btn btn-lg btn-success btn-block" type="submit" value="login" name="login" >
</fieldset>
</form>
<center><b>Not registered ?</b> <br></b><a href="<?php echo base_url('user'); ?>">Register here</a></center><!--for centered text-->
</div>
</div>
</div>
</div>
</div>
</body>
</html>
[/code]
Membuat View : user_profile.php
[code hl="1, 4, 7"]
<?php
$user_id=$this->session->userdata('user_id');
if(!$user_id){
redirect('user/login_view');
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>User Profile Dashboard-CodeIgniter Login Registration</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-4">
<table class="table table-bordered table-striped">
<tr>
<th colspan="2"><h4 class="text-center">User Info</h3></th>
</tr>
<tr>
<td>User Name</td>
<td><?php echo $this->session->userdata('user_name'); ?></td>
</tr>
<tr>
<td>User Email</td>
<td><?php echo $this->session->userdata('user_email'); ?></td>
</tr>
<tr>
<td>User Age</td>
<td><?php echo $this->session->userdata('user_age'); ?></td>
</tr>
<tr>
<td>User Mobile</td>
<td><?php echo $this->session->userdata('user_mobile'); ?></td>
</tr>
</table>
</div>
</div>
<a href="<?php echo base_url('user/user_logout');?>" > <button type="button" class="btn-primary">Logout</button></a>
</div>
</body>
</html>
[/code]
No comments:
Write comments