| Server IP : 127.0.0.1 / Your IP : 192.168.99.1 Web Server : Apache/2.4.35 (Win64) OpenSSL/1.1.1b mod_fcgid/2.3.9 System : Windows NT WIN-JEJMB9EN0L0 10.0 build 14393 (Windows Server 2016) AMD64 User : Administrator ( 0) PHP Version : 7.4.33 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : OFF | Perl : OFF | Python : OFF | Sudo : OFF | Pkexec : OFF Directory : D:/laragon/www/sop2/application/controllers/ |
Upload File : |
<?php
defined('BASEPATH') or exit('No direct script access allowed');
class Auth extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->library('form_validation');
}
public function index()
{
/* if ($this->session->userdata('email')) {
redirect('user');
} */
$this->form_validation->set_rules('email', 'Email', 'required|trim|valid_email');
$this->form_validation->set_rules('password', 'Password', 'required|trim');
if ($this->form_validation->run() == false) {
$data['title'] = 'Login Page';
$this->load->view('templates/auth_header', $data);
$this->load->view('auth/login');
$this->load->view('templates/auth_footer');
} else {
//validasi sukses
$this->_login();
}
}
private function _login()
{
$email = htmlspecialchars($this->input->post('email', true));
$password = htmlspecialchars($this->input->post('password', true));
$user = $this->db->get_where('user', ['email' => $email])->row_array();
if ($user) {
//usernya ada
if ($user['is_active'] == 1) {
//cek passwordnya
if (password_verify($password, $user['password'])) {
$data = [
'email' => $user['email'],
'role_id' => $user['role_id']
];
$this->session->set_userdata($data);
if ($user['role_id'] == 1) {
redirect('admin');
}elseif ($user['role_id'] == 2) {
redirect('action');
} else {
redirect('approve');
}
} else {
$this->session->set_flashdata('message', '<div class="alert alert-danger" role="alert">Wrong password! </div>');
redirect('auth');
}
} else {
$this->session->set_flashdata('message', '<div class="alert alert-danger" role="alert">This email has not been activated! </div>');
redirect('auth');
}
} else {
$this->session->set_flashdata('message', '<div class="alert alert-danger" role="alert">Email is not registered! </div>');
redirect('auth');
}
}
public function registration()
{
if ($this->session->userdata('email')) {
redirect('user');
}
$this->form_validation->set_rules('name', 'Name', 'required|trim');
$this->form_validation->set_rules('email', 'Email', 'required|trim|valid_email|is_unique[user.email]', [
'is_unique' => 'This email has already registered!'
]);
$this->form_validation->set_rules('password1', 'Password', 'required|trim|min_length[3]|matches[password2]', [
'matches' => 'Password dont match!',
'min_length' => 'Password to short!'
]);
$this->form_validation->set_rules('password2', 'Password', 'required|trim|matches[password1]');
if ($this->form_validation->run() == false) {
$data['title'] = 'Create Account';
$this->load->view('templates/auth_header', $data);
$this->load->view('auth/registration');
$this->load->view('templates/auth_footer');
} else {
$data = [
'name' => htmlspecialchars($this->input->post('name', true)),
'email' => htmlspecialchars($this->input->post('email', true)),
'image' => 'default.jpg',
'password' => password_hash($this->input->post('password1'), PASSWORD_DEFAULT),
'role_id' => 2,
'is_active' => 1,
'date_created' => time()
];
$this->db->insert('user', $data);
$this->session->set_flashdata('message', '<div class="alert alert-success" role="alert">Congratulation! your account has been created. Please Login </div>');
redirect('auth');
}
}
public function logout()
{
$this->session->unset_userdata('email');
$this->session->unset_userdata('role_id');
$this->session->set_flashdata('message', '<div class="alert alert-success" role="alert">You have been logged out! </div>');
redirect('auth');
}
public function blocked()
{
$this->load->view('auth/blocked');
}
}