PHÁT TRIỂN ỨNG DỤNG WEB
Bài 2:
PHP Nâng cao
Nguyễn Hữu Thể
1
Nội dung
▪ PHP Date() Function
▪ PHP Include File
▪ PHP File Handling
▪ PHP File Upload
▪ PHP Cookies
▪ PHP Sessions
▪ PHP Sending E-mails
▪ PHP Error Handling
▪ PHP Exception Handling
2
PHP Date() Function
− Hàm date() định dạng một timestamp để có thể đọc ngày/ giờ.
− Timestamp là một chuỗi ký tự, biểu thị ngày tháng và/ hoặc thời
gian.
❖ Syntax
3
PHP Date() - Format the Date
− Tham số format trong hàm date() để định dạng ngày/ giờ.
▪ d - Đại diện các ngày của tháng (01-31)
▪ m - Đại diện một tháng (01-12)
▪ Y - Đại diện một năm (bốn chữ số)
− Các ký tự: "/", ".", hoặc "-" cũng có thể được chèn vào
4
PHP Date() - Adding a Timestamp
− Tham số tùy chọn timestamp trong hàm date() xác định một dấu
thời gian.
− Nếu không chỉ định timestamp, ngày/giờ hiện tại sẽ được sử
dụng.
− Hàm mktime() trả về Unix timestamp cho một ngày.
❖ Syntax for mktime()
5
PHP Date() - Adding a Timestamp
− Để đến một ngày trong tương lai, thêm đối số ngày của mktime()
6
PHP Include File
− Chèn nội dung của file PHP vào một file PHP, sử dụng hàm:
▪ include(“filename”) hoặc require(“filename”)
▪ Cách khác: include 'filename’ hoặc require 'filename'
− Hai hàm này giống nhau, trừ cách xử lý lỗi:
▪ include() tạo ra một cảnh báo, kịch bản sẽ tiếp tục thực hiện
▪ require() tạo ra một lỗi, và kịch bản sẽ ngừng
− Tạo các hàm, các thành phần tái sử dụng trên nhiều trang.
− Tạo các file header, footer, menu chuẩn cho tất cả các trang.
▪ Khi header cần cập nhật, chỉ cần cập nhật các file,
▪ Khi thêm một trang mới vào trang web, chỉ thay đổi file
menu (thay vì cập nhật các liên kết trên tất cả các trang
web).
7
PHP include() Function
− Hàm include() lấy nội dung trong một file chỉ định vào file
hiện tại.
− Nếu lỗi xảy ra, hàm include() tạo ra một cảnh báo, nhưng kịch
bản sẽ tiếp tục thực hiện.
❖ Example
− include file "header.php" vào một trang, sử dụng hàm include()
8
PHP include() Function
− file "menu.php", nên sử dụng trên tất cả các trang
− Các trang trong Web site nên include file menu này.
9
PHP include() Function
− Nếu lỗi xảy ra, hàm include() tạo ra một cảnh báo, nhưng kịch
bản sẽ tiếp tục thực hiện.
10
PHP require() Function
− Nếu lỗi xảy ra, hàm require() sẽ thông báo lỗi, và kịch bản sẽ
dừng lại.
11
PHP require() Function
− Lưu ý: nên sử dụng hàm require(), vì kịch bản khơng nên tiếp tục
thực hiện sau khi xuất hiện lỗi.
12
PHP File Handling
❖ Opening a File
− Hàm fopen(): mở file trong PHP.
− Tham số đầu tiên chứa tên file được mở và tham số thứ hai quy
định mode của file được mở ra
13
PHP File Handling
− File có thể được mở theo một trong các mode:
14
PHP File Handling
− Tạo ra một thông báo nếu hàm fopen() không thể mở file
15
Closing a File
− Hàm fclose(): đóng một file đã mở.
16
Check End-of-file
− Hàm feof() kiểm tra nếu kết thúc file (“end-of-file” - EOF).
− Dùng kiểm tra các dữ liệu không rõ chiều dài.
❖ Lưu ý: Không thể đọc từ file đã mở trong mode w, a, x.
17
Reading a File Line by Line
− Hàm fgets(): đọc một dòng từ một file.
− Sau khi gọi hàm này, con trỏ file đã di chuyển đến dòng kế tiếp.
18
Reading a File Character by Character
− Hàm fgetc(): đọc một ký tự đơn từ một file.
− Sau khi gọi hàm này, con trỏ file di chuyển đến ký tự tiếp theo.
19
PHP File Upload
− Upload files to the server
❖ Configure The "php.ini" File
− First, ensure that PHP is configured to allow file uploads.
− In your "php.ini" file, search for the file_uploads directive, and
set it to On:
file_uploads = On
20
PHP File Upload - Create The HTML Form
− Create an HTML form that allow users to choose the image file
they want to upload
<!DOCTYPE html>
<html>
<body>
<form action="upload_process.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
</body>
</html>
21
PHP File Upload - Create The Upload File PHP
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
?>
22
File Upload - Check if File Already Exists
− Now we can add some restrictions.
− First, we will check if the file already exists in the "uploads"
folder. If it does, an error message is displayed, and
$uploadOk is set to 0:
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
23
File Upload - Limit File Size
− The file input field in our HTML form above is named
"fileToUpload".
− Check the size of the file. If the file is larger than 500KB, an
error message is displayed, and $uploadOk is set to 0
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
24
File Upload - Limit File Type
− The code below only allows users to upload JPG, JPEG, PNG,
and GIF files.
− All other file types gives an error message before setting
$uploadOk to 0
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" &&
$imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
25