<?php
class Config
{
//Thông tin website
const SITE_URL = 'http://localhost/';
const BASE_PATH = 'C:\Wamp\www\\';
//Thông tin cấu hình DB
const DB_SERVER = 'localhost';
const DB_USERNAME = 'root';
const DB_PASSWORD = '';
const DB_DATABASE = 'training_php_02';
}
?>
class Tên_lớp
{
//Nội dung của lớp
}
class Person
{}
const Tên_hằng = Giá_trị_của_hằng;
const SITE_URL = 'http://localhost/';
PDF thì mình chưa nghĩ tới nhưng video thì thời gian tới mình sẽ sắp xếp thực hiện bộ video cơ bản trước rồi mới tới bài nâng cao này.mong a làm bàn pdf hoặc video để share ạ
<?php
class MySql
{
protected $connection;
protected $query;
public function __construct()
{
//Kết nối DB
$this->connection = mysqli_connect(Config::DB_SERVER, Config::DB_USERNAME, Config::DB_PASSWORD, Config::DB_DATABASE) or die('Not connected DB!');
//Yêu cầu lưu trữ UTF8 (Tiếng Việt)
mysqli_query('SET NAMES UTF8', $this->connection);
}
public function query($sql)
{
//Truy vấn
return $this->query = mysqli_query($this->connection, $sql);
}
public function fetch()
{
//Lấy dữ liệu
return mysqli_fetch_assoc($this->query);
}
}
?>
<?php
require Config::BASE_PATH . 'libraries/MySql.php';
class Db extends MySql
{}
?>
Phạm_vi $Tên_thuộc_tính;
Phạm_vi $Tên_thuộc_tính = Giá_trị_của_thuộc_tính;
public $a;
protected $b;
private $c;
$this->Tên_thuộc_tính;
self::$Tên_thuộc_tính;
parent::$Tên_thuộc_tính;
$this->a;
self::$b;
parent::$c;
Phạm_vi function Tên_phương_thức()
{
//Nội dung phương thức
}
Phạm_vi function Tên_phương_thức($Tham_số_1, $Tham_số_2, $Tham_số_n)
{
//Nội dung phương thức
}
public function query($sql)
{}
$this->Tên_phương_thức();
self::Tên_phương_thức();
parent::Tên_phương_thức();
$this->query('SELECT * FROM tbl_user');
self::fetch();
parent::get();
class Lớp_dẫn_xuất extends Lớp_cơ_sở
{}
class Db extends MySql
{}
<?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 '../../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'];
//Khởi tạo đối tượng người dùng (User)
$userModel = new User();
//Lấy thông tin người dùng
$user = $userModel->getByUsername($username);
//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->getPassword() === 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
class UserObj
{
protected $userId;
protected $username;
protected $password;
protected $fullname;
protected $email;
protected $status;
protected $created;
protected $modified;
public function setUserId($userId)
{
$this->userId = $userId;
}
public function getUserId()
{
return $this->userId;
}
public function setUsername($username)
{
$this->username = $username;
}
public function getUsername()
{
return $this->username;
}
public function setPassword($password)
{
$this->password = $password;
}
public function getPassword()
{
return $this->password;
}
public function setFullname($fullname)
{
$this->fullname = $fullname;
}
public function getFullname()
{
return $this->fullname;
}
public function setEmail($email)
{
$this->email = $email;
}
public function getEmail()
{
return $this->email;
}
public function setStatus($status)
{
$this->status = $status;
}
public function getStatus()
{
return $this->status;
}
public function setCreated($created)
{
$this->created = $created;
}
public function getCreated()
{
return $this->created;
}
public function setModified($modified)
{
$this->modified = $modified;
}
public function getModified()
{
return $this->modified;
}
}
?>
<?php
require_once Config::BASE_PATH . 'libraries/Db.php';
require_once Config::BASE_PATH . 'models/UserObj.php';
class User
{
protected $db;
public function __construct()
{
$this->db = new Db();
}
public function getByUsername($username)
{
//SQL
$sql = "SELECT * FROM tbl_user WHERE username = '$username' AND status = 1";
//Query
$this->db->query($sql);
//Fetch
$row = $this->db->fetch();
//Khởi tạo đối tượng UserObj
$userObj = new UserObj();
//Gán thông tin
$userObj->setUserId($row['user_id']);
$userObj->setUsername($row['username']);
$userObj->setPassword($row['password']);
$userObj->setFullname($row['fullname']);
$userObj->setEmail($row['email']);
$userObj->setStatus($row['status']);
$userObj->setCreated($row['created']);
$userObj->setModified($row['modified']);
//Return
return $userObj;
}
}
?>
<!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');
?>
$Tên_đối_tượng = new Tên_lớp();
$userObj = new UserObj();
$Tên_đối_tượng->Tên_thuộc_tính;
$Tên_đối_tượng->Tên_phương_thức();
$obj = new Obj();
$obj->propertyOne;
$obj->methodOne();
<?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 '../../models/User.php';
//Khởi tạo đối tượng người dùng (User)
$userModel = new User();
//Lấy danh sách người dùng
$userList = $userModel->getList();
//Tiêu đề trang
$title = 'Người dùng - Danh sách';
//Giao diện (View)
$view = 'user/list.tpl.php';
//Require khung giao diện (Layout)
require '../../views/admin/layout.tpl.php';
?>
public function getList()
{
//SQL
$sql = "SELECT * FROM tbl_user ORDER BY user_id DESC";
//Query
$this->db->query($sql);
//Tạo mãng lưu trữ
$listUser = array();
//Fetch
while ($row = $this->db->fetch()) {
//Khởi tạo đối tượng UserObj
$userObj = new UserObj();
//Gán thông tin
$userObj->setUserId($row['user_id']);
$userObj->setUsername($row['username']);
$userObj->setPassword($row['password']);
$userObj->setFullname($row['fullname']);
$userObj->setEmail($row['email']);
$userObj->setStatus($row['status']);
$userObj->setCreated($row['created']);
$userObj->setModified($row['modified']);
//Gán vào mãng lưu trữ
$listUser[] = $userObj;
}
//Return
return $listUser;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Quản trị - <?php echo $title; ?></title>
</head>
<body>
<?php require Config::BASE_PATH . 'views/admin/' . $view; ?>
</body>
</html>
<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 foreach ($userList as $user): ?>
<tr>
<td>
<?php echo $user->getUserId(); ?>
</td>
<td>
<a href="<?php echo Config::SITE_URL . 'admin/user/edit.php?user_id=' . $user->getUserId(); ?>"><?php echo $user->getUsername(); ?></a>
</td>
<td>
<?php echo $user->getFullname(); ?>
</td>
<td>
<?php echo ($user->getStatus() == 1) ? 'Kích hoạt' : 'Không kích hoạt'; ?>
</td>
<td>
<?php echo date('d/m/Y H:i:s', strtotime($user->getCreated())); ?>
</td>
<td>
<?php echo date('d/m/Y H:i:s', strtotime($user->getModified())); ?>
</td>
<td>
<a href="<?php echo Config::SITE_URL . 'admin/user/delete.php?user_id=' . $user->getUserId(); ?>">Xóa</a>
</td>
</tr>
<?php endforeach; ?>
</table>
<?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 '../../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 (Có thể sử dụng UserObj để lưu dữ liệu)
$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')
);
//Khởi tạo đối tượng người dùng (User)
$userModel = new User();
//Thêm mới
if ($userModel->add($data)) {
//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');
//Ngừng thực thi
exit();
}
}
//Tiêu đề trang
$title = 'Người dùng - Thêm mới';
//Giao diện (View)
$view = 'user/add.tpl.php';
//Require khung giao diện (Layout)
require '../../views/admin/layout.tpl.php';
?>
public function add($data)
{
//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 $this->db->query($sql);
}
<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>
if (!file_exists('path/content.txt')) {
die('File not found!');
} else {
unlink('path/content.txt');
}
try {
//Khối lệnh cần bắt lỗi
} catch(Exception $Biến_exception) {
//Xử lý ngoại lệ
}
try {
if (!file_exists('path/content.txt')) {
//Nếu không tìm thấy tập tin thì lưu thông báo lỗi và chuyển sang catch
throw new Exception('File not found!');
}
//Nếu tìm thấy tập tin
unlink('path/content.txt');
} catch (Exception $e) {
echo $e->getMessage();
}
error_reporting(Cấp_độ_báo_lỗi);
error_reporting(E_ALL);
error_reporting(0);
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?