Professional Documents
Culture Documents
Lập trình và Thiết kế Web 1 - Bài 9 - PHP nâng cao (download tai tailieutuoi.com)
Lập trình và Thiết kế Web 1 - Bài 9 - PHP nâng cao (download tai tailieutuoi.com)
i 10
PHP nâng cao
Nội dung
Upload File
Gửi Email
Cookies
Sessions
Nội dung
Upload File
Gửi Email
Cookies
Sessions
Upload File
Internet
file or Intranet
Disk
driver
Upload.php
• $_FILES[“file"]["name"]
• $_FILES[“file"]["type"]
• $_FILES[“file"]["size"]
• $_FILES[“file"]["tmp_name"]
• $_FILES["file"]["error"]
• move_uploaded_file(tmpName, savedName)
• file_exists(savedName)
© 2007 Khoa CNTT - ĐHKHTN
Lập trình và Thiết kế Web 1 – Bài 9 : PHP nâng cao
move_uploaded_file(tmp_name, saved_name)
if (($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
&& ($_FILES["file"]["size"] < 20000))
{
if ($_FILES["file"]["error"] > 0)
{
echo “Error Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
// Xu ly luu file
}
}
?>
PHP Warning:
move_uploaded_file(upload/14.jpg)
[function.move-uploaded-file]:
failed to open stream: Permission
denied in .
Nội dung
Upload File
Gửi Email
Cookies
Sessions
Gửi Email
Gửi Email
Internet
or Intranet
Mail Client
mail(to,subject,message…)
Internet
or Intranet
Web Server
Gửi Email
Sử dụng mailto
<A href="mailto:abc@yahoo.com?SUBJECT=Question&BODY=Plz answer">
Format: mailto:address[?key=setting][&key=setting] …
Key Ý nghĩa
CC Danh sách địa chỉ Email người nhận, mỗi địa chỉ cách nhau bằng dấu chấm phẩy (;).
BCC Danh sách địa chỉ Email người nhận được ẩn mỗi địa chỉ cách nhau bằng dấu chấm phẩy (;).
Gửi Email
mail(to,subject,message,headers,parameters)
Key Specifies
Cấu hình mặc định được định nghĩa trong file php.ini
Default Ý nghĩa
SMTP localhost DSN hoặc IP Address của SMTP Server
Ví dụ
<?php
// định nghĩa email người nhận
$to = „noname@yahoo.com';
// tiêu đề
$subject = 'Test email';
// nội dung email
$message = "Hello World!\n\nThis is my first mail.";
// định nghĩa email người gửi và email trả lời
$headers = "From: mymail@yahoo.com\r\nReply-To: replymail@yahoo.com";
// gửi email
$mail_sent = @mail( $to, $subject, $message, $headers );
// kiểm tra gửi thành công và thông báo
echo $mail_sent ? "Mail sent" : "Mail failed";
?>
Gửi Email
Ví dụ
<?php
require_once "Mail.php";
$host = “smtp.gmail.com";
$username = “ttbhanh@gmail.com";
$password = "smtp_password";
$to = “noname@yahoo.com”;
$headers = array ('From' => ‟ttbhanh@gmail.com‟,
'Subject' => „Test send mail‟);
$smtp = Mail::factory('smtp', array ('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $headers, „this is my message‟);
if (PEAR::isError($mail))
echo($mail->getMessage());
else
echo("<p>Message successfully sent!</p>");
?>
Nội dung
Upload File
Gửi Email
Cookies
Sessions
Cookies
cookie
$_COOKIE
setcookie
Client Webserver
Sử dụng Cookies
Ví dụ Cookies
$_COOKIE[fieldName]
cookie
Lưu username & password
setcookie
Client
Webserver
Nội dung
Upload File
Gửi Email
Cookies
Sessions
Session
$_SESSION[sessname]
Cookie: PHPSESSID
session_start
Client Webserver
Là thông tin về client được server lưu trên máy của server
Sử dụng cookie lưu định danh duy nhất cho từng client
Mục đích lưu biến dữ liệu dùng chung cho nhiều trang
trong 1 phiên làm việc của client
Sử dụng Session
Parameter Default
session.auto_start 0 Không tự động
session.cookie_lifetime 0 Tồn tại cho đến
khi đóng browser
session.name PHPSESSID Tên biến cookie
…
Ví dụ Session
Làm thế nào để ngăn không cho người dùng truy cập
vào các trang web nếu chưa đăng nhập?
Ý tưởng
– Dùng các biến Session để lưu trạng thái đăng nhập của người
dùng:
$_SESSION[“IsLogin”] = 0/1 : Lưu trạng thái đăng nhập
$_SESSION[ “Username”] : Lưu Tên đăng nhập
$_SESSION[ “Authentication”] : Lưu Loại quyền đăng
nhập
…
Làm thế nào để ngăn không cho người dùng truy cập
vào các trang web nếu chưa đăng nhập?
Làm thế nào để ngăn không cho người dùng truy cập
vào các trang web nếu chưa đăng nhập?
4. Trong tất cả các trang muốn bảo mật, phải thêm đoạn mã
sau để kiểm tra người dùng đã login hay chưa, nếu chưa
thì redirect lại trang login.htm
<?php
session_start();
if ($_SESSION["IsLogin“] != 1)
header(“Location: login.htm“);
?>
Database/File
So sánh
Session (client1)
cookies Session (client2)
Internet
or Intranet
cookies
Web servers
Clients
Thông tin chia sẽ Vị trí Thời gian Số client Số trang web
lưu trữ tồn tại sử dụng
Database/File Server Dài N N
Session Server Ngắn 1 N
Cookies Client Dài 1 N