admin
configs
extends
libraries
models
views
-----admin
-----front
templates
-----admin
-----front
userfiles
<?php
//Thông tin website
define('SITE_URL', 'http://localhost/');
//Thông tin cấu hình DB
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', '');
define('DB_DATABASE', 'training_php_01');
?>
//Ghi chú một dòng
/*
Ghi chú dòng 1
Ghi chú dòng 2
*/
<?php
//Require
require 'configs/config.php';
//Kết nối
$connect = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_DATABASE) or die('Not connected DB!');
//Yêu cầu lưu trữ UTF8 (Tiếng Việt)
mysqli_query('SET NAMES UTF8', $connect);
?>
$a;
$_a;
$a1;
$a = 1;
$b = 'Hello';
<?php
//Khởi động session
session_start();
//Kiểm tra nếu đã đăng nhập thì quay về trang chủ quản trị
if (isset($_SESSION['user'])) {
header('location:../home/home.php');
}
//Require các tập tin cần thiết
require '../../configs/config.php';
require '../../libraries/connect.php';
require '../../models/user.php';
//Kiểm tra dữ liệu POST lên
if (isset($_POST['username']) && !empty($_POST['username']) && isset($_POST['password']) && !empty($_POST['password'])) {
//Gán tài khoản và mật khẩu nhận được từ form vào 2 biến tương ứng
$username = $_POST['username'];
$password = $_POST['password'];
//Lấy thông tin người dùng từ DB
$user = get_user_by_username($username, $connect);
//Kiểm tra sự tồn tại của người dùng và mật khẩu có trùng khớp
if ($user && $user['password'] === md5($password)) {
//Tạo session lưu thông tin người dùng đăng nhập thành công
$_SESSION['user'] = $user;
//Chuyển hướng về trang chủ quản trị
header('location:../home/home.php');
} else {
//Bật cờ lỗi
$error = true;
}
}
//Require tập tin giao diện (View)
require '../../views/admin/user/login.tpl.php';
?>
<?php
function get_user_by_username($username, $connect)
{
//SQL
$sql = "SELECT * FROM tbl_user WHERE username = '$username' AND status = 1";
//Query
$query = mysqli_query($connect, $sql);
//Return
return mysqli_fetch_assoc($query);
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Quản trị - Đăng nhập</title>
</head>
<body>
<form name="login" method="post" action="">
<?php if (isset($error) && $error == true): ?>
<p style="color: red;">Sai Tài khoản hoặc Mật khẩu!</p>
<?php endif; ?>
<p>
<label>Tài khoản:</label>
<input type="text" name="username" value="">
</p>
<p>
<label>Mật khẩu:</label>
<input type="password" name="password" value="">
</p>
<p>
<input type="submit" value="Đăng nhập">
</p>
</form>
</body>
</html>
<?php
//Khởi động session
session_start();
//Hủy toàn bộ session
session_destroy();
//Quay về trang đăng nhập
header('location:login.php');
?>
$_SESSION['Tên_session'] = 'Giá_trị_của_session';
$_SESSION['a'] = 1;
$_SESSION['b'] = 'Hello';
$_SESSION['Tên_session'];
$_SESSION['a'];
if (Điều_kiện) {
//Khối lệnh thỏa điều kiện
}
if (Điều_kiện) {
//Khối lệnh thỏa điều kiện
} else {
//Khối lệnh không thỏa điều kiện
}
if (Điều_kiện_1) {
//Khối lệnh thỏa điều kiện 1
} elseif (Điều_kiện_2) {
//Khối lệnh không thỏa điều kiện 1 nhưng thỏa điều kiện 2
} else {
//Khối lệnh không thỏa điều kiện 1 và 2
}
if (1 == 1) {
echo '1 = 1';
}
if (1 > 2) {
echo '1 > 2';
} else {
echo '1 < 2';
}
switch (Giá_trị_đem_so_sánh) {
case Giá_trị_so_sánh_1:
//Khối lệnh thỏa giá trị so sánh 1
break;
case Giá_trị_so_sánh_2:
//Khối lệnh thỏa giá trị so sánh 2
break;
default:
//Khối lệnh không thỏa giá trị so sánh nào ở trên
}
<?php
//Khởi động session
session_start();
//Kiểm tra nếu chưa đăng nhập thì quay về trang đăng nhập
if (!isset($_SESSION['user'])) {
header('location:login.php');
}
//Require các tập tin cần thiết
require '../../configs/config.php';
require '../../libraries/connect.php';
require '../../models/user.php';
//Lấy danh sách người dùng
$user_list = get_user_list($connect);
//Require tập tin giao diện (View)
require '../../views/admin/user/list.tpl.php';
?>
function get_user_list($connect)
{
//SQL
$sql = "SELECT * FROM tbl_user ORDER BY user_id DESC";
//Return
return mysqli_query($connect, $sql);
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Quản trị - Người dùng - Danh sách</title>
</head>
<body>
<table width="100%" cellpadding="10">
<tr>
<th>ID</th>
<th>Tài khoản</th>
<th>Họ tên</th>
<th>Trạng thái</th>
<th>Ngày tạo</th>
<th>Ngày chỉnh sửa</th>
<th>Tác vụ</th>
</tr>
<?php while ($user = mysqli_fetch_assoc($user_list)): ?>
<tr>
<td>
<?php echo $user['user_id']; ?>
</td>
<td>
<a href="<?php echo SITE_URL . 'admin/user/edit.php?user_id=' . $user['user_id']; ?>"><?php echo $user['username']; ?></a>
</td>
<td>
<?php echo $user['fullname']; ?>
</td>
<td>
<?php echo ($user['status'] == 1) ? 'Kích hoạt' : 'Không kích hoạt'; ?>
</td>
<td>
<?php echo date('d/m/Y H:i:s', strtotime($user['created'])); ?>
</td>
<td>
<?php echo date('d/m/Y H:i:s', strtotime($user['modified'])); ?>
</td>
<td>
<a href="<?php echo SITE_URL . 'admin/user/delete.php?user_id=' . $user['user_id']; ?>">Xóa</a>
</td>
</tr>
<?php endwhile; ?>
</table>
</body>
</html>
function Tên_hàm()
{
//Khối lệnh thực hiện nhiệm vụ nào đó
}
function Tên_hàm(Tham_số_1, Tham_số_2, Tham_số_n)
{
//Khối lệnh thực hiện nhiệm vụ nào đó
}
function Tên_hàm(Tham_số_1 = 'Giá_trị_mặc_định_1', Tham_số_2 = 'Giá_trị_mặc_định_2', Tham_số_n = 'Giá_trị_mặc_định_n')
{
//Khối lệnh thực hiện nhiệm vụ nào đó
}
function get_user_list($connect)
{
//Những dòng lệnh các bạn đã biết ở trên
}
return mysqli_query($connect, $sql);
<?php
//Khởi động session
session_start();
//Kiểm tra nếu chưa đăng nhập thì quay về trang đăng nhập
if (!isset($_SESSION['user'])) {
header('location:login.php');
}
//Require các tập tin cần thiết
require '../../configs/config.php';
require '../../libraries/connect.php';
require '../../models/user.php';
//Nếu có post dữ liệu lên thì xử lý
if ($_POST) {
//Nhận dữ liệu từ form và gán vào một mãng
$data = array(
'username' => $_POST['username'],
'password' => md5($_POST['password']),
'fullname' => $_POST['fullname'],
'email' => $_POST['email'],
'status' => isset($_POST['status']) ? 1 : 0,
'created' => date('Y-m-d H:i:s'),
'modified' => date('Y-m-d H:i:s')
);
//Thêm mới
if (add_user($data, $connect)) {
//Tạo session để lưu cờ thông báo thành công
$_SESSION['success'] = true;
//Tải lại trang (Mục đích là để reset form)
header('location:add.php');
}
}
//Require tập tin giao diện (View)
require '../../views/admin/user/add.tpl.php';
?>
function add_user($data, $connect)
{
//SQL
$sql = "INSERT INTO tbl_user(username, password, fullname, email, status, created, modified) VALUES ('{$data['username']}', '{$data['password']}', '{$data['fullname']}', '{$data['email']}', {$data['status']}, '{$data['created']}', '{$data['modified']}')";
//Return
return mysqli_query($connect, $sql);
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Quản trị - Người dùng - Thêm mới</title>
</head>
<body>
<form name="add" method="post" action="">
<?php if (isset($_SESSION['success'])): ?>
<p style="color: green;">Người dùng đã được thêm mới thành công!</p>
<?php unset($_SESSION['success']); ?>
<?php endif; ?>
<p>
<label>Tài khoản:</label>
<input type="text" name="username" value="">
</p>
<p>
<label>Mật khẩu:</label>
<input type="password" name="password" value="">
</p>
<p>
<label>Họ tên:</label>
<input type="text" name="fullname" value="">
</p>
<p>
<label>Email:</label>
<input type="text" name="email" value="">
</p>
<p>
<label>Trạng thái:</label>
<input type="checkbox" name="status" value="1">
</p>
<p>
<input type="submit" value="Thêm mới">
</p>
</form>
</body>
</html>
<form name="add" method="post" action="">
$_POST['Tên_trường'];
$_POST['username'];
$_POST['password'];
<form name="add" method="get" action="">
urlpath?Biến_1=Giá_trị_1&Biến_2=Giá_trị_2&Biến_n=Giá_trị_n
http://localhost/admin/user/edit.php?user_id=1
$_GET['Tên_trường'];
$_GET['Tên_biến'];
$_GET['user_id'];
$Tên_mãng = array();
$Tên_mãng = array(Phần_tử_1, Phần_tử_2, Phần_tử_n);
$Tên_mãng = array(
'Khóa_1' => 'Giá_trị_1',
'Khóa_2' => 'Giá_trị_2',
'Khóa_n' => 'Giá_trị_n'
);
$Tên_mãng[Chỉ_số];
$Tên_mãng['Tên_khóa'];
cảm ơn bạn
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?