ºÝºÝߣ

ºÝºÝߣShare a Scribd company logo
L?p tr¨¬nh v¨¤ Thi?t k? Web 1




Ba?i 9
PHP n?ng cao




                               Khoa CNTT ¨C ?H.KHTN
                                            ? 2007 Khoa C?ng ngh? th?ng tin
L?p tr¨¬nh v¨¤ Thi?t k? Web 1 ¨C B¨¤i 9 : PHP n?ng cao




N?i dung

  ? K? thu?t Upload File b?ng PHP
  ? K? thu?t g?i mail b?ng PHP
  ? S? d?ng Cookie trong PHP
  ? S? d?ng Session trong PHP




                                        ? 2007 Khoa CNTT - ?HKHTN
L?p tr¨¬nh v¨¤ Thi?t k? Web 1 ¨C B¨¤i 9 : PHP n?ng cao




N?i dung

  ? K? thu?t Upload File b?ng PHP
  ? K? thu?t g?i mail b?ng PHP
  ? S? d?ng Cookie trong PHP
  ? S? d?ng Session trong PHP




                                        ? 2007 Khoa CNTT - ?HKHTN
L?p tr¨¬nh v¨¤ Thi?t k? Web 1 ¨C B¨¤i 9 : PHP n?ng cao




K? thu?t Upload File b?ng PHP

   ? C? ch? Upload File
   ? Thi?t k? Form ?? Upload File
   ? Thi?t k? trang x? l? File ???c upload tr¨ºn server
   ? Ki?m tra l?i, ??nh d?ng, k¨ªch th??c File upload
   ? V?n ?? quy?n truy c?p file




                                         ? 2007 Khoa CNTT - ?HKHTN
L?p tr¨¬nh v¨¤ Thi?t k? Web 1 ¨C B¨¤i 9 : PHP n?ng cao



C? ch? Upload File
  Y¨ºu c?u trang upload.php
                                                                              Webserver




     file



                                                                                   Disk
                                                                                  driver


                                                                 Upload.php
                                    ?    $_FILES[¡°file¡±]["name"]
                                    ?    $_FILES[¡°file¡±]["type"]
                                    ?    $_FILES[¡°file¡±]["size"]
                                    ?    $_FILES[¡°file¡±]["tmp_name"]
                                    ?    $_FILES["file¡±]["error"]




                                           ? 2007 Khoa CNTT - ?HKHTN
L?p tr¨¬nh v¨¤ Thi?t k? Web 1 ¨C B¨¤i 9 : PHP n?ng cao




Thi?t k? Form ?? 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 =¡°ProductImg¡±><br>
        <input type=¡°submit¡± value=¡°Submit">
</form>




                                          ? 2007 Khoa CNTT - ?HKHTN
L?p tr¨¬nh v¨¤ Thi?t k? Web 1 ¨C B¨¤i 9 : PHP n?ng cao




Thi?t k? trang x? l? File ???c upload tr¨ºn server

    move_uploaded_file(tmp_name, saved_name)


                                    --- Upload.php ---
<?php
  move_uploaded_file($_FILES["ProductImg"]["tmp_name"],
                           "upload/" . $_FILES["ProductImg"]["name"]);




  echo   "Upload: " . $_FILES["ProductImg"]["name"] . ¡°<br>¡±;
  echo   "Type: " . $_FILES["ProductImg"]["type"] . ¡°<br>¡±;
  echo   "Size: " . ($_FILES["ProductImg"]["size¡°]/1024) . ¡°Kb<br>¡±;
  echo   ¡°Temp. Stored in: " . $_FILES["ProductImg"]["tmp_name"];
 ?>




                                             ? 2007 Khoa CNTT - ?HKHTN
L?p tr¨¬nh v¨¤ Thi?t k? Web 1 ¨C B¨¤i 9 : PHP n?ng cao




Ki?m tra l?i, ??nh d?ng, k¨ªch th??c File upload

   ? L?u ? :
    ¨C $_FILES[¡°¡­¡±][¡°type¡±]
      ? ¡°image/gif¡±
      ? ¡°image/jpeg¡±              ?           Firefox nh?n ??y l¨¤ file jpeg
      ? ¡°image/pjpeg¡±             ?           IE nh?n ??y l¨¤ file jpeg


    ¨C $_FILES[¡°¡­¡±][¡°size¡±]                      : K¨ªch th??c file t¨ªnh theo byte


    ¨C $_FILES[¡°¡­¡±][¡°error¡±] : M? l?i khi upload File
      ? = 0 : Kh?ng c¨® l?i
      ? > 0 : C¨® l?i
                                         ? 2007 Khoa CNTT - ?HKHTN
L?p tr¨¬nh v¨¤ Thi?t k? Web 1 ¨C B¨¤i 9 : PHP n?ng cao




V?n ?? v? quy?n truy c?p File

PHP Warning:
move_uploaded_file(upload/14.jpg
) [function.move-uploaded-file]:
failed to open stream:
Permission denied in.
                                                                        Account
                                                                      IUSR_XXX


L?u ? :
? Ch? c?p quy?n ghi cho th? m?c c?n thi?t
? Kh?ng c?p quy?n ghi cho th? m?c WebRoot




                                          ? 2007 Khoa CNTT - ?HKHTN
L?p tr¨¬nh v¨¤ Thi?t k? Web 1 ¨C B¨¤i 9 : PHP n?ng cao




N?i dung

  ? K? thu?t Upload File b?ng PHP
  ? K? thu?t g?i mail b?ng PHP
  ? S? d?ng Cookie trong PHP
  ? S? d?ng Session trong PHP




                                        ? 2007 Khoa CNTT - ?HKHTN
L?p tr¨¬nh v¨¤ Thi?t k? Web 1 ¨C B¨¤i 9 : PHP n?ng cao




K? thu?t g?i Email b?ng PHP

   ? S? d?ng th? vi?n open source: PHP Mailer
     http://phpmailer.sourceforge.net/
   ? Ch¨¦p 3 file: class.phpmailer.php, class.smtp.php,
     phpmailer.lang-en.php v¨¤o th? m?c web c?a site




                                         ? 2007 Khoa CNTT - ?HKHTN
L?p tr¨¬nh v¨¤ Thi?t k? Web 1 ¨C B¨¤i 9 : PHP n?ng cao




K? thu?t g?i Email b?ng PHP

   ? G?i email s? d?ng Mail Server c?a GMail




                                         ? 2007 Khoa CNTT - ?HKHTN
L?p tr¨¬nh v¨¤ Thi?t k? Web 1 ¨C B¨¤i 9 : PHP n?ng cao




N?i dung

  ? K? thu?t Upload File b?ng PHP
  ? K? thu?t g?i mail b?ng PHP
  ? S? d?ng Cookie trong PHP
  ? S? d?ng Session trong PHP




                                        ? 2007 Khoa CNTT - ?HKHTN
L?p tr¨¬nh v¨¤ Thi?t k? Web 1 ¨C B¨¤i 9 : PHP n?ng cao




Cookie ¨C Gi?i thi?u

      cookie

                                                                                  $_COOKIE


                                      setcookie
      Client                                                          Webserver




 ? 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 k¨¨m file cookie
   ?? l?u l?n tr??c l¨ºn server
 ? Vi?c x? l? th?ng tin (l?u, l?y) trong cookie do server th?c hi?n
 ? Th??ng ???c s? d?ng ?? l?u th?ng tin c¨¢ nh?n c?a client

                                          ? 2007 Khoa CNTT - ?HKHTN
L?p tr¨¬nh v¨¤ Thi?t k? Web 1 ¨C B¨¤i 9 : PHP n?ng cao




Cookie ¨C C¨² ph¨¢p s? d?ng
  ? L?nh ghi cookie
    setcookie(name, value, expire, path, domain);
    setrawcookie(name, value, expire, path, domain);
      ?   name      : T¨ºn cookie
      ?   value     : Gi¨¢ tr? cookie
      ?   expire : Th?i ?i?m m¨¤ cookie h?t hi?u l?c
      ?   path      : ???ng d?n tr¨ºn server m¨¤ cookie c¨® hi?u l?c
      ?   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
    echo $_COOKIE[¡°cookieName"];


  ? X¨®a cookie
    setcookie(¡°cookieName", "", time() -3600);


                                        ? 2007 Khoa CNTT - ?HKHTN
L?p tr¨¬nh v¨¤ Thi?t k? Web 1 ¨C B¨¤i 9 : PHP n?ng cao




Cookie ¨C V¨ª d?


                                                                       $_COOKIE[fieldName]

  cookie
                           L?u username & password


                                    setcookie
   Client

                                                                         Webserver




                                           ? 2007 Khoa CNTT - ?HKHTN
L?p tr¨¬nh v¨¤ Thi?t k? Web 1 ¨C B¨¤i 9 : PHP n?ng cao




N?i dung

  ? K? thu?t Upload File b?ng PHP
  ? K? thu?t g?i mail b?ng PHP
  ? S? d?ng Cookie trong PHP
  ? S? d?ng Session trong PHP




                                        ? 2007 Khoa CNTT - ?HKHTN
L?p tr¨¬nh v¨¤ Thi?t k? Web 1 ¨C B¨¤i 9 : PHP n?ng cao




Session
                                                                     $_SESSION[sessionVar]
   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 session 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

                                         ? 2007 Khoa CNTT - ?HKHTN
L?p tr¨¬nh v¨¤ Thi?t k? Web 1 ¨C B¨¤i 9 : PHP n?ng cao




Session ¨C C¨² ph¨¢p s? d?ng
   ? Kh?i ??ng Session
     session_start();
     ¨C B?t bu?c ph?i xu?t hi?n tr??c th? <html>


   ? Ghi & ??c gi¨¢ tr? Session
     $_SESSION[¡°sessionVar¡±] = $value ;

     $_SESSION[¡°sessionVar¡±] = array();
     $_SESSION[¡°sessionVar¡±][] = $value;

     if (isset($_SESSION[¡°sessionVar"])
          echo $_SESSION[¡°sessionVar"];

   ? H?y Bi?n trong Session
     unset($_SESSION[¡°sessionVar¡±]);


   ? H?y c? Session
     session_destroy();

                                          ? 2007 Khoa CNTT - ?HKHTN
L?p tr¨¬nh v¨¤ Thi?t k? Web 1 ¨C B¨¤i 9 : PHP n?ng cao




Session ¨C V¨ª d? : ??m s? l?n duy?t trang web
<?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.¡±;
?>




                                            ? 2007 Khoa CNTT - ?HKHTN
L?p tr¨¬nh v¨¤ Thi?t k? Web 1 ¨C B¨¤i 9 : PHP n?ng cao




Session - ?ng d?ng cho Login

   ? 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¡±] = true/false                    : 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



                                         ? 2007 Khoa CNTT - ?HKHTN
L?p tr¨¬nh v¨¤ Thi?t k? Web 1 ¨C B¨¤i 9 : PHP n?ng cao




Session - ?ng d?ng cho Login ¨C G?m 4 b??c
   1. T?o trang login.htm y¨ºu c?u ng??i d¨´ng ??ng nh?p.


   2. T?o trang validateuser.php x? l¨ª th?ng tin ??ng nh?p t? trang
      login.htm
     ¨C K?t n?i v?i CSDL, ki?m tra th?ng tin ??ng nh?p c¨® h?p l? hay kh?ng ?
       ?   N?u kh?ng h?p l? th¨¬ cho redirect v? trang login.htm.
       ?   N?u h?p l? th¨¬ d¨´ng m?t bi?n trong Session ?? l?u tr?ng th¨¢i login th¨¤nh c?ng
           ¨C V¨ª d?: $_SESSION[¡°IsLogin¡±] = true.
           ¨C L?u ?: Ph?i ??t gi¨¢ tr? m?c ??nh cho bi?n Session n¨¤y l¨¤ false 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 ($_SESSION[¡°IsLogin¡±]        = false).




                                         ? 2007 Khoa CNTT - ?HKHTN
L?p tr¨¬nh v¨¤ Thi?t k? Web 1 ¨C B¨¤i 9 : PHP n?ng cao




Session - ?ng d?ng cho Login

   4.    Trong t?t c? c¨¢c trang mu?n b?o m?t, th¨ºm ?o?n m? sau ?? ki?m tra
         ng??i d¨´ng ?? ??ng nh?p hay ch?a, n?u ch?a th¨¬ redirect l?i trang
         login.htm


<?php
session_start();

if (!isset($_SESSION[¡°IsLogin¡±]) || $_SESSION[¡°IsLogin¡±] == false)
        header(¡°Location: login.htm¡±);
?>




                                            ? 2007 Khoa CNTT - ?HKHTN
L?p tr¨¬nh v¨¤ Thi?t k? Web 1 ¨C B¨¤i 9 : PHP n?ng cao




Session - ?ng d?ng cho Gi? h¨¤ng (shopping cart)




                                                              X¨®a bi?n trong Session
{¡®Book1¡¯, ¡®Book2¡¯, ¡®Book3¡¯}
Session : Array : Mathang [ ]                                                          Ghi xu?ng CSDL

 Session : Array : Soluong[ ]

         {13, 2, 7}




                                               ? 2007 Khoa CNTT - ?HKHTN
L?p tr¨¬nh v¨¤ Thi?t k? Web 1 ¨C B¨¤i 9 : PHP n?ng cao


So s¨¢nh : Session ¨C Cookie ¨C Database
                           Cookie1
                            Cookie1

                                                                                Session 1

                        Client 1
 Cookie2
  Cookie2                                                                       Web Server
                                                                    Session 2                 Session 3
                                            Internet
      Client 2                             or Intranet
                                                                                                  Database Server


   Cookie3
    Cookie3            Client 3




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               Nhi?u                 Nhi?u
Session                               Server       Ng?n                     1                Nhi?u
Cookies                               Client         D¨¤i                    1                Nhi?u

                                                ? 2007 Khoa CNTT - ?HKHTN

More Related Content

09 web course_-_php_nang_cao

  • 1. L?p tr¨¬nh v¨¤ Thi?t k? Web 1 Ba?i 9 PHP n?ng cao Khoa CNTT ¨C ?H.KHTN ? 2007 Khoa C?ng ngh? th?ng tin
  • 2. L?p tr¨¬nh v¨¤ Thi?t k? Web 1 ¨C B¨¤i 9 : PHP n?ng cao N?i dung ? K? thu?t Upload File b?ng PHP ? K? thu?t g?i mail b?ng PHP ? S? d?ng Cookie trong PHP ? S? d?ng Session trong PHP ? 2007 Khoa CNTT - ?HKHTN
  • 3. L?p tr¨¬nh v¨¤ Thi?t k? Web 1 ¨C B¨¤i 9 : PHP n?ng cao N?i dung ? K? thu?t Upload File b?ng PHP ? K? thu?t g?i mail b?ng PHP ? S? d?ng Cookie trong PHP ? S? d?ng Session trong PHP ? 2007 Khoa CNTT - ?HKHTN
  • 4. L?p tr¨¬nh v¨¤ Thi?t k? Web 1 ¨C B¨¤i 9 : PHP n?ng cao K? thu?t Upload File b?ng PHP ? C? ch? Upload File ? Thi?t k? Form ?? Upload File ? Thi?t k? trang x? l? File ???c upload tr¨ºn server ? Ki?m tra l?i, ??nh d?ng, k¨ªch th??c File upload ? V?n ?? quy?n truy c?p file ? 2007 Khoa CNTT - ?HKHTN
  • 5. L?p tr¨¬nh v¨¤ Thi?t k? Web 1 ¨C B¨¤i 9 : PHP n?ng cao C? ch? Upload File Y¨ºu c?u trang upload.php Webserver file Disk driver Upload.php ? $_FILES[¡°file¡±]["name"] ? $_FILES[¡°file¡±]["type"] ? $_FILES[¡°file¡±]["size"] ? $_FILES[¡°file¡±]["tmp_name"] ? $_FILES["file¡±]["error"] ? 2007 Khoa CNTT - ?HKHTN
  • 6. L?p tr¨¬nh v¨¤ Thi?t k? Web 1 ¨C B¨¤i 9 : PHP n?ng cao Thi?t k? Form ?? 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 =¡°ProductImg¡±><br> <input type=¡°submit¡± value=¡°Submit"> </form> ? 2007 Khoa CNTT - ?HKHTN
  • 7. L?p tr¨¬nh v¨¤ Thi?t k? Web 1 ¨C B¨¤i 9 : PHP n?ng cao Thi?t k? trang x? l? File ???c upload tr¨ºn server move_uploaded_file(tmp_name, saved_name) --- Upload.php --- <?php move_uploaded_file($_FILES["ProductImg"]["tmp_name"], "upload/" . $_FILES["ProductImg"]["name"]); echo "Upload: " . $_FILES["ProductImg"]["name"] . ¡°<br>¡±; echo "Type: " . $_FILES["ProductImg"]["type"] . ¡°<br>¡±; echo "Size: " . ($_FILES["ProductImg"]["size¡°]/1024) . ¡°Kb<br>¡±; echo ¡°Temp. Stored in: " . $_FILES["ProductImg"]["tmp_name"]; ?> ? 2007 Khoa CNTT - ?HKHTN
  • 8. L?p tr¨¬nh v¨¤ Thi?t k? Web 1 ¨C B¨¤i 9 : PHP n?ng cao Ki?m tra l?i, ??nh d?ng, k¨ªch th??c File upload ? L?u ? : ¨C $_FILES[¡°¡­¡±][¡°type¡±] ? ¡°image/gif¡± ? ¡°image/jpeg¡± ? Firefox nh?n ??y l¨¤ file jpeg ? ¡°image/pjpeg¡± ? IE nh?n ??y l¨¤ file jpeg ¨C $_FILES[¡°¡­¡±][¡°size¡±] : K¨ªch th??c file t¨ªnh theo byte ¨C $_FILES[¡°¡­¡±][¡°error¡±] : M? l?i khi upload File ? = 0 : Kh?ng c¨® l?i ? > 0 : C¨® l?i ? 2007 Khoa CNTT - ?HKHTN
  • 9. L?p tr¨¬nh v¨¤ Thi?t k? Web 1 ¨C B¨¤i 9 : PHP n?ng cao V?n ?? v? quy?n truy c?p File PHP Warning: move_uploaded_file(upload/14.jpg ) [function.move-uploaded-file]: failed to open stream: Permission denied in. Account IUSR_XXX L?u ? : ? Ch? c?p quy?n ghi cho th? m?c c?n thi?t ? Kh?ng c?p quy?n ghi cho th? m?c WebRoot ? 2007 Khoa CNTT - ?HKHTN
  • 10. L?p tr¨¬nh v¨¤ Thi?t k? Web 1 ¨C B¨¤i 9 : PHP n?ng cao N?i dung ? K? thu?t Upload File b?ng PHP ? K? thu?t g?i mail b?ng PHP ? S? d?ng Cookie trong PHP ? S? d?ng Session trong PHP ? 2007 Khoa CNTT - ?HKHTN
  • 11. L?p tr¨¬nh v¨¤ Thi?t k? Web 1 ¨C B¨¤i 9 : PHP n?ng cao K? thu?t g?i Email b?ng PHP ? S? d?ng th? vi?n open source: PHP Mailer http://phpmailer.sourceforge.net/ ? Ch¨¦p 3 file: class.phpmailer.php, class.smtp.php, phpmailer.lang-en.php v¨¤o th? m?c web c?a site ? 2007 Khoa CNTT - ?HKHTN
  • 12. L?p tr¨¬nh v¨¤ Thi?t k? Web 1 ¨C B¨¤i 9 : PHP n?ng cao K? thu?t g?i Email b?ng PHP ? G?i email s? d?ng Mail Server c?a GMail ? 2007 Khoa CNTT - ?HKHTN
  • 13. L?p tr¨¬nh v¨¤ Thi?t k? Web 1 ¨C B¨¤i 9 : PHP n?ng cao N?i dung ? K? thu?t Upload File b?ng PHP ? K? thu?t g?i mail b?ng PHP ? S? d?ng Cookie trong PHP ? S? d?ng Session trong PHP ? 2007 Khoa CNTT - ?HKHTN
  • 14. L?p tr¨¬nh v¨¤ Thi?t k? Web 1 ¨C B¨¤i 9 : PHP n?ng cao Cookie ¨C Gi?i thi?u cookie $_COOKIE setcookie Client Webserver ? 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 k¨¨m file cookie ?? l?u l?n tr??c l¨ºn server ? Vi?c x? l? th?ng tin (l?u, l?y) trong cookie do server th?c hi?n ? Th??ng ???c s? d?ng ?? l?u th?ng tin c¨¢ nh?n c?a client ? 2007 Khoa CNTT - ?HKHTN
  • 15. L?p tr¨¬nh v¨¤ Thi?t k? Web 1 ¨C B¨¤i 9 : PHP n?ng cao Cookie ¨C C¨² ph¨¢p s? d?ng ? L?nh ghi cookie setcookie(name, value, expire, path, domain); setrawcookie(name, value, expire, path, domain); ? name : T¨ºn cookie ? value : Gi¨¢ tr? cookie ? expire : Th?i ?i?m m¨¤ cookie h?t hi?u l?c ? path : ???ng d?n tr¨ºn server m¨¤ cookie c¨® hi?u l?c ? 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 echo $_COOKIE[¡°cookieName"]; ? X¨®a cookie setcookie(¡°cookieName", "", time() -3600); ? 2007 Khoa CNTT - ?HKHTN
  • 16. L?p tr¨¬nh v¨¤ Thi?t k? Web 1 ¨C B¨¤i 9 : PHP n?ng cao Cookie ¨C V¨ª d? $_COOKIE[fieldName] cookie L?u username & password setcookie Client Webserver ? 2007 Khoa CNTT - ?HKHTN
  • 17. L?p tr¨¬nh v¨¤ Thi?t k? Web 1 ¨C B¨¤i 9 : PHP n?ng cao N?i dung ? K? thu?t Upload File b?ng PHP ? K? thu?t g?i mail b?ng PHP ? S? d?ng Cookie trong PHP ? S? d?ng Session trong PHP ? 2007 Khoa CNTT - ?HKHTN
  • 18. L?p tr¨¬nh v¨¤ Thi?t k? Web 1 ¨C B¨¤i 9 : PHP n?ng cao Session $_SESSION[sessionVar] 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 session 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 ? 2007 Khoa CNTT - ?HKHTN
  • 19. L?p tr¨¬nh v¨¤ Thi?t k? Web 1 ¨C B¨¤i 9 : PHP n?ng cao Session ¨C C¨² ph¨¢p s? d?ng ? Kh?i ??ng Session session_start(); ¨C B?t bu?c ph?i xu?t hi?n tr??c th? <html> ? Ghi & ??c gi¨¢ tr? Session $_SESSION[¡°sessionVar¡±] = $value ; $_SESSION[¡°sessionVar¡±] = array(); $_SESSION[¡°sessionVar¡±][] = $value; if (isset($_SESSION[¡°sessionVar"]) echo $_SESSION[¡°sessionVar"]; ? H?y Bi?n trong Session unset($_SESSION[¡°sessionVar¡±]); ? H?y c? Session session_destroy(); ? 2007 Khoa CNTT - ?HKHTN
  • 20. L?p tr¨¬nh v¨¤ Thi?t k? Web 1 ¨C B¨¤i 9 : PHP n?ng cao Session ¨C V¨ª d? : ??m s? l?n duy?t trang web <?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.¡±; ?> ? 2007 Khoa CNTT - ?HKHTN
  • 21. L?p tr¨¬nh v¨¤ Thi?t k? Web 1 ¨C B¨¤i 9 : PHP n?ng cao Session - ?ng d?ng cho Login ? 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¡±] = true/false : 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 ? 2007 Khoa CNTT - ?HKHTN
  • 22. L?p tr¨¬nh v¨¤ Thi?t k? Web 1 ¨C B¨¤i 9 : PHP n?ng cao Session - ?ng d?ng cho Login ¨C G?m 4 b??c 1. T?o trang login.htm y¨ºu c?u ng??i d¨´ng ??ng nh?p. 2. T?o trang validateuser.php x? l¨ª th?ng tin ??ng nh?p t? trang login.htm ¨C K?t n?i v?i CSDL, ki?m tra th?ng tin ??ng nh?p c¨® h?p l? hay kh?ng ? ? N?u kh?ng h?p l? th¨¬ cho redirect v? trang login.htm. ? N?u h?p l? th¨¬ d¨´ng m?t bi?n trong Session ?? l?u tr?ng th¨¢i login th¨¤nh c?ng ¨C V¨ª d?: $_SESSION[¡°IsLogin¡±] = true. ¨C L?u ?: Ph?i ??t gi¨¢ tr? m?c ??nh cho bi?n Session n¨¤y l¨¤ false 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 ($_SESSION[¡°IsLogin¡±] = false). ? 2007 Khoa CNTT - ?HKHTN
  • 23. L?p tr¨¬nh v¨¤ Thi?t k? Web 1 ¨C B¨¤i 9 : PHP n?ng cao Session - ?ng d?ng cho Login 4. Trong t?t c? c¨¢c trang mu?n b?o m?t, th¨ºm ?o?n m? sau ?? ki?m tra ng??i d¨´ng ?? ??ng nh?p hay ch?a, n?u ch?a th¨¬ redirect l?i trang login.htm <?php session_start(); if (!isset($_SESSION[¡°IsLogin¡±]) || $_SESSION[¡°IsLogin¡±] == false) header(¡°Location: login.htm¡±); ?> ? 2007 Khoa CNTT - ?HKHTN
  • 24. L?p tr¨¬nh v¨¤ Thi?t k? Web 1 ¨C B¨¤i 9 : PHP n?ng cao Session - ?ng d?ng cho Gi? h¨¤ng (shopping cart) X¨®a bi?n trong Session {¡®Book1¡¯, ¡®Book2¡¯, ¡®Book3¡¯} Session : Array : Mathang [ ] Ghi xu?ng CSDL Session : Array : Soluong[ ] {13, 2, 7} ? 2007 Khoa CNTT - ?HKHTN
  • 25. L?p tr¨¬nh v¨¤ Thi?t k? Web 1 ¨C B¨¤i 9 : PHP n?ng cao So s¨¢nh : Session ¨C Cookie ¨C Database Cookie1 Cookie1 Session 1 Client 1 Cookie2 Cookie2 Web Server Session 2 Session 3 Internet Client 2 or Intranet Database Server Cookie3 Cookie3 Client 3 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 Nhi?u Nhi?u Session Server Ng?n 1 Nhi?u Cookies Client D¨¤i 1 Nhi?u ? 2007 Khoa CNTT - ?HKHTN