By GokiSoft.com| 21:13 22/01/2024|
Học PHP

[Share Code] Quản lý sách bằng PHP - trang quản trị sách bằng PHP - Lập trình PHP - C2307L

#config.php

<?php
session_start();

define('HOSTING', 'localhost:3307');
// define('USERNAME', 'gokisoft');
define('USERNAME', 'root');
// define('PWD', 'iv@*NIFyTdKEgh]D');
define('PWD', '');
define('DATABASE', 'library');

/**
* Query: insert, update, delete
*/
function query($sql) {
	// $conn = mysqli_connect('localhost:3307', 'gokisoft', 'iv@*NIFyTdKEgh]D', 'c2307l');
	$conn = mysqli_connect(HOSTING, USERNAME, PWD, DATABASE);
	mysqli_set_charset($conn, 'utf8');

	//B2. Thuc hien insert du lieu vao CSDL
	// $sql = "insert into users (fullname, email, phone_number, address, created_at, updated_at) values ('TRAN VAN AAA', 'tranvana@gmail.com', '1234567890', 'Ha Noi', '2023-01-02 09:30:00', '2023-01-02 09:30:00')";
	// $sql = "delete from users where id = ".$id;
	mysqli_query($conn, $sql);

	//B3. Dong ket noi
	mysqli_close($conn);
}

function select($sql) {
	$conn = mysqli_connect(HOSTING, USERNAME, PWD, DATABASE);
	mysqli_set_charset($conn, 'utf8');

	//B2. Thuc hien insert du lieu vao CSDL
	// $sql = "insert into users (fullname, email, phone_number, address, created_at, updated_at) values ('TRAN VAN AAA', 'tranvana@gmail.com', '1234567890', 'Ha Noi', '2023-01-02 09:30:00', '2023-01-02 09:30:00')";
	// $sql = "select * from users";
	$resultset = mysqli_query($conn, $sql);
	$dataList = [];

	while(($row = mysqli_fetch_array($resultset, 1)) !== null) {
		$dataList[] = $row;
	}

	//B3. Dong ket noi
	mysqli_close($conn);

	return $dataList;
}

function md5Security($str) {
	// return md5('Gokisoft.com'.md5($str).'kdjhgfkdjh3245(*&(jhgf873264KJHKJds');
	return password_hash($str, PASSWORD_DEFAULT);
}

#index.php

<?php
// $value = password_verify("123456", '$2y$10$lWbMGyreZiLIzki6m83MXuktIFl8DO9Mkw30bLiHBHQ81zvM47V0W');
// echo $value;
?>

<!DOCTYPE html>
<html>
<head>
	<title>Home Page</title>
	<meta charset="utf-8">
</head>
<body>
<p>
	<a href="modules/user/login.php">Login Page</a>
</p>
<p>
	<a href="modules/book/index.php">Book Page</a>
</p>
</body>
</html>

#readme.txt

B1) Tao CSDL
create table users (
	id int primary key auto_increment,
	username varchar(50),
	email varchar(150),
	fullname varchar(50),
	phone_number varchar(20),
	password varchar(32)
);

create table books (
	id int primary key auto_increment,
	author_name varchar(50),
	book_name varchar(150),
	price float,
	manufacturer_name varchar(150)
);

B2) Tao project
- Tao cau truc du an
	- entities: ORM (voi moi table trong CSDL -> tao class object tuong ung)
		book.php
		user.php
	- modules
		- book
			index.php
			add.php
			edit.php
			delete.php
		- user
			login.php
			register.php
- Hoan thien entities

B3) Nang cao kien thuc
	- Bao mat -> mat khau
		-> Ma hoa mat khau: ap dung ma hoa 1 chieu
			A original -> encrypt -> B hash (ko the dich nguoc lai thanh A)
			Ly thuyet: md5 -> ma hoa 1 chieu & ko the dich nguoc dc -> table hash
				md5 & bscrypt
	- Keep login
		SESSION -> Xu ly van de nay -> Trong thuc te -> ko lam bang cach nay
		SESSION & COOKIE & DATABASE -> login chuan.

#book.php

<?php
class Book {
	public $id;
	public $authorName;
	public $bookName;
	public $price;
	public $manufacturerName;

	public function processForm() {
		if(isset($_POST['id'])) {
			$this->id = $_POST['id'];
		}
		if(isset($_POST['authorName'])) {
			$this->authorName = $_POST['authorName'];
		}
		if(isset($_POST['bookName'])) {
			$this->bookName = $_POST['bookName'];
		}
		if(isset($_POST['price'])) {
			$this->price = $_POST['price'];
		}
		if(isset($_POST['manufacturerName'])) {
			$this->manufacturerName = $_POST['manufacturerName'];
		}
	}

	public function insert() {
		$sql = "insert into books (author_name, book_name, price, manufaturer_name) values ('".$this->authorName."', '".$this->bookName."', '".$this->price."', '".$this->manufacturerName."')";
		query($sql);
	}

	public function findAll() {
		$sql = "select * from books";
		return select($sql);
	}

	public function update() {
		//Todo
	}

	public function delete() {
		//Todo
	}
}

#user.php

<?php
class Users {
	public $id;
	public $username;
	public $email;
	public $fullname;
	public $phoneNumber;
	public $password;
	public $pwdOriginal;

	/**
	* Su dung method: POST
	*/
	public function processForm() {
		if(isset($_POST['id'])) {
			$this->id = $_POST['id'];
		}
		if(isset($_POST['username'])) {
			$this->username = $_POST['username'];
		}
		if(isset($_POST['email'])) {
			$this->email = $_POST['email'];
		}
		if(isset($_POST['fullname'])) {
			$this->fullname = $_POST['fullname'];
		}
		if(isset($_POST['phoneNumber'])) {
			$this->phoneNumber = $_POST['phoneNumber'];
		}
		if(isset($_POST['password'])) {
			$this->pwdOriginal = $_POST['password'];
			$this->password = $_POST['password'];
			$this->password = md5Security($this->password);
		}
	}

	public function register() {
		$sql = "insert into users (username, email, fullname, phone_number, password) values ('".$this->username."', '".$this->email."', '".$this->fullname."', '".$this->phoneNumber."', '".$this->password."')";
		query($sql);
	}

	/**
	* Su dung ma hoa bscrypt
	*/
	public function login() {
		$sql = "select * from users where email = '".$this->email."'";
		$dataList = select($sql);

		if($dataList != null && count($dataList) > 0) {
			//Login thanh cong
			$pwdHash = $dataList[0]['password'];
			$value = password_verify($this->pwdOriginal, $pwdHash);
			// echo $value;
			if($value) {
				$_SESSION['user'] = $dataList[0];

				return true;
			}
		}
		return false;
	}

	// public function login() {
	// 	$sql = "select * from users where email = '".$this->email."' and password = '".$this->password."'";
	// 	$dataList = select($sql);

	// 	if($dataList != null && count($dataList) > 0) {
	// 		//Login thanh cong
	// 		return true;
	// 	} else {
	// 		//Login fail
	// 		return false;
	// 	}
	// }
}

#add.php

#delete.php

#edit.php

#index.php

<?php
require_once('../../config.php');
require_once('../../entities/book.php');

if(!isset($_SESSION['user'])) {
	header('Location: ../user/login.php');
}

$std = new Book();
$dataList = $std->findAll();
?>

<!DOCTYPE html>
<html>
<head>
	<title>Book Page</title>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0" />
	<!-- Latest compiled and minified CSS -->
	<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
	<!-- Latest compiled JavaScript -->
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
	<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
	<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.1/font/bootstrap-icons.css">
	<style type="text/css">
		.form-group {
			margin-bottom: 20px;
		}
	</style>
</head>
<body>
<div class="container">
	<div class="row">
		<h1 style="color: red; text-align: center">Welcome <?=$_SESSION['user']['fullname']?></h1>
		<div class="col-md-12 mt-3">
			<a href="add.php"><button class="btn btn-success" style="float: left;">Them moi</button></a>
			<form method="get">
				<input type="text" name="s" placeholder="Tim kiem ..." class="form-control" style="width: 200px; float: right;">
			</form>
		</div>
		<div class="col-md-12">
			<div class="card mt-3">
				<div class="card-header bg-info text-white">
					DANH SACH SINH VIEN
				</div>
				<div class="card-body">
					<table class="table table-bordered">
						<thead>
							<tr>
								<th>STT</th>
								<th>Ten Sach</th>
								<th>Tac Gia</th>
								<th>Gia Ban</th>
								<th>Nha SX</th>
								<th style="width: 180px;"></th>
							</tr>
						</thead>
						<tbody>
		<?php
		$count = 0;
		foreach ($dataList as $item) {
			echo '<tr>
					<td>'.(++$count).'</td>
					<td>'.$item['book_name'].'</td>
					<td>'.$item['author_name'].'</td>
					<td>'.$item['price'].'</td>
					<td>'.$item['manufacturer_name'].'</td>
					<td>
						<a href="edit.php?id='.$item['id'].'"><button class="btn btn-warning">Sua</button></a>
						<a href="delete.php?id='.$item['id'].'"><button class="btn btn-danger">Xoa</button></a>
					</td>
				</tr>';
		}
		?>
						</tbody>
					</table>
				</div>
			</div>
		</div>
	</div>
</div>
</body>
</html>

#login.php

<?php
require_once('../../config.php');
require_once('../../entities/user.php');

$title = "";
if(!empty($_POST)) {
	$user = new Users();
	$user->processForm();
	$check = $user->login();

	if($check) {
		header('Location: ../book/index.php');
		die();
	} else {
		$title = "TAI KHOAN KHONG TON TAI";
	}
}
?>

<!DOCTYPE html>
<html>
<head>
	<title>Login Page</title>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0" />
	<!-- Latest compiled and minified CSS -->
	<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
	<!-- Latest compiled JavaScript -->
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
	<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
	<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.1/font/bootstrap-icons.css">
	<style type="text/css">
		.form-group {
			margin-bottom: 20px;
		}
	</style>
</head>
<body>
<div class="container">
	<div class="row">
		<div class="col-md-12 mt-3">
			<form method="post">
				<h1 style="color: red"><?=$title?></h1>
				<div class="form-group">
					<label>Email: </label>
					<input type="text" name="email" placeholder="Enter email" class="form-control">
				</div>
				<div class="form-group">
					<label>Password: </label>
					<input type="password" name="password" placeholder="Enter pwd" class="form-control">
				</div>
				<div class="form-group">
					<button class="btn btn-success">Login</button>
					<p>
						<a href="register.php">Create a new account</a>
					</p>
				</div>
			</form>
		</div>
	</div>
</div>
</body>
</html>

#register.php

<?php
require_once('../../config.php');
require_once('../../entities/user.php');

$title = "";
if(!empty($_POST)) {
	$user = new Users();
	$user->processForm();
	$user->register();

	header('Location: login.php');
	die();
}
?>

<!DOCTYPE html>
<html>
<head>
	<title>Login Page</title>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0" />
	<!-- Latest compiled and minified CSS -->
	<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
	<!-- Latest compiled JavaScript -->
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
	<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
	<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.1/font/bootstrap-icons.css">
	<style type="text/css">
		.form-group {
			margin-bottom: 20px;
		}
	</style>
</head>
<body>
<div class="container">
	<div class="row">
		<div class="col-md-12 mt-3">
			<form method="post">
				<div class="form-group">
					<label>User Name: </label>
					<input required type="text" name="username" placeholder="Enter username" class="form-control">
				</div>
				<div class="form-group">
					<label>Email: </label>
					<input required type="text" name="email" placeholder="Enter email" class="form-control">
				</div>
				<div class="form-group">
					<label>Full Name: </label>
					<input required type="text" name="fullname" placeholder="Enter fullname" class="form-control">
				</div>
				<div class="form-group">
					<label>Phone: </label>
					<input required type="text" name="phoneNumber" placeholder="Enter phone" class="form-control">
				</div>
				<div class="form-group">
					<label>Password: </label>
					<input required type="password" name="password" placeholder="Enter pwd" class="form-control">
				</div>
				<div class="form-group">
					<button class="btn btn-success">Login</button>
					<p>
						<a href="register.php">Create a new account</a>
					</p>
				</div>
			</form>
		</div>
	</div>
</div>
</body>
</html>
Tags:

Phản hồi từ học viên

5

(Dựa trên đánh giá ngày hôm nay)