โครงสร้างการจัดวางไฟล์
application/
├── controllers/
│ └── Member_login.php
├── models/
│ └── Member_login_model.php
├── views/
│ └── member_login/
│ └── login_view.php
ส่วนของฟอร์ม HTML ( View )
<div class="container">
<h2>Login</h2>
<?php if($this->session->flashdata('error_message')): ?>
<div class="alert alert-danger">
<?php echo $this->session->flashdata('error_message'); ?>
</div>
<?php endif; ?>
<?php echo form_open('member_login/login'); ?>
<div class="form-group">
<label for="username">Username:</label>
<input type="text" class="form-control" id="username" name="username">
</div>
<div class="form-group">
<label for="password">Password:</label>
<input type="password" class="form-control" id="password" name="password">
</div>
<button type="submit" class="btn btn-primary">Login</button>
<?php echo form_close(); ?>
</div>
ส่วนของ Controller
<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* [ Controller File name : Member_login.php ]
*/
class Member_login extends CRUD_Controller
{
private $per_page;
private $another_js;
private $another_css;
public function __construct()
{
parent::__construct();
$this->per_page = 30;
$this->num_links = 6;
$this->uri_segment = 4;
$this->load->model('Member_login_model', 'Login');
$this->data['page_url'] = site_url('member_login');
$this->data['page_title'] = 'PHP CI MANIA';
}
// ------------------------------------------------------------------------
/**
* Index of controller
*/
public function index()
{
$this->login();
}
// ------------------------------------------------------------------------
/**
* Login function
*/
public function login()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'required');
$this->form_validation->set_rules('password', 'Password', 'required');
if ($this->form_validation->run() == TRUE) {
$username = $this->input->post('username');
$password = $this->input->post('password');
// Authenticate user
$user = $this->Login->get_user_by_username($username);
if ($user && password_verify($password, $user['password'])) {
$this->session->set_userdata('user_id', $user['id']);
$this->session->set_userdata('username', $user['username']);
redirect('dashboard'); // Redirect to a dashboard or some other page
} else {
$this->session->set_flashdata('error_message', 'Invalid Username or Password');
redirect('member_login');
}
}
$this->render_view('member_login/login_view');
}
/**
* Logout function
*/
public function logout()
{
$this->session->unset_userdata('user_id');
$this->session->unset_userdata('username');
$this->session->sess_destroy();
redirect('member_login');
}
// ------------------------------------------------------------------------
protected function render_view($path)
{
$this->data['top_navbar'] = $this->parser->parse('template/sb-admin-bs4/top_navbar_view', $this->top_navbar_data, TRUE);
$this->data['left_sidebar'] = $this->parser->parse('template/sb-admin-bs4/left_sidebar_view', $this->left_sidebar_data, TRUE);
$this->data['breadcrumb_list'] = $this->parser->parse('template/sb-admin-bs4/breadcrumb_view', $this->breadcrumb_data, TRUE);
$this->data['page_content'] = $this->parser->parse_repeat($path, $this->data, TRUE);
$this->data['another_css'] = $this->another_css;
$this->data['another_js'] = $this->another_js;
$this->data['utilities_file_time'] = filemtime('assets/js/ci_utilities.js');
$this->parser->parse('template/sb-admin-bs4/homepage_view', $this->data);
}
}
ส่วนของ Model
application/models/Member_login_model.php
<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
class Member_login_model extends CI_Model
{
public function get_user_by_username($username)
{
$this->db->where('username', $username);
$query = $this->db->get('members');
return $query->row_array();
}
}
คำสั่ง SQL สำหรับสร้างฐานข้อมูล
CREATE TABLE `members` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(50) NOT NULL,
`password` varchar(255) NOT NULL,
`email` varchar(100) DEFAULT NULL,
`created_at` datetime DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `username` (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
PHP CI MANIA - PHP Code Generator
โปรแกรมช่วยสร้างโค้ด "ลดเวลาการเขียนโปรแกรม"
ความคิดเห็น
แสดงความคิดเห็น