1. PHP trong lập trình web động
Đinh Xuân Lâm
Bộ môn Mạng và Truyền Thông
Khoa CNTT-ĐH CNTT & TT
2. Đọc các biến từ HTML
<form> trong HTML có 2 thuộc tính:
– action=“chuỗi”: Địa chỉ trang web nhận dữ
liệu (Đặt là địa chỉ trang PHP xử lý DL)
– method=“chuỗi”: Phương thức gửi dữ liệu
• GET (mặc định)
• POST
3. Phương thức GET hoặc URL
Truyền dữ liệu (biến) qua URL:
– Các biến được truyền thành từng cặp
biến=giá_trị phân cách bởi dấu &
– Phân cách với địa chỉ trang ban đầu bởi dấu hỏi
chấm (?)
Ví dụ: Truyền 3 biến a, b, c có giá trị lần lượt
là 1, 2, -3 vào trang http://localhost/ptb2.php
qua URL:
http://localhost/ptb2.php?a=1&b=2&c=-3
4. Phương thức GET hoặc URL (tt)
Khi ta Submit 1 form sử dụng phương thức GET,
dữ liệu được truyền qua URL:
– Tên các biến là tên đối tượng trên form
– Giá trị biến là giá trị NSD nhập vào đối tượng
Để đọc giá trị các biến trong PHP: Sử dụng mảng
$_GET, các chỉ số là tên biến.
Ví dụ: Trong trang ptb2.php ở trên đọc các biến
a, b, c:
$a = $_GET[“a”];
$b = $_GET[“b”];
$c = $_GET[“c”];
5. Truyền dữ liệu theo phương thức POST
Dữ liệu của Form post được truyền trong
thân của yêu cầu HTTP
Để đọc dữ liệu sử dụng mảng $_POST với
các chỉ số là tên của đối tượng trên form
gửi đến.
7. Khái niệm cookie
Là những mẩu thông tin nhỏ được lưu trên
máy của người sử dụng thường dùng để
lưu trữ thông tin về người sử dụng.
Khả năng tạo cookie phụ thuộc vào trình
duyệt và sự cho phép của người sử dụng.
Trong IE, để xem các cookies đã được tạo
thì vào: Tools/Internet
Options/Setting…/View Files
8. Thiết lập cookie
setcookie(tên, giá_trị, t)
– Tên: Tên cookie được tạo ra
– giá_trị: giá trị được đặt cho cookie
– t: Số: thời gian hết hạn của cookie
Chú ý:
– Lệnh setcookie phải được gọi trước khi gửi bất cứ
nội dung gì về client (Trước các thẻ HTML, trước
echo, print)
– Để thiết lập thời gian hết hạn của cookie thường sử
dụng hàm
time()+khoảng thời gian (tính bằng
giây)
9. Lệnh đầy đủ
bool setcookie ( string name [, string
value [, int expire [, string path [, string
domain [, bool secure]]]]] )
10. Đọc cookie
Sử dụng mảng $_COOKIE với chỉ các số là
tên các cookie.
Chú ý:
– Ta không thể đọc cookie vừa được thiết lập ngay
trong trong cùng 1 trang vừa thiết lập gọi
setcookie.
13. Các hàm FTP
1. ftp_alloc - Allocates space for a file to be uploaded
2. ftp_cdup - Changes to the parent directory
3. ftp_chdir - Changes the current directory on a FTP server
4. ftp_chmod - Set permissions on a file via FTP
5. ftp_close - Closes an FTP connection
6. ftp_connect - Opens an FTP connection
7. ftp_delete - Deletes a file on the FTP server
8. ftp_exec - Requests execution of a program on the FTP server
9. ftp_fget - Downloads a file from the FTP server and saves to an open file
10. ftp_fput - Uploads from an open file to the FTP server
11. ftp_get_option - Retrieves various runtime behaviours of the current FTP stream
12. ftp_get - Downloads a file from the FTP server
13. ftp_login - Logs in to an FTP connection
14. ftp_mdtm - Returns the last modified time of the given file
15. ftp_mkdir - Creates a directory
16. ftp_nb_continue - Continues retrieving/sending a file (non-blocking)
17. ftp_nb_fget - Retrieves a file from the FTP server and writes it to an open file (non-
blocking)
14. Các hàm FTP (tt)
1. ftp_nb_fput - Stores a file from an open file to the FTP server (non-blocking)
2. ftp_nb_get - Retrieves a file from the FTP server and writes it to a local file (non-
blocking)
3. ftp_nb_put - Stores a file on the FTP server (non-blocking)
4. ftp_nlist - Returns a list of files in the given directory
5. ftp_pasv - Turns passive mode on or off
6. ftp_put - Uploads a file to the FTP server
7. ftp_pwd - Returns the current directory name
8. ftp_quit - Alias of ftp_close
9. ftp_raw - Sends an arbitrary command to an FTP server
10. ftp_rawlist - Returns a detailed list of files in the given directory
11. ftp_rename - Renames a file or a directory on the FTP server
12. ftp_rmdir - Removes a directory
13. ftp_set_option - Set miscellaneous runtime FTP options
14. ftp_site - Sends a SITE command to the server
15. ftp_size - Returns the size of the given file
16. ftp_ssl_connect - Opens an Secure SSL-FTP connection
17. ftp_systype - Returns the system type identifier of the remote FTP server