{"id":18005,"date":"2021-06-24T10:40:07","date_gmt":"2021-06-24T03:40:07","guid":{"rendered":"https:\/\/amonous.com\/?p=18005"},"modified":"2021-06-24T10:40:07","modified_gmt":"2021-06-24T03:40:07","slug":"session-va-cookie-trong-lap-trinh-web","status":"publish","type":"post","link":"https:\/\/amonous.com\/session-va-cookie-trong-lap-trinh-web\/","title":{"rendered":"Session v\u00e0 cookie trong l\u1eadp tr\u00ecnh Web"},"content":{"rendered":"
Session, Cookie l\u00e0 2 kh\u00e1i ni\u1ec7m \u0111\u01b0\u1ee3c r\u1ea5t quan tr\u1ecdng \u0111\u1ed1i v\u1edbi c\u00e1c l\u1eadp tr\u00ecnh web. Khi m\u1edbi b\u1eaft \u0111\u1ea7u t\u1eadp l\u00e0m web, nhi\u1ec1u b\u1ea1n ch\u1eafc h\u1eb3n r\u1ea5t hay nh\u1ea7m l\u1eabn gi\u1eefa c\u00e1c kh\u00e1i ni\u1ec7m n\u00e0y. H\u00e3y c\u00f9ng t\u00ecm hi\u1ec3u v\u1ec1 kh\u00e1i ni\u1ec7m c\u00e1ch d\u00f9ng c\u1ee7a Session v\u00e0 Cookie nh\u00e9.<\/strong><\/em><\/p>\n \u0110\u00e2y l\u00e0 m\u1ed9t phi\u00ean l\u00e0m vi\u1ec7c \u0111\u01b0\u1ee3c d\u00f9ng trong l\u1eadp tr\u00ecnh web v\u00e0 c\u00f3 th\u1ec3 k\u1ebft n\u1ed1i v\u1edbi database. N\u00f3i m\u1ed9t c\u00e1ch d\u1ec5 hi\u1ec3u, session ch\u00ednh l\u00e0 ch\u01b0\u01a1ng tr\u00ecnh hay c\u00e1ch th\u1ee9c \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 l\u01b0u tr\u1eef th\u00f4ng tin c\u1ee7a ng\u01b0\u1eddi d\u00f9ng khi h\u1ecd s\u1eed d\u1ee5ng trang web c\u1ee7a m\u00ecnh.<\/p>\n Session \u0111\u01b0\u1ee3c l\u01b0u tr\u1eef trong m\u1ed9t t\u1eadp tin b\u1ea5t k\u1ef3 n\u00e0o \u0111\u00f3 c\u1ee7a server. Vi\u1ec7c l\u01b0u tr\u1eef d\u1eef li\u1ec7u trong session kh\u00f4ng ph\u1ee5 thu\u1ed9c v\u00e0o quy\u1ebft \u0111\u1ecbnh c\u1ee7a ng\u01b0\u1eddi d\u00f9ng. Tuy nhi\u00ean, c\u00e1c session th\u00f4ng th\u01b0\u1eddng s\u1ebd ch\u1ec9 l\u01b0u tr\u1eef nh\u1eefng th\u00f4ng tin ng\u1eafn h\u1ea1n m\u00e0 th\u00f4i. V\u00ed d\u1ee5 nh\u01b0 khi b\u1ea1n \u0111\u0103ng nh\u1eadp v\u00e0o m\u1ed9t trang website theo t\u00e0i kho\u1ea3n email v\u00e0 m\u1eadt kh\u1ea9u c\u1ee7a m\u00ecnh \u0111\u01b0\u1ee3c x\u00e1c nh\u1eadn l\u00e0 ch\u00ednh x\u00e1c, khi \u0111\u00f3 m\u1ed9t session s\u1ebd \u0111\u01b0\u1ee3c t\u1ea1o trong m\u00e1y ch\u1ee7 \u0111\u1ec3 l\u01b0u tr\u1eef l\u1ea1i c\u00e1c h\u00e0nh \u0111\u1ed9ng c\u1ee7a b\u1ea1n t\u1edbi khi phi\u00ean \u0111\u01b0\u1ee3c k\u1ebft th\u00fac. \u0110i\u1ec1u n\u00e0y c\u0169ng t\u01b0\u01a1ng t\u1ef1 nh\u01b0 \u0111ang th\u00eam h\u00e0ng h\u00f3a v\u00e0o gi\u1ecf h\u00e0ng nh\u01b0ng ch\u01b0a thanh to\u00e1n.<\/p>\n L\u01b0u tr\u1eef c\u00e1c Session d\u01b0\u1eddng nh\u01b0 r\u1ea5t ph\u1ed5 bi\u1ebfn tr\u00ean c\u00e1c website. Th\u1ef1c ra l\u00fd do cho vi\u1ec7c n\u00e0y c\u0169ng kh\u00e1 d\u1ec5 hi\u1ec3u b\u1edfi c\u00e1c l\u1ee3i \u00edch m\u00e0 n\u00f3 \u0111em l\u1ea1i. Trong c\u00e1c c\u00f4ng vi\u1ec7c giao ti\u1ebfp gi\u1eefa m\u00e1y ch\u1ee7 l\u00e0 website server v\u1edbi tr\u00ecnh duy\u1ec7t th\u00ec s\u1ebd \u0111\u01b0\u1ee3c th\u1ef1c hi\u1ec7n qua h\u00e0ng ho\u1ea1t router tr\u00ean m\u1ea1ng. Nh\u01b0ng v\u1ea5n \u0111\u1ec1 l\u00e0 l\u00e0m sao \u0111\u1ec3 ph\u00e2n bi\u1ec7t \u0111\u01b0\u1ee3c c\u00e1c tr\u00ecnh duy\u1ec7t kh\u00e1c nhau c\u1ee7a ng\u01b0\u1eddi d\u00f9ng?<\/p>\n V\u00ec v\u1eady khi s\u1eed d\u1ee5ng session s\u1ebd gi\u00fap b\u1ea1n:<\/p>\n <\/p>\n M\u1ed9t session \u0111\u01b0\u1ee3c b\u1eaft \u0111\u1ea7u khi client g\u1eedi y\u00eau c\u1ea7u \u0111\u1ebfn m\u00e1y ch\u1ee7 (server). V\u00e0, n\u00f3 t\u1ed3n t\u1ea1i xuy\u00ean su\u1ed1t t\u1eeb trang n\u00e0y \u0111\u1ebfn trang kh\u00e1c trong \u1ee9ng d\u1ee5ng web. M\u1ed9t session ch\u1ec9 d\u1eebng l\u1ea1i khi h\u1ebft th\u1eddi gian (timeout) ho\u1eb7c khi ng\u01b0\u1eddi d\u00f9ng \u0111\u00f3ng \u1ee9ng d\u1ee5ng. Gi\u00e1 tr\u1ecb c\u1ee7a c\u00e1c session s\u1ebd \u0111\u01b0\u1ee3c l\u01b0u tr\u1eef trong m\u1ed9t file tr\u00ean server.<\/p>\n Cookie<\/strong> c\u0169ng \u0111\u01b0\u1ee3c d\u00f9ng \u0111\u1ec3 l\u01b0u nh\u1eefng th\u00f4ng tin t\u1ea1m th\u1eddi. Nh\u01b0ng t\u1eadp tin cookie s\u1ebd \u0111\u01b0\u1ee3c truy\u1ec1n t\u1eeb server t\u1edbi browser v\u00e0 \u0111\u01b0\u1ee3c l\u01b0u tr\u1eef tr\u00ean m\u00e1y t\u00ednh c\u1ee7a b\u1ea1n khi b\u1ea1n truy c\u1eadp v\u00e0o \u1ee9ng d\u1ee5ng.<\/p>\n Cookie<\/strong> th\u01b0\u1eddng \u0111\u01b0\u1ee3c t\u1ea1o ra khi ng\u01b0\u1eddi d\u00f9ng truy c\u1eadp m\u1ed9t website. Cookie <\/strong>s\u1ebd ghi nh\u1edb nh\u1eefng th\u00f4ng tin nh\u01b0:<\/p>\n C\u00e1c th\u00f4ng tin n\u00e0y \u0111\u01b0\u1ee3c l\u01b0u trong m\u00e1y t\u00ednh \u0111\u1ec3 nh\u1eadn bi\u1ebft ng\u01b0\u1eddi d\u00f9ng khi truy c\u1eadp v\u00e0o m\u1ed9t trang web.<\/p>\n Khi ng\u01b0\u1eddi d\u00f9ng truy c\u1eadp \u0111\u1ebfn m\u1ed9t trang web c\u00f3 s\u1eed d\u1ee5ng cookie, web server c\u1ee7a trang \u0111\u00f3 s\u1ebd t\u1ef1 \u0111\u1ed9ng g\u1eedi cookie \u0111\u1ebfn m\u00e1y t\u00ednh c\u1ee7a ng\u01b0\u1eddi d\u00f9ng. Khi truy c\u1eadp \u0111\u1ebfn c\u00e1c trang web s\u1eed d\u1ee5ng \u0111\u01b0\u1ee3c cookie \u0111\u00e3 l\u01b0u, nh\u1eefng cookie n\u00e0y t\u1ef1 \u0111\u1ed9ng g\u1eedi th\u00f4ng tin c\u1ee7a ng\u01b0\u1eddi d\u00f9ng v\u1ec1 cho ch\u1ee7 c\u1ee7a n\u00f3 (ng\u01b0\u1eddi t\u1ea1o ra cookie). Tuy nhi\u00ean, nh\u1eefng th\u00f4ng tin do cookie ghi nh\u1eadn kh\u00f4ng \u0111\u01b0\u1ee3c ti\u1ebft l\u1ed9 r\u1ed9ng r\u00e3i, ch\u1ec9 c\u00f3 website ch\u1ee9a cookie m\u1edbi c\u00f3 th\u1ec3 xem \u0111\u01b0\u1ee3c nh\u1eefng th\u00f4ng tin n\u00e0y.<\/p>\n M\u1ed7i cookie th\u01b0\u1eddng c\u00f3 kho\u1ea3ng th\u1eddi gian timeout nh\u1ea5t \u0111\u1ecbnh do l\u1eadp tr\u00ecnh vi\u00ean x\u00e1c \u0111\u1ecbnh tr\u01b0\u1edbc. Nh\u1eefng th\u00f4ng tin \u0111\u01b0\u1ee3c l\u01b0u v\u00e0o cookie v\u00ed d\u1ee5 nh\u01b0 th\u00f4ng tin \u0111\u0103ng nh\u1eadp, thao t\u00e1c ng\u01b0\u1eddi d\u00f9ng, t\u1ea7n xu\u1ea5t gh\u00e9 th\u0103m website, th\u1eddi gian truy c\u1eadp\u2026 T\u1ea5t c\u1ea3 ch\u00fang \u0111\u1ec1u l\u00e0 nh\u1eefng th\u00f4ng tin mang t\u00ednh t\u1ea1m th\u1eddi v\u00e0 \u0111\u01b0\u1ee3c l\u01b0u trong 1 kho\u1ea3ng th\u1eddi gian.<\/p>\n > H\u1ecdc c\u00f4ng ngh\u1ec7 th\u00f4ng tin c\u1ea7n chu\u1ea9n b\u1ecb g\u00ec?<\/a><\/p>\n > 3 l\u00ed do v\u00f4 c\u00f9ng thuy\u1ebft ph\u1ee5c \u0111\u1ec3 ch\u1ecdn ng\u00e0nh C\u00f4ng ngh\u1ec7 Th\u00f4ng tin<\/a><\/p>\n > Th\u1ee7 khoa \u0111\u1ea7u v\u00e0o c\u1ee7a K18 m\u00e1ch n\u01b0\u1edbc b\u00ed quy\u1ebft ch\u1ecdn ng\u00e0nh, ch\u1ecdn tr\u01b0\u1eddng<\/a><\/p>\nSession<\/strong><\/h3>\n
Session l\u00e0 g\u00ec?<\/strong><\/h5>\n
T\u1ea1i sao l\u1ea1i c\u1ea7n \u0111\u1ebfn session?<\/strong><\/h5>\n
\n
Session \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng nh\u01b0 th\u1ebf n\u00e0o?<\/strong><\/h5>\n
Cookie<\/h3>\n
Kh\u00e1i ni\u1ec7m<\/strong>:<\/h5>\n
C\u00e1ch s\u1eed d\u1ee5ng Cookie:<\/h5>\n
\n
So s\u00e1nh Session v\u00e0 cookie<\/strong><\/h3>\n
\n
Xem th\u00eam<\/h5>\n
\n