際際滷

際際滷Share a Scribd company logo
L?p tr━nh v┐ Thi?t k? Web 1
? 2007 Khoa C?ng ngh? th?ng tin
Khoa CNTT C ?H.KHTN
i 10
PHP n?ng cao
L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9 : PHP n?ng cao
? 2007 Khoa CNTT - ?HKHTN
N?i dung
? Upload File
? G?i Email
? Cookies
? Sessions
L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9 : PHP n?ng cao
? 2007 Khoa CNTT - ?HKHTN
N?i dung
? Upload File
? G?i Email
? Cookies
? Sessions
L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9 : PHP n?ng cao
? 2007 Khoa CNTT - ?HKHTN
Upload File
? C? ch? Upload File
? Thi?t k? Form cho ph└p Upload File
? X? l? L?u File upload l┷n server
? Ki?m tra l?i, ??nh d?ng, k┴ch th??c File upload
L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9 : PHP n?ng cao
? 2007 Khoa CNTT - ?HKHTN
Webserver
C? ch? Upload File
Internet
or Intranet
Y┷u c?u trang upload.php
Disk
driver
file
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)
L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9 : PHP n?ng cao
? 2007 Khoa CNTT - ?HKHTN
Thi?t k? Form cho ph└p Upload File
? Thi?t l?p thu?c t┴nh Form
C Method : POST
C Enctype: multipart/form-data
--- UploadForm.htm ---
<FORM method="POST" action=^upload.php"
enctype="multipart/form-data">
<input type="FILE" name =^myfile" ><br>
<input type="submit" value=^Submit">
</FORM>
L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9 : PHP n?ng cao
? 2007 Khoa CNTT - ?HKHTN
X? l? l?u File l┷n h? th?ng file system tr┷n Server
? move_uploaded_file(tmp_name, saved_name)
--- Upload.php ---
<?php
move_uploaded_file( $_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo ^Temp. Stored in: " . $_FILES["file"]["tmp_name"];
?>
L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9 : PHP n?ng cao
? 2007 Khoa CNTT - ?HKHTN
Ki?m tra L?i, ??nh d?ng & K┴ch th??c File
--- Upload.php ---
<?php
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
}
}
?>
L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9 : PHP n?ng cao
? 2007 Khoa CNTT - ?HKHTN
C?u h━nh ph?n quy?n WRITE cho th? m?c upload
PHP Warning:
move_uploaded_file(upload/14.jpg)
[function.move-uploaded-file]:
failed to open stream: Permission
denied in .
L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9 : PHP n?ng cao
? 2007 Khoa CNTT - ?HKHTN
N?i dung
? Upload File
? G?i Email
? Cookies
? Sessions
L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9 : PHP n?ng cao
? 2007 Khoa CNTT - ?HKHTN
G?i Email
? C? ch? g?i Email
? G?i mail s? d?ng Outlook
? G?i mail s? d?ng Web Mail
C PHP mail() C Non Authentication
C PHP PEAR package C SMTP Authentication
L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9 : PHP n?ng cao
? 2007 Khoa CNTT - ?HKHTN
G?i Email
? C? ch? g?i Email
? G?i mail s? d?ng Outlook
? G?i mail s? d?ng Web Mail
C PHP mail() C Non Authentication
C PHP PEAR package C SMTP Authentication
L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9 : PHP n?ng cao
? 2007 Khoa CNTT - ?HKHTN
C? ch? g?i Email Mail server
Mail Client
mail(to,subject,message´)
Web Server
Internet
or Intranet
Internet
or Intranet
L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9 : PHP n?ng cao
? 2007 Khoa CNTT - ?HKHTN
C? ch? g?i Email
L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9 : PHP n?ng cao
? 2007 Khoa CNTT - ?HKHTN
V┴ d? C?u h━nh G?i Email s? d?ng GMAIL
L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9 : PHP n?ng cao
? 2007 Khoa CNTT - ?HKHTN
G?i Email
? C? ch? g?i Email
? G?i mail s? d?ng Outlook
? G?i mail s? d?ng Web Mail
C PHP mail() C Non Authentication
C PHP PEAR package C SMTP Authentication
L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9 : PHP n?ng cao
? 2007 Khoa CNTT - ?HKHTN
G?i mail s? d?ng c?u h━nh trong Mail Client - Outlook
? S? d?ng mailto
? Format: mailto:address[?key=setting][&key=setting] ´
<A href="mailto:abc@yahoo.com?SUBJECT=Question&BODY=Plz answer">
Key ? ngh?a
SUBJECT Ti┷u ?? Email
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 (;).
BODY N?i dung Email.
L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9 : PHP n?ng cao
? 2007 Khoa CNTT - ?HKHTN
G?i Email
? C? ch? g?i Email
? G?i mail s? d?ng Outlook
? G?i mail s? d?ng Web Mail
C PHP mail() C Non Authentication
C PHP PEAR package C SMTP Authentication
L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9 : PHP n?ng cao
? 2007 Khoa CNTT - ?HKHTN
G?i mail s? d?ng PHP mail() - Non Authentication
? mail(to,subject,message,headers,parameters)
Key Specifies
TO ??a ch━ email ng??i nh?n
SUBJECT Ti┷u ?? Email (KH?NG ???c ch?a k? t? xu?ng d┛ng)
MESSAGE N?i dung Email
HEADERS
Th?ng tin th┷m (vd: FROM, BCC, CC,´) C│c th?ng tin n┐y n┷n ph?n c│ch nhau
b?ng k? t? xu?ng d┛ng (rn)
PARAMETERS Tham s? c?u h━nh cho ?ng d?ng g?i mail
L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9 : PHP n?ng cao
? 2007 Khoa CNTT - ?HKHTN
C?u h━nh m?c ??nh c?a PHP mail() C Non Authentication
? 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
smtp_port 25 Port c?a SMTP
sendmail_from NULL ??a ch? ng??i g?i
sendmail_path NULL ???ng d?n ??n ?ng d?ng g?i mail
(UNIX)
L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9 : PHP n?ng cao
? 2007 Khoa CNTT - ?HKHTN
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!nnThis is my first mail.";
// ??nh ngh?a email ng??i g?i v┐ email tr? l?i
$headers = "From: mymail@yahoo.comrnReply-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";
?>
L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9 : PHP n?ng cao
? 2007 Khoa CNTT - ?HKHTN
G?i Email
? C? ch? g?i Email
? G?i mail s? d?ng Outlook
? G?i mail s? d?ng Web Mail
C PHP mail() C Non Authentication
C PHP PEAR package C SMTP Authentication
L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9 : PHP n?ng cao
? 2007 Khoa CNTT - ?HKHTN
G?i mail s? d?ng PHP PEAR package C SMTP Authentication
1. S? d?ng PEAR package Mail.php
C require_once(?Mail.php?);
2. Kh?i t?o th? hi?n ?? g?i mail s? d?ng Mail::factory()
C $smtp = Mail::factory(?smtp?, $params);
? $params[^host ̄] = ^hostname ̄;
? $params[^auth ̄] = true;
? $params[^username ̄] = ^smtp_username ̄;
? $params[^password ̄] = ^smtp_password ̄;
3. G?i mail s? d?ng h┐m send()
C $mail = $smtp->send($to, $headers, $message)
? $headers['From'] = 'from@example.com';
? $headers['To'] = $to;
? $headers['Subject'] = 'Test message';
? $headers['Cc'] = 'cc@example.com';
? $headers['Reply-To'] = 'from@example.com';
4. Ki?m tra l?i g?i mail
C if (PEAR::isError($mail))
echo($mail->getMessage());
L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9 : PHP n?ng cao
? 2007 Khoa CNTT - ?HKHTN
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>");
?>
L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9 : PHP n?ng cao
? 2007 Khoa CNTT - ?HKHTN
N?i dung
? Upload File
? G?i Email
? Cookies
? Sessions
L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9 : PHP n?ng cao
? 2007 Khoa CNTT - ?HKHTN
Cookies
? L┐ 1 t?p tin ???c server l?u xu?ng m│y c?a client
? M?i l?n client g?i request 1 trang web, ??ng th?i s? g?i file
cookie n┐y l┷n server
? C│c ?i?u khi?n x? l? th?ng tin (l?u, l?y) do server th?c hi?n
? Th??ng ???c s? d?ng ?? l?u th?ng tin c│ nh?n c?a client
Webserver
cookie
setcookie
$_COOKIE
Client
L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9 : PHP n?ng cao
? 2007 Khoa CNTT - ?HKHTN
S? d?ng Cookies
? L?nh ghi cookie
C setcookie(name, value, expire, path, domain);
C setrawcookie(name, value, expire, path, domain);
? Name: t┷n cookie
? Value: gi│ tr? cookie
? Expire: ng┐y/kho?ng th?i gian h?t h?n h?t h?n
? Path : ???ng d?n m┐ cookie ???c g?i ?i
? Domain : x│c ??nh t┷n mi?n m┐ cookie ???c g?i ?i
? B?t bu?c ph?i xu?t hi?n tr??c th? <html>
? L?y gi│ tr? cookie
C echo $_COOKIE[^cookieName"];
? X┏a cookie
C setcookie(^cookieName", "", time()-3600);
L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9 : PHP n?ng cao
? 2007 Khoa CNTT - ?HKHTN
V┴ d? Cookies
? L?u th?ng tin ??ng nh?p
cookie
setcookie
$_COOKIE[fieldName]
Client
L?u username & password
Webserver
L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9 : PHP n?ng cao
? 2007 Khoa CNTT - ?HKHTN
N?i dung
? Upload File
? G?i Email
? Cookies
? Sessions
L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9 : PHP n?ng cao
? 2007 Khoa CNTT - ?HKHTN
Session
? 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
Webserver
Cookie: PHPSESSID
session_start
$_SESSION[sessname]
Client
L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9 : PHP n?ng cao
? 2007 Khoa CNTT - ?HKHTN
S? d?ng Session
? Kh?i ??ng Session
C session_start();
? B?t bu?c ph?i xu?t hi?n tr??c th? <html>
? Ghi & ??c gi│ tr? Session
C $_SESSION[^sessionName ̄] = value
C $_SESSION[^sessionName ̄][ ] = array(´)
C if (isset($_SESSION[^sessionName"])
echo $_SESSION[^sessionName"];
? H?y Session
C session_destroy();
L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9 : PHP n?ng cao
? 2007 Khoa CNTT - ?HKHTN
C?u h━nh Session
? C?u h━nh trong php.ini
? ini_set(`parameter¨, value);
C G?i tr??c h┐m session_start();
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
´
L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9 : PHP n?ng cao
? 2007 Khoa CNTT - ?HKHTN
V┴ d? Session
? ??m s? l?n duy?t 1 trang web c?a 1 user
<?php
session_start( );
if (isset($_SESSION["count"]))
$_SESSION["count"] = $_SESSION["count"] + 1;
else
$_SESSION["count"] = 1;
print "You've looked at this page " . $_SESSION['count'] .
' times.';
?>
L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9 : PHP n?ng cao
? 2007 Khoa CNTT - ?HKHTN
?ng d?ng 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
C 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?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9 : PHP n?ng cao
? 2007 Khoa CNTT - ?HKHTN
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?
1. T?o trang login.htm y┷u c?u ng??i d┫ng ??ng nh?p.
2. T?o trang validateuser.php l┐ trang x? l┴ th?ng tin ??ng nh?p t? trang
login
C K?t n?i v?i CSDL, ki?m tra xem th?ng tin ??ng nh?p c┏ ?┣ng hay kh?ng.
C N?u kh?ng ?┣ng th━ cho redirect ??n trang login.htm.
C C┛n n?u ??NG th━ d┫ng m?t bi?n Session ?? l?u tr?ng th│i login th┐nh
c?ng l?i.
? V┴ d? $_SESSION["IsLogin^] = 1.
? L?u ? l┐ ph?i ??t gi│ tr? m?c ??nh cho bi?n n┐y l┐ 0 khi kh?i t?o m?t
Session.
3. T?o trang logout.php l┐ trang x? l? khi ng??i d┫ng logout
? Reset tr?ng th│i login l┐ ch?a ??ng nh?p
L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9 : PHP n?ng cao
? 2007 Khoa CNTT - ?HKHTN
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^);
?>
L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9 : PHP n?ng cao
? 2007 Khoa CNTT - ?HKHTN
So s│nh
Clients
Web servers
Database/File
Session (client1)
Session (client2)cookies
cookies
Th?ng tin chia s? V? tr┴
l?u tr?
Th?i gian
t?n t?i
S? client S? trang web
s? d?ng
Database/File Server D┐i N N
Session Server Ng?n 1 N
Cookies Client D┐i 1 N
Internet
or Intranet

More Related Content

What's hot (10)

際際滷 01
際際滷 01際際滷 01
際際滷 01
tuanduongcntt
?
Php03 php trong-laptrinhwebdong
Php03 php trong-laptrinhwebdongPhp03 php trong-laptrinhwebdong
Php03 php trong-laptrinhwebdong
conglongit90
?
際際滷 00 gioi thieu
際際滷 00   gioi thieu際際滷 00   gioi thieu
際際滷 00 gioi thieu
tuanduongcntt
?
Web301 slide 1
Web301   slide 1Web301   slide 1
Web301 slide 1
tuanduongcntt
?
H?i t━nh h━nh bk tiny bktiny-hdsd
H?i t━nh h━nh bk tiny   bktiny-hdsdH?i t━nh h━nh bk tiny   bktiny-hdsd
H?i t━nh h━nh bk tiny bktiny-hdsd
Vu Hung Nguyen
?
L?p tr━nh web - HTML c? b?n
L?p tr━nh web - HTML c? b?nL?p tr━nh web - HTML c? b?n
L?p tr━nh web - HTML c? b?n
Nh┏c Nh┏c
?
B┐i 6 T?i file v┐ h━nh ?nh l┷n website - X?y d?ng ?ng d?ng web
B┐i 6 T?i file v┐ h━nh ?nh l┷n website - X?y d?ng ?ng d?ng webB┐i 6 T?i file v┐ h━nh ?nh l┷n website - X?y d?ng ?ng d?ng web
B┐i 6 T?i file v┐ h━nh ?nh l┷n website - X?y d?ng ?ng d?ng web
MasterCode.vn
?
Web301 slide 6
Web301   slide 6Web301   slide 6
Web301 slide 6
tuanduongcntt
?
Php03 php trong-laptrinhwebdong
Php03 php trong-laptrinhwebdongPhp03 php trong-laptrinhwebdong
Php03 php trong-laptrinhwebdong
conglongit90
?
際際滷 00 gioi thieu
際際滷 00   gioi thieu際際滷 00   gioi thieu
際際滷 00 gioi thieu
tuanduongcntt
?
H?i t━nh h━nh bk tiny bktiny-hdsd
H?i t━nh h━nh bk tiny   bktiny-hdsdH?i t━nh h━nh bk tiny   bktiny-hdsd
H?i t━nh h━nh bk tiny bktiny-hdsd
Vu Hung Nguyen
?
L?p tr━nh web - HTML c? b?n
L?p tr━nh web - HTML c? b?nL?p tr━nh web - HTML c? b?n
L?p tr━nh web - HTML c? b?n
Nh┏c Nh┏c
?
B┐i 6 T?i file v┐ h━nh ?nh l┷n website - X?y d?ng ?ng d?ng web
B┐i 6 T?i file v┐ h━nh ?nh l┷n website - X?y d?ng ?ng d?ng webB┐i 6 T?i file v┐ h━nh ?nh l┷n website - X?y d?ng ?ng d?ng web
B┐i 6 T?i file v┐ h━nh ?nh l┷n website - X?y d?ng ?ng d?ng web
MasterCode.vn
?

Similar to Php nang cao (20)

PHP.pdf
PHP.pdfPHP.pdf
PHP.pdf
TinPh6
?
際際滷 01-Web Application Overview (1).pdf
際際滷 01-Web Application Overview (1).pdf際際滷 01-Web Application Overview (1).pdf
際際滷 01-Web Application Overview (1).pdf
HuyVnh4
?
Postfix Dovecot CentOS 6.5
Postfix Dovecot CentOS 6.5Postfix Dovecot CentOS 6.5
Postfix Dovecot CentOS 6.5
thienma217
?
(274364013) baibaocaogiuaky 140505213913-phpapp02
(274364013) baibaocaogiuaky 140505213913-phpapp02(274364013) baibaocaogiuaky 140505213913-phpapp02
(274364013) baibaocaogiuaky 140505213913-phpapp02
lehuyvinh
?
CVNguyenThanhLam-102015-Vi
CVNguyenThanhLam-102015-ViCVNguyenThanhLam-102015-Vi
CVNguyenThanhLam-102015-Vi
Lam Nguyen
?
Reveal the Power of Php
Reveal the Power of PhpReveal the Power of Php
Reveal the Power of Php
V? Duy Tu?n
?
Bai1 gioi thieu_servlet_va_jsp_8952
Bai1 gioi thieu_servlet_va_jsp_8952Bai1 gioi thieu_servlet_va_jsp_8952
Bai1 gioi thieu_servlet_va_jsp_8952
Ham Ch?i
?
H?c l?p tr━nh Wordpress - b┐i 2 - T━m hi?u c?u tr┣c v┐ c?u h━nh n?ng cao
H?c l?p tr━nh Wordpress - b┐i 2 - T━m hi?u c?u tr┣c v┐ c?u h━nh n?ng cao H?c l?p tr━nh Wordpress - b┐i 2 - T━m hi?u c?u tr┣c v┐ c?u h━nh n?ng cao
H?c l?p tr━nh Wordpress - b┐i 2 - T━m hi?u c?u tr┣c v┐ c?u h━nh n?ng cao
KhanhPham
?
Bai th08 php voi csdl
Bai th08 php voi csdlBai th08 php voi csdl
Bai th08 php voi csdl
Giang Nguy?n
?
Ki?n th?c c?n thi?t l┐m vi?c
Ki?n th?c c?n thi?t l┐m vi?cKi?n th?c c?n thi?t l┐m vi?c
Ki?n th?c c?n thi?t l┐m vi?c
manhvokiem
?
PHP c┏ th? l┐m g━? 9 Th? th┣ v? c┏ th? l┐m v?i PHP
PHP c┏ th? l┐m g━?  9 Th? th┣ v? c┏ th? l┐m v?i PHPPHP c┏ th? l┐m g━?  9 Th? th┣ v? c┏ th? l┐m v?i PHP
PHP c┏ th? l┐m g━? 9 Th? th┣ v? c┏ th? l┐m v?i PHP
NIIT - ICT H┐ N?i
?
9 application layer
9 application layer9 application layer
9 application layer
Nguyen Ba Hoang
?
php_nangcao [compatibility mode]
php_nangcao [compatibility mode]php_nangcao [compatibility mode]
php_nangcao [compatibility mode]
vokhuyet187
?
Lab lap trinhmang_v3
Lab lap trinhmang_v3Lab lap trinhmang_v3
Lab lap trinhmang_v3
hieunguyen929915
?
[ST] T┐i li?u thi?t k? website cho ng??i m?i b?t ??u
[ST] T┐i li?u thi?t k? website cho ng??i m?i b?t ??u[ST] T┐i li?u thi?t k? website cho ng??i m?i b?t ??u
[ST] T┐i li?u thi?t k? website cho ng??i m?i b?t ??u
Tin H?c KEY
?
Python moi
Python moiPython moi
Python moi
D?p L?
?
Web1012 slide 1
Web1012   slide 1Web1012   slide 1
Web1012 slide 1
C┐ R?t
?
Htaccess
HtaccessHtaccess
Htaccess
L┷ T┣
?
DoThanhNghi2016_Python.pdf
DoThanhNghi2016_Python.pdfDoThanhNghi2016_Python.pdf
DoThanhNghi2016_Python.pdf
TamDo58
?
際際滷 01-Web Application Overview (1).pdf
際際滷 01-Web Application Overview (1).pdf際際滷 01-Web Application Overview (1).pdf
際際滷 01-Web Application Overview (1).pdf
HuyVnh4
?
Postfix Dovecot CentOS 6.5
Postfix Dovecot CentOS 6.5Postfix Dovecot CentOS 6.5
Postfix Dovecot CentOS 6.5
thienma217
?
(274364013) baibaocaogiuaky 140505213913-phpapp02
(274364013) baibaocaogiuaky 140505213913-phpapp02(274364013) baibaocaogiuaky 140505213913-phpapp02
(274364013) baibaocaogiuaky 140505213913-phpapp02
lehuyvinh
?
CVNguyenThanhLam-102015-Vi
CVNguyenThanhLam-102015-ViCVNguyenThanhLam-102015-Vi
CVNguyenThanhLam-102015-Vi
Lam Nguyen
?
Reveal the Power of Php
Reveal the Power of PhpReveal the Power of Php
Reveal the Power of Php
V? Duy Tu?n
?
Bai1 gioi thieu_servlet_va_jsp_8952
Bai1 gioi thieu_servlet_va_jsp_8952Bai1 gioi thieu_servlet_va_jsp_8952
Bai1 gioi thieu_servlet_va_jsp_8952
Ham Ch?i
?
H?c l?p tr━nh Wordpress - b┐i 2 - T━m hi?u c?u tr┣c v┐ c?u h━nh n?ng cao
H?c l?p tr━nh Wordpress - b┐i 2 - T━m hi?u c?u tr┣c v┐ c?u h━nh n?ng cao H?c l?p tr━nh Wordpress - b┐i 2 - T━m hi?u c?u tr┣c v┐ c?u h━nh n?ng cao
H?c l?p tr━nh Wordpress - b┐i 2 - T━m hi?u c?u tr┣c v┐ c?u h━nh n?ng cao
KhanhPham
?
Bai th08 php voi csdl
Bai th08 php voi csdlBai th08 php voi csdl
Bai th08 php voi csdl
Giang Nguy?n
?
Ki?n th?c c?n thi?t l┐m vi?c
Ki?n th?c c?n thi?t l┐m vi?cKi?n th?c c?n thi?t l┐m vi?c
Ki?n th?c c?n thi?t l┐m vi?c
manhvokiem
?
PHP c┏ th? l┐m g━? 9 Th? th┣ v? c┏ th? l┐m v?i PHP
PHP c┏ th? l┐m g━?  9 Th? th┣ v? c┏ th? l┐m v?i PHPPHP c┏ th? l┐m g━?  9 Th? th┣ v? c┏ th? l┐m v?i PHP
PHP c┏ th? l┐m g━? 9 Th? th┣ v? c┏ th? l┐m v?i PHP
NIIT - ICT H┐ N?i
?
php_nangcao [compatibility mode]
php_nangcao [compatibility mode]php_nangcao [compatibility mode]
php_nangcao [compatibility mode]
vokhuyet187
?
[ST] T┐i li?u thi?t k? website cho ng??i m?i b?t ??u
[ST] T┐i li?u thi?t k? website cho ng??i m?i b?t ??u[ST] T┐i li?u thi?t k? website cho ng??i m?i b?t ??u
[ST] T┐i li?u thi?t k? website cho ng??i m?i b?t ??u
Tin H?c KEY
?
Python moi
Python moiPython moi
Python moi
D?p L?
?
Web1012 slide 1
Web1012   slide 1Web1012   slide 1
Web1012 slide 1
C┐ R?t
?
DoThanhNghi2016_Python.pdf
DoThanhNghi2016_Python.pdfDoThanhNghi2016_Python.pdf
DoThanhNghi2016_Python.pdf
TamDo58
?

Php nang cao

  • 1. L?p tr━nh v┐ Thi?t k? Web 1 ? 2007 Khoa C?ng ngh? th?ng tin Khoa CNTT C ?H.KHTN i 10 PHP n?ng cao
  • 2. L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9 : PHP n?ng cao ? 2007 Khoa CNTT - ?HKHTN N?i dung ? Upload File ? G?i Email ? Cookies ? Sessions
  • 3. L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9 : PHP n?ng cao ? 2007 Khoa CNTT - ?HKHTN N?i dung ? Upload File ? G?i Email ? Cookies ? Sessions
  • 4. L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9 : PHP n?ng cao ? 2007 Khoa CNTT - ?HKHTN Upload File ? C? ch? Upload File ? Thi?t k? Form cho ph└p Upload File ? X? l? L?u File upload l┷n server ? Ki?m tra l?i, ??nh d?ng, k┴ch th??c File upload
  • 5. L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9 : PHP n?ng cao ? 2007 Khoa CNTT - ?HKHTN Webserver C? ch? Upload File Internet or Intranet Y┷u c?u trang upload.php Disk driver file 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)
  • 6. L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9 : PHP n?ng cao ? 2007 Khoa CNTT - ?HKHTN Thi?t k? Form cho ph└p Upload File ? Thi?t l?p thu?c t┴nh Form C Method : POST C Enctype: multipart/form-data --- UploadForm.htm --- <FORM method="POST" action=^upload.php" enctype="multipart/form-data"> <input type="FILE" name =^myfile" ><br> <input type="submit" value=^Submit"> </FORM>
  • 7. L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9 : PHP n?ng cao ? 2007 Khoa CNTT - ?HKHTN X? l? l?u File l┷n h? th?ng file system tr┷n Server ? move_uploaded_file(tmp_name, saved_name) --- Upload.php --- <?php move_uploaded_file( $_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]); echo "Upload: " . $_FILES["file"]["name"] . "<br />"; echo "Type: " . $_FILES["file"]["type"] . "<br />"; echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />"; echo ^Temp. Stored in: " . $_FILES["file"]["tmp_name"]; ?>
  • 8. L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9 : PHP n?ng cao ? 2007 Khoa CNTT - ?HKHTN Ki?m tra L?i, ??nh d?ng & K┴ch th??c File --- Upload.php --- <?php 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 } } ?>
  • 9. L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9 : PHP n?ng cao ? 2007 Khoa CNTT - ?HKHTN C?u h━nh ph?n quy?n WRITE cho th? m?c upload PHP Warning: move_uploaded_file(upload/14.jpg) [function.move-uploaded-file]: failed to open stream: Permission denied in .
  • 10. L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9 : PHP n?ng cao ? 2007 Khoa CNTT - ?HKHTN N?i dung ? Upload File ? G?i Email ? Cookies ? Sessions
  • 11. L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9 : PHP n?ng cao ? 2007 Khoa CNTT - ?HKHTN G?i Email ? C? ch? g?i Email ? G?i mail s? d?ng Outlook ? G?i mail s? d?ng Web Mail C PHP mail() C Non Authentication C PHP PEAR package C SMTP Authentication
  • 12. L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9 : PHP n?ng cao ? 2007 Khoa CNTT - ?HKHTN G?i Email ? C? ch? g?i Email ? G?i mail s? d?ng Outlook ? G?i mail s? d?ng Web Mail C PHP mail() C Non Authentication C PHP PEAR package C SMTP Authentication
  • 13. L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9 : PHP n?ng cao ? 2007 Khoa CNTT - ?HKHTN C? ch? g?i Email Mail server Mail Client mail(to,subject,message´) Web Server Internet or Intranet Internet or Intranet
  • 14. L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9 : PHP n?ng cao ? 2007 Khoa CNTT - ?HKHTN C? ch? g?i Email
  • 15. L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9 : PHP n?ng cao ? 2007 Khoa CNTT - ?HKHTN V┴ d? C?u h━nh G?i Email s? d?ng GMAIL
  • 16. L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9 : PHP n?ng cao ? 2007 Khoa CNTT - ?HKHTN G?i Email ? C? ch? g?i Email ? G?i mail s? d?ng Outlook ? G?i mail s? d?ng Web Mail C PHP mail() C Non Authentication C PHP PEAR package C SMTP Authentication
  • 17. L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9 : PHP n?ng cao ? 2007 Khoa CNTT - ?HKHTN G?i mail s? d?ng c?u h━nh trong Mail Client - Outlook ? S? d?ng mailto ? Format: mailto:address[?key=setting][&key=setting] ´ <A href="mailto:abc@yahoo.com?SUBJECT=Question&BODY=Plz answer"> Key ? ngh?a SUBJECT Ti┷u ?? Email 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 (;). BODY N?i dung Email.
  • 18. L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9 : PHP n?ng cao ? 2007 Khoa CNTT - ?HKHTN G?i Email ? C? ch? g?i Email ? G?i mail s? d?ng Outlook ? G?i mail s? d?ng Web Mail C PHP mail() C Non Authentication C PHP PEAR package C SMTP Authentication
  • 19. L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9 : PHP n?ng cao ? 2007 Khoa CNTT - ?HKHTN G?i mail s? d?ng PHP mail() - Non Authentication ? mail(to,subject,message,headers,parameters) Key Specifies TO ??a ch━ email ng??i nh?n SUBJECT Ti┷u ?? Email (KH?NG ???c ch?a k? t? xu?ng d┛ng) MESSAGE N?i dung Email HEADERS Th?ng tin th┷m (vd: FROM, BCC, CC,´) C│c th?ng tin n┐y n┷n ph?n c│ch nhau b?ng k? t? xu?ng d┛ng (rn) PARAMETERS Tham s? c?u h━nh cho ?ng d?ng g?i mail
  • 20. L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9 : PHP n?ng cao ? 2007 Khoa CNTT - ?HKHTN C?u h━nh m?c ??nh c?a PHP mail() C Non Authentication ? 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 smtp_port 25 Port c?a SMTP sendmail_from NULL ??a ch? ng??i g?i sendmail_path NULL ???ng d?n ??n ?ng d?ng g?i mail (UNIX)
  • 21. L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9 : PHP n?ng cao ? 2007 Khoa CNTT - ?HKHTN 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!nnThis is my first mail."; // ??nh ngh?a email ng??i g?i v┐ email tr? l?i $headers = "From: mymail@yahoo.comrnReply-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"; ?>
  • 22. L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9 : PHP n?ng cao ? 2007 Khoa CNTT - ?HKHTN G?i Email ? C? ch? g?i Email ? G?i mail s? d?ng Outlook ? G?i mail s? d?ng Web Mail C PHP mail() C Non Authentication C PHP PEAR package C SMTP Authentication
  • 23. L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9 : PHP n?ng cao ? 2007 Khoa CNTT - ?HKHTN G?i mail s? d?ng PHP PEAR package C SMTP Authentication 1. S? d?ng PEAR package Mail.php C require_once(?Mail.php?); 2. Kh?i t?o th? hi?n ?? g?i mail s? d?ng Mail::factory() C $smtp = Mail::factory(?smtp?, $params); ? $params[^host ̄] = ^hostname ̄; ? $params[^auth ̄] = true; ? $params[^username ̄] = ^smtp_username ̄; ? $params[^password ̄] = ^smtp_password ̄; 3. G?i mail s? d?ng h┐m send() C $mail = $smtp->send($to, $headers, $message) ? $headers['From'] = 'from@example.com'; ? $headers['To'] = $to; ? $headers['Subject'] = 'Test message'; ? $headers['Cc'] = 'cc@example.com'; ? $headers['Reply-To'] = 'from@example.com'; 4. Ki?m tra l?i g?i mail C if (PEAR::isError($mail)) echo($mail->getMessage());
  • 24. L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9 : PHP n?ng cao ? 2007 Khoa CNTT - ?HKHTN 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>"); ?>
  • 25. L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9 : PHP n?ng cao ? 2007 Khoa CNTT - ?HKHTN N?i dung ? Upload File ? G?i Email ? Cookies ? Sessions
  • 26. L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9 : PHP n?ng cao ? 2007 Khoa CNTT - ?HKHTN Cookies ? L┐ 1 t?p tin ???c server l?u xu?ng m│y c?a client ? M?i l?n client g?i request 1 trang web, ??ng th?i s? g?i file cookie n┐y l┷n server ? C│c ?i?u khi?n x? l? th?ng tin (l?u, l?y) do server th?c hi?n ? Th??ng ???c s? d?ng ?? l?u th?ng tin c│ nh?n c?a client Webserver cookie setcookie $_COOKIE Client
  • 27. L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9 : PHP n?ng cao ? 2007 Khoa CNTT - ?HKHTN S? d?ng Cookies ? L?nh ghi cookie C setcookie(name, value, expire, path, domain); C setrawcookie(name, value, expire, path, domain); ? Name: t┷n cookie ? Value: gi│ tr? cookie ? Expire: ng┐y/kho?ng th?i gian h?t h?n h?t h?n ? Path : ???ng d?n m┐ cookie ???c g?i ?i ? Domain : x│c ??nh t┷n mi?n m┐ cookie ???c g?i ?i ? B?t bu?c ph?i xu?t hi?n tr??c th? <html> ? L?y gi│ tr? cookie C echo $_COOKIE[^cookieName"]; ? X┏a cookie C setcookie(^cookieName", "", time()-3600);
  • 28. L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9 : PHP n?ng cao ? 2007 Khoa CNTT - ?HKHTN V┴ d? Cookies ? L?u th?ng tin ??ng nh?p cookie setcookie $_COOKIE[fieldName] Client L?u username & password Webserver
  • 29. L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9 : PHP n?ng cao ? 2007 Khoa CNTT - ?HKHTN N?i dung ? Upload File ? G?i Email ? Cookies ? Sessions
  • 30. L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9 : PHP n?ng cao ? 2007 Khoa CNTT - ?HKHTN Session ? 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 Webserver Cookie: PHPSESSID session_start $_SESSION[sessname] Client
  • 31. L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9 : PHP n?ng cao ? 2007 Khoa CNTT - ?HKHTN S? d?ng Session ? Kh?i ??ng Session C session_start(); ? B?t bu?c ph?i xu?t hi?n tr??c th? <html> ? Ghi & ??c gi│ tr? Session C $_SESSION[^sessionName ̄] = value C $_SESSION[^sessionName ̄][ ] = array(´) C if (isset($_SESSION[^sessionName"]) echo $_SESSION[^sessionName"]; ? H?y Session C session_destroy();
  • 32. L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9 : PHP n?ng cao ? 2007 Khoa CNTT - ?HKHTN C?u h━nh Session ? C?u h━nh trong php.ini ? ini_set(`parameter¨, value); C G?i tr??c h┐m session_start(); 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 ´
  • 33. L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9 : PHP n?ng cao ? 2007 Khoa CNTT - ?HKHTN V┴ d? Session ? ??m s? l?n duy?t 1 trang web c?a 1 user <?php session_start( ); if (isset($_SESSION["count"])) $_SESSION["count"] = $_SESSION["count"] + 1; else $_SESSION["count"] = 1; print "You've looked at this page " . $_SESSION['count'] . ' times.'; ?>
  • 34. L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9 : PHP n?ng cao ? 2007 Khoa CNTT - ?HKHTN ?ng d?ng 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 C 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 ´
  • 35. L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9 : PHP n?ng cao ? 2007 Khoa CNTT - ?HKHTN 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? 1. T?o trang login.htm y┷u c?u ng??i d┫ng ??ng nh?p. 2. T?o trang validateuser.php l┐ trang x? l┴ th?ng tin ??ng nh?p t? trang login C K?t n?i v?i CSDL, ki?m tra xem th?ng tin ??ng nh?p c┏ ?┣ng hay kh?ng. C N?u kh?ng ?┣ng th━ cho redirect ??n trang login.htm. C C┛n n?u ??NG th━ d┫ng m?t bi?n Session ?? l?u tr?ng th│i login th┐nh c?ng l?i. ? V┴ d? $_SESSION["IsLogin^] = 1. ? L?u ? l┐ ph?i ??t gi│ tr? m?c ??nh cho bi?n n┐y l┐ 0 khi kh?i t?o m?t Session. 3. T?o trang logout.php l┐ trang x? l? khi ng??i d┫ng logout ? Reset tr?ng th│i login l┐ ch?a ??ng nh?p
  • 36. L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9 : PHP n?ng cao ? 2007 Khoa CNTT - ?HKHTN 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^); ?>
  • 37. L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9 : PHP n?ng cao ? 2007 Khoa CNTT - ?HKHTN So s│nh Clients Web servers Database/File Session (client1) Session (client2)cookies cookies Th?ng tin chia s? V? tr┴ l?u tr? Th?i gian t?n t?i S? client S? trang web s? d?ng Database/File Server D┐i N N Session Server Ng?n 1 N Cookies Client D┐i 1 N Internet or Intranet