diff --git a/BT/BT1.php b/BT/BT1.php new file mode 100644 index 000000000..4adce3703 --- /dev/null +++ b/BT/BT1.php @@ -0,0 +1,44 @@ + + + + + + Dữ liệu sinh viên đăng ký môn học + + +

Dữ liệu sinh viên đăng ký môn học

+ connect_error) { + die("Connection failed: " . $conn->connect_error); + } + + // Truy vấn dữ liệu từ bảng SinhVien và DangKy + $sql = "SELECT SinhVien.MSSV, SinhVien.HoTen, MonHoc.MaMH, MonHoc.TenMH, DangKy.Ky + FROM SinhVien + INNER JOIN DangKy ON SinhVien.MSSV = DangKy.MSSV + INNER JOIN MonHoc ON DangKy.MaMH = MonHoc.MaMH"; + $result = $conn->query($sql); + + // Kiểm tra và hiển thị dữ liệu + if ($result->num_rows > 0) { + // Duyệt qua từng hàng kết quả + while($row = $result->fetch_assoc()) { + echo "MSSV: " . $row["MSSV"]. " - Họ và tên: " . $row["HoTen"]. " - Mã môn học: " . $row["MaMH"]. " - Tên môn học: " . $row["TenMH"]. " - Kỳ: " . $row["Ky"]. "
"; + } + } else { + echo "Không có dữ liệu"; + } + $conn->close(); + ?> + + \ No newline at end of file diff --git a/BT/BT1.sql b/BT/BT1.sql new file mode 100644 index 000000000..214ac6ab0 --- /dev/null +++ b/BT/BT1.sql @@ -0,0 +1,18 @@ +CREATE TABLE SinhVien ( + MSSV VARCHAR(10) PRIMARY KEY, + HoTen VARCHAR(255) +); + +CREATE TABLE MonHoc ( + MaMH VARCHAR(10) PRIMARY KEY, + TenMH VARCHAR(255) +); + +CREATE TABLE DangKy ( + MSSV VARCHAR(10), + MaMH VARCHAR(10), + Ky VARCHAR(10), + FOREIGN KEY (MSSV) REFERENCES SinhVien(MSSV), + FOREIGN KEY (MaMH) REFERENCES MonHoc(MaMH), + PRIMARY KEY (MSSV, MaMH, Ky) +); diff --git a/BT/Tuan 3/Bai 2/BT2-W3.sql b/BT/Tuan 3/Bai 2/BT2-W3.sql new file mode 100644 index 000000000..9d0fe632d --- /dev/null +++ b/BT/Tuan 3/Bai 2/BT2-W3.sql @@ -0,0 +1,8 @@ +SQL +CREATE TABLE files ( + id INT PRIMARY KEY AUTO_INCREMENT, + filename VARCHAR(255) NOT NULL, + filetype VARCHAR(50) NOT NULL, + upload_date DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, + filesize BIGINT NOT NULL +); diff --git a/BT/Tuan 3/Bai 2/db_config.php b/BT/Tuan 3/Bai 2/db_config.php new file mode 100644 index 000000000..d76adf902 --- /dev/null +++ b/BT/Tuan 3/Bai 2/db_config.php @@ -0,0 +1,13 @@ +setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); +} catch (PDOException $e) { + die("Could not connect to the database $dbname :" . $e->getMessage()); +} +?> diff --git a/BT/Tuan 3/Bai 2/index.php b/BT/Tuan 3/Bai 2/index.php new file mode 100644 index 000000000..ad3e1f11d --- /dev/null +++ b/BT/Tuan 3/Bai 2/index.php @@ -0,0 +1,83 @@ + 2097152) { // 2MB + $error = "Kích thước tệp tin vượt quá giới hạn (2MB)."; + } + + // Kiểm tra loại tệp tin hợp lệ + $allow_types = array('jpg', 'jpeg', 'png', 'gif', 'pdf', 'doc', 'docx', 'xls', 'xlsx'); + if (!in_array($file_type, $allow_types)) { + $error = "Chỉ hỗ trợ các loại tệp tin: " . implode(', ', $allow_types) . "."; + } + + // Nếu không có lỗi, tiến hành tải lên tệp tin + if (!$error) { + // Tạo tên tệp tin mới theo định dạng: YYYYMMDD_HHMMSS_Mã_ngẫu_nhiên.ext + $new_file_name = date('YmdHis') . '_' . uniqid() . '.' . $file_type; + $target_file = $target_dir . $new_file_name; + + // Di chuyển tệp tin từ vị trí tạm thời vào thư mục đích + if (move_uploaded_file($temp_file, $target_file)) { + // Lưu thông tin tệp tin vào CSDL + $sql = "INSERT INTO files (name, type, size, uploaded_at) VALUES ('$new_file_name', '$file_type', '" . $_FILES['myfile']['size'] . "', NOW())"; + mysqli_query($conn, $sql); + + if (mysqli_affected_rows($conn) > 0) { + $success = "Tải lên tệp tin thành công!"; + } else { + $error = "Lỗi khi lưu thông tin tệp tin."; + } + } else { + $error = "Lỗi khi di chuyển tệp tin."; + } + } +} + +// Lấy danh sách tệp tin từ CSDL +$sql = "SELECT * FROM files ORDER BY name ASC"; // Sắp xếp theo tên tệp tin (tăng dần) +if (isset($_GET['sort']) && $_GET['sort'] == 'date') { + $sql = "SELECT * FROM files ORDER BY uploaded_at DESC"; // Sắp xếp theo ngày tải lên (giảm dần) +} +$result = mysqli_query($conn, $sql); + +?> + + + + + + + Quản lý tệp tin + + + +

Quản lý tệp tin

+ + $error

"; ?> + $success

"; ?> + +

Tải lên tệp tin

+
+ + + +
+ +

Danh sách tệp tin

+ + + +
Tên tệp tin diff --git a/BT/Tuan 3/Bai 2/register.php b/BT/Tuan 3/Bai 2/register.php new file mode 100644 index 000000000..5ba5ca465 --- /dev/null +++ b/BT/Tuan 3/Bai 2/register.php @@ -0,0 +1,72 @@ + 0) { + $error = "Tên đăng nhập đã tồn tại."; + } else { + // Mã hóa mật khẩu bằng SHA1 + $hashed_password = sha1($password); + + // Lưu thông tin người dùng vào CSDL + $sql = "INSERT INTO users (username, password, email) VALUES ('$username', '$hashed_password', '$email')"; + mysqli_query($conn, $sql); + + if (mysqli_affected_rows($conn) > 0) { + $success = "Đăng ký thành công!"; + } else { + $error = "Lỗi khi đăng ký."; + } + } + } +} + +?> + + + + + + + Đăng ký + + + +

Đăng ký

+ + $error

"; ?> + $success

"; ?> + +
+ + + + + + + + + + +
+ + diff --git a/BT/Tuan 3/Bai 2/style.css b/BT/Tuan 3/Bai 2/style.css new file mode 100644 index 000000000..ccb67c5e8 --- /dev/null +++ b/BT/Tuan 3/Bai 2/style.css @@ -0,0 +1,53 @@ +body { + font-family: Arial, sans-serif; + margin: 0; + padding: 20px; +} + +h1 { + text-align: center; + margin-bottom: 20px; +} + +form { + width: 300px; + margin: 0 auto; + padding: 20px; + border: 1px solid #ccc; +} + +form label { + display: block; + margin-bottom: 5px; +} + +form input[type="file"] { + width: 100%; + padding: 8px; + border: 1px solid #ccc; + box-sizing: border-box; +} + +form button { + padding: 8px 15px; + background-color: #4CAF50; + color: white; + border: none; + cursor: pointer; +} + +table { + width: 100%; + margin-top: 20px; + border-collapse: collapse; +} + +th, td { + border: 1px solid #ccc; + padding: 8px; + text-align: left; +} + +th { + background-color: #f2f2f2; +} diff --git a/BT/Tuan 3/Bai 2/upload.php b/BT/Tuan 3/Bai 2/upload.php new file mode 100644 index 000000000..45a43d29d --- /dev/null +++ b/BT/Tuan 3/Bai 2/upload.php @@ -0,0 +1,87 @@ + 5000000) { + $message = "Sorry, your file is too large."; + $uploadOk = 0; + } + + // Allow certain file formats + if ($fileType != "jpg" && $fileType != "png" && $fileType != "jpeg" && $fileType != "gif") { + $message = "Sorry, only JPG, JPEG, PNG & GIF files are allowed."; + $uploadOk = 0; + } + + if ($uploadOk == 0) { + $message = "Sorry, your file was not uploaded."; + } else { + if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) { + $sql = "INSERT INTO files (name, type, size) VALUES (?, ?, ?)"; + $stmt = $conn->prepare($sql); + $stmt->execute([ + basename($_FILES["fileToUpload"]["name"]), + $fileType, + $_FILES["fileToUpload"]["size"] + ]); + $message = "The file " . htmlspecialchars(basename($_FILES["fileToUpload"]["name"])) . " has been uploaded."; + } else { + $message = "Sorry, there was an error uploading your file."; + } + } +} + +// Hiển thị danh sách các tệp đã tải lên +$sql = "SELECT * FROM files ORDER BY uploaded_at DESC"; +$files = $conn->query($sql); +?> + + + + + + + File Manager + + + +

File Upload & Display

+
+
+ Select file to upload: + + +
+
+ $message

"; ?> + + + + + + + + + + + + fetch()) : ?> + + + + + + + + +
NameTypeSizeDate Uploaded
+ + diff --git a/BT/Tuan 3/Bai 2/upload/hinh-nen-luffy-gear-5-58.jpg b/BT/Tuan 3/Bai 2/upload/hinh-nen-luffy-gear-5-58.jpg new file mode 100644 index 000000000..4bcbc52d6 Binary files /dev/null and b/BT/Tuan 3/Bai 2/upload/hinh-nen-luffy-gear-5-58.jpg differ diff --git a/BT/Tuan 3/Bai 2/upload/hinh-nen-luffy-gear-5-59.jpg b/BT/Tuan 3/Bai 2/upload/hinh-nen-luffy-gear-5-59.jpg new file mode 100644 index 000000000..d0ed2a5b2 Binary files /dev/null and b/BT/Tuan 3/Bai 2/upload/hinh-nen-luffy-gear-5-59.jpg differ diff --git a/BT/Tuan 3/Bai 3/BT3-W3.sql b/BT/Tuan 3/Bai 3/BT3-W3.sql new file mode 100644 index 000000000..e69de29bb diff --git a/BT/Tuan 3/Bai 3/db_config.php b/BT/Tuan 3/Bai 3/db_config.php new file mode 100644 index 000000000..e69de29bb diff --git a/BT/Tuan 3/Bai 3/index.php b/BT/Tuan 3/Bai 3/index.php new file mode 100644 index 000000000..e69de29bb diff --git a/BT/Tuan 3/Bai 3/register.php b/BT/Tuan 3/Bai 3/register.php new file mode 100644 index 000000000..992912905 --- /dev/null +++ b/BT/Tuan 3/Bai 3/register.php @@ -0,0 +1,72 @@ + 0) { + $error = "Tên đăng nhập đã tồn tại."; + } else { + // Mã hóa mật khẩu bằng SHA1 + $hashed_password = sha1($password); + + // Lưu thông tin người dùng vào CSDL + $sql = "INSERT INTO users (username, password, email) VALUES ('$username', '$hashed_password', '$email')"; + mysqli_query($conn, $sql); + + if (mysqli_affected_rows($conn) > 0) { + $success = "Đăng ký thành công!"; + } else { + $error = "Lỗi khi đăng ký."; + } + } + } +} + +?> + + + + + + + Đăng ký + + + +

Đăng ký

+ + $error

"; ?> + $success

"; ?> + +
+ + + + + + + + + + +
+ + \ No newline at end of file diff --git a/BT/Tuan 3/Bai 3/style.css b/BT/Tuan 3/Bai 3/style.css new file mode 100644 index 000000000..e69de29bb diff --git a/BT/Tuan 3/Bai 3/upload.php b/BT/Tuan 3/Bai 3/upload.php new file mode 100644 index 000000000..e69de29bb diff --git a/BT/Tuan 3/Bai 3/upload/hinh-nen-luffy-gear-5-58.jpg b/BT/Tuan 3/Bai 3/upload/hinh-nen-luffy-gear-5-58.jpg new file mode 100644 index 000000000..4bcbc52d6 Binary files /dev/null and b/BT/Tuan 3/Bai 3/upload/hinh-nen-luffy-gear-5-58.jpg differ diff --git a/BT/Tuan 3/Bai 3/upload/hinh-nen-luffy-gear-5-59.jpg b/BT/Tuan 3/Bai 3/upload/hinh-nen-luffy-gear-5-59.jpg new file mode 100644 index 000000000..d0ed2a5b2 Binary files /dev/null and b/BT/Tuan 3/Bai 3/upload/hinh-nen-luffy-gear-5-59.jpg differ diff --git a/BT/Tuan 3/Bai 4/BT4-W3.sql b/BT/Tuan 3/Bai 4/BT4-W3.sql new file mode 100644 index 000000000..b973c1fea --- /dev/null +++ b/BT/Tuan 3/Bai 4/BT4-W3.sql @@ -0,0 +1,7 @@ +CREATE TABLE files ( + id INT PRIMARY KEY AUTO_INCREMENT, + name VARCHAR(255) NOT NULL, + type VARCHAR(50) NOT NULL, + size BIGINT NOT NULL, + uploaded_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP +); diff --git a/BT/Tuan 3/Bai 4/db_config.php b/BT/Tuan 3/Bai 4/db_config.php new file mode 100644 index 000000000..2361b2620 --- /dev/null +++ b/BT/Tuan 3/Bai 4/db_config.php @@ -0,0 +1,13 @@ + diff --git a/BT/Tuan 3/Bai 4/index.php b/BT/Tuan 3/Bai 4/index.php new file mode 100644 index 000000000..ad3e1f11d --- /dev/null +++ b/BT/Tuan 3/Bai 4/index.php @@ -0,0 +1,83 @@ + 2097152) { // 2MB + $error = "Kích thước tệp tin vượt quá giới hạn (2MB)."; + } + + // Kiểm tra loại tệp tin hợp lệ + $allow_types = array('jpg', 'jpeg', 'png', 'gif', 'pdf', 'doc', 'docx', 'xls', 'xlsx'); + if (!in_array($file_type, $allow_types)) { + $error = "Chỉ hỗ trợ các loại tệp tin: " . implode(', ', $allow_types) . "."; + } + + // Nếu không có lỗi, tiến hành tải lên tệp tin + if (!$error) { + // Tạo tên tệp tin mới theo định dạng: YYYYMMDD_HHMMSS_Mã_ngẫu_nhiên.ext + $new_file_name = date('YmdHis') . '_' . uniqid() . '.' . $file_type; + $target_file = $target_dir . $new_file_name; + + // Di chuyển tệp tin từ vị trí tạm thời vào thư mục đích + if (move_uploaded_file($temp_file, $target_file)) { + // Lưu thông tin tệp tin vào CSDL + $sql = "INSERT INTO files (name, type, size, uploaded_at) VALUES ('$new_file_name', '$file_type', '" . $_FILES['myfile']['size'] . "', NOW())"; + mysqli_query($conn, $sql); + + if (mysqli_affected_rows($conn) > 0) { + $success = "Tải lên tệp tin thành công!"; + } else { + $error = "Lỗi khi lưu thông tin tệp tin."; + } + } else { + $error = "Lỗi khi di chuyển tệp tin."; + } + } +} + +// Lấy danh sách tệp tin từ CSDL +$sql = "SELECT * FROM files ORDER BY name ASC"; // Sắp xếp theo tên tệp tin (tăng dần) +if (isset($_GET['sort']) && $_GET['sort'] == 'date') { + $sql = "SELECT * FROM files ORDER BY uploaded_at DESC"; // Sắp xếp theo ngày tải lên (giảm dần) +} +$result = mysqli_query($conn, $sql); + +?> + + + + + + + Quản lý tệp tin + + + +

Quản lý tệp tin

+ + $error

"; ?> + $success

"; ?> + +

Tải lên tệp tin

+
+ + + +
+ +

Danh sách tệp tin

+ + + +
Tên tệp tin diff --git a/BT/Tuan 3/Bai 4/style.css b/BT/Tuan 3/Bai 4/style.css new file mode 100644 index 000000000..ccb67c5e8 --- /dev/null +++ b/BT/Tuan 3/Bai 4/style.css @@ -0,0 +1,53 @@ +body { + font-family: Arial, sans-serif; + margin: 0; + padding: 20px; +} + +h1 { + text-align: center; + margin-bottom: 20px; +} + +form { + width: 300px; + margin: 0 auto; + padding: 20px; + border: 1px solid #ccc; +} + +form label { + display: block; + margin-bottom: 5px; +} + +form input[type="file"] { + width: 100%; + padding: 8px; + border: 1px solid #ccc; + box-sizing: border-box; +} + +form button { + padding: 8px 15px; + background-color: #4CAF50; + color: white; + border: none; + cursor: pointer; +} + +table { + width: 100%; + margin-top: 20px; + border-collapse: collapse; +} + +th, td { + border: 1px solid #ccc; + padding: 8px; + text-align: left; +} + +th { + background-color: #f2f2f2; +} diff --git a/BT/Tuan 3/Bai 4/upload.php b/BT/Tuan 3/Bai 4/upload.php new file mode 100644 index 000000000..2268fcad4 --- /dev/null +++ b/BT/Tuan 3/Bai 4/upload.php @@ -0,0 +1,52 @@ + 0) { + $row = mysqli_fetch_assoc($result); + + // Xóa tệp tin khỏi ổ cứng + $target_file = "upload/" . $row['name']; + unlink($target_file); + + // Xóa thông tin tệp tin khỏi CSDL + $sql = "DELETE FROM files WHERE id = $file_id"; + mysqli_query($conn, $sql); + + if (mysqli_affected_rows($conn) > 0) { + $success = "Xóa tệp tin thành công!"; + header('Location: index.php'); // Trở về trang chủ sau khi xóa + exit(); + } else { + $error = "Lỗi khi xóa thông tin tệp tin."; + } + } else { + $error = "Tệp tin không tồn tại."; + } +} + +// Lấy danh sách tệp tin từ CSDL (phần này có thể di chuyển vào index.php) +$sql = "SELECT * FROM files ORDER BY name ASC"; // Sắp xếp theo tên tệp tin (tăng dần) +if (isset($_GET['sort']) && $_GET['sort'] == 'date') { + $sql = "SELECT * FROM files ORDER BY uploaded_at DESC"; // Sắp xếp theo ngày tải lên (giảm dần) +} +$result = mysqli_query($conn, $sql); +?> + +**4. File `delete.php`** (trong thư mục `includes`):** + +```php + + + + + + Đăng nhập + + + +
+

Đăng nhập

+
+ + + + + + +
+
+ + diff --git a/BT/Tuan 3/Bai1/logout.php b/BT/Tuan 3/Bai1/logout.php new file mode 100644 index 000000000..d3315d8ed --- /dev/null +++ b/BT/Tuan 3/Bai1/logout.php @@ -0,0 +1,16 @@ + diff --git a/BT/Tuan 3/Bai1/protect.php b/BT/Tuan 3/Bai1/protect.php new file mode 100644 index 000000000..6e92e2013 --- /dev/null +++ b/BT/Tuan 3/Bai1/protect.php @@ -0,0 +1,25 @@ +'; + +// Xử lý form +if (isset($_POST["token"]) && $_POST["token"] === $token) { + // Xử lý form an toàn +} else { + // Lỗi mã token CSRF + echo "Mã token CSRF không hợp lệ!"; +} diff --git a/BT/Tuan 3/Bai1/validateuser.php b/BT/Tuan 3/Bai1/validateuser.php new file mode 100644 index 000000000..01661140d --- /dev/null +++ b/BT/Tuan 3/Bai1/validateuser.php @@ -0,0 +1,41 @@ + 0) { + // Đăng nhập thành công + $_SESSION["IsLogin"] = true; // Lưu trạng thái đăng nhập + header("Location: welcome.php"); // Chuyển hướng đến trang chào mừng + exit(); +} else { + // Đăng nhập thất bại + echo "Tên người dùng hoặc mật khẩu không hợp lệ!"; + header("Location: login.html"); // Chuyển hướng về trang login +} + +// Đóng kết nối CSDL +mysqli_close($conn); \ No newline at end of file diff --git a/BT/Tuan 3/Bai1/welcome.php b/BT/Tuan 3/Bai1/welcome.php new file mode 100644 index 000000000..a2318700a --- /dev/null +++ b/BT/Tuan 3/Bai1/welcome.php @@ -0,0 +1,17 @@ + + + + + + + Trang chủ + + +

Trang chủ

+ +

Chào mừng bạn đến với trang web của chúng tôi!

+
+ +
+ + diff --git a/README.md b/README.md index 6eaa82ecb..3779ab5d6 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,9 @@ -# Try Out Development Containers: PHP +# Development Containers: PHP +Nhóm: + +1. Dang Hoang Anh +2. Nguyen Xuan Hieu +3. Nguyen Le Thu [![Open in Dev Containers](https://img.shields.io/static/v1?label=Dev%20Containers&message=Open&color=blue&logo=visualstudiocode)](https://vscode.dev/redirect?url=vscode://ms-vscode-remote.remote-containers/cloneInVolume?url=https://github.com/microsoft/vscode-remote-try-php)