Transcript Slide 1
ทส215 การเขียนโปรแกรมบนเว็บ 1 Cookie & Session ั อาจารย์อรรถวิท ชงคมานนท์ สาขาวิชาเทคโนโลยีสารสนเทศ คณะวิทยาศาสตร์ www.itsci.mju.ac.th 1 [email protected] Cookies 12 [email protected] 13 [email protected] Cookies ้ ื่ สารระหว่าง client และ คือ collection ทีใ่ ชในการเก็ บข ้อมูลชนิดหนึง่ ในการสอ ้ server ทีม ่ ก ี ารเก็บรักษาข ้อมูลบางอย่างไว ้บนเครือ ่ งของผู ้ใช ้ เพือ ่ จะนาไปใชใหม่ ภายหลัง Cookies ถูกจัดการผ่าน object Response โดยตรง และ การกระทาผ่าน object HttpCookie รูปแบบการบันทึก Cookies ผ่าน Response Response.Cookies[“CookieName”][[“SubKey”].Property] = Value 14 [email protected] Cookies ผ่าน Response คุณสมบัตท ิ ส ี่ าคัญได ้แก่ Expires ใชอ่้ านหรือกาหนดวันหมดอายุของ Cookies Domain กาหนดโดเมนทีเ่ ป็ นผู ้บันทึก Cookies นี้ HasKeys ตรวจสอบว่ามีคย ี ย ์ อ ่ ย ๆ อีกหรือไม่ 15 [email protected] Cookies ผ่าน Response ตัวอย่างการสร ้าง Cookies Response.Cookies[“Cookies1”][“name”] = “aaaaa”; Response.Cookies[“Cookies1”][“major”] = “itmju”; Response.Cookies[“Cookies1”].Expires=DateTime.Now.AddDays(30); คือการกาหนดวันหมดอายุ 30 วัน ตัวอย่างการอ่านข ้อมูลจาก Cookies Str = Request.Cookies[“Cookies1”][“name”]; 16 [email protected] Cookies ผ่าน HttpCookie การสร ้าง object Cookies HttpCookie myCookie = new HttpCookie("Cookies1"); myCookie.Values["name"] = "aaaaa"; myCookie.Values["major"] = "itmju"; myCookie.Domain = "iTech.mju.ac.th"; myCookie.Expires = DateTime.Now.AddDays(30); Response.Cookies.Add(myCookie); 17 [email protected] Cookies ผ่าน HttpCookie ่ การอ่าน object Cookies เชน HttpCookie myCookie = new HttpCookie("Cookies1"); myCookie = Request.Cookies["Cookies1"]; if (myCookie != null) { Label1.Text = myCookie.Values["name"]; Label2.Text = myCookie.Values["major"]; } 18 [email protected] Cookies Example – WebForm3.aspx <%@ Page Language="C#" …"%> <HTML><HEAD><title>WebForm3</title></HEAD> <body MS_POSITIONING="GridLayout"> <form id="Form1" method="post" runat="server"> <asp:TextBox id="TextBox1" runat="server" ></asp:TextBox> <asp:Button id="Button2" runat="server" Text="Next"></asp:Button> <asp:Label id="Label3" runat="server" >Add Cookies</asp:Label> <asp:Label id="Label2" runat="server" >Major</asp:Label> <asp:TextBox id="TextBox2" runat="server" ></asp:TextBox> <asp:Label id="Label1" runat="server" >Name</asp:Label> <asp:Button id="Button1" runat="server“ Text="Add"></asp:Button> </form> </body> </HTML> 19 [email protected] Cookies Example – WebForm3.aspx 20 [email protected] Cookies Example – WebForm3.aspx.cs public class Web_08_WebForm3 : System.Web.UI.Page { private void Button2_Click(object sender, System.EventArgs e) { Response.Redirect("WebForm4.aspx"); } private void Button1_Click(object sender, System.EventArgs e) { Response.Cookies["Cookies1"]["name"] = TextBox1.Text; Response.Cookies["Cookies1"]["major"] = TextBox2.Text; Response.Cookies["Cookies1"].Expires = DateTime.Now.AddDays(30); } } 21 [email protected] Cookies Example – WebForm4.aspx <%@ Page Language="c#" … "%> <HTML> <HEAD> <title>WebForm4</title> </HEAD> <body MS_POSITIONING="GridLayout"> <form id="Form1" method="post" runat="server"> <asp:Label id="Label1" runat="server" >Label</asp:Label> <asp:Label id="Label2" runat="server">Label</asp:Label> </form> </body> </HTML> 22 [email protected] Cookies Example – WebForm4.aspx.cs Public Class Web_08_WebForm4 : System.Web.UI.Page { } private void Page_Load(object sender, System.EventArgs e) { string Str1 = Request.Cookies["Cookies1"]["name"]; string Str2 = Request.Cookies["Cookies1"]["major"]; Label1.Text = "Hello : " + Str1; Label2.Text = "Major : " + Str2; } 23 [email protected] Cookies Example – WebForm4.aspx 24 [email protected] C:\Documents and Settings\Administrator\Cookies 25 [email protected] Advantages of Cookies Following are main advantages of using cookies in web application: It's very simple to use and implement. Browser's taking care send data. For multiple sites cookies, Browser automatically arranges them. 26 [email protected] Disadvantages of Cookies Its store data in a simple text format. so it's not secure at all. There is a size limit of cookies data ( 4096 bytes / 4KB). Number if cookies also limited. Most Browser provides limits of storing cookies is 20. If new cookies came, it will discard the old one. Some of browser support up to 300. We need to configure browser. It will not work on a high security configuration of browser. [I have explained about this in details.] 27 [email protected] Application ้ คือ ตัวแปรในระดับทีแ ่ ต่ละเพจของเว็บต ้องการใชในการท างาน เดียวกัน มี 2 ลักษณะคือ สร ้างโดยใช ้ Contents ื่ ตัวแปร”] Application.Contents[“ชอ Application.Contents[“name”] = “aaaaa”; สร ้างโดยไม่ใช ้ Contents ื่ ตัวแปร”] Application [“ชอ Application[“name”] = “aaaaa”; ภายใต ้ขอบเขต โดยปกติแล ้วจะสร ้างไว ้ในไฟล์ Global.asax 28 [email protected] Application ้ การนาตัวแปรไปใชงาน สร ้างโดยใช ้ Contents string n = “”; n = Application.Contents[“name”]; สร ้างโดยไม่ใช ้ Contents string n = “”; n = Application[“name”]; 29 [email protected] Application Method ทีส ่ าคัญได ้แก่ ้ Lock สาหรับล็อคไม่ให ้ผู ้อืน ่ ใชงาน UnLock สาหรับยกเลิกการล็อค ื่ ตัวแปร”] Remove[“ชอ ลบตัวแปรนั น ้ ๆ ออก RemoveAll ลบตัวแปรทัง้ หมดออกไป ื่ ตัวแปรทุกตัวของ HttpApplicationState AllKeys แสดงชอ Count นั บจานวนตัวแปรทุกตัวของ HttpApplicationState Add() เพิม ่ ตัวแปร *** Clear() ลบตัวแปรทัง้ หมดออกไป = RemoveAll 30 [email protected] Global.asax(.Net 2003) using System; using System.Collections; using System.ComponentModel; using System.Web; using System.Web.SessionState; namespace Web_08 { public class Global : System.Web.HttpApplication { private System.ComponentModel.IContainer components = null; } } public Global() { InitializeComponent(); } protected void Application_Start(Object sender, EventArgs e) { Application.Contents["name"] = "aaaaaa"; Application.Contents["major"] = "itmju"; } protected void Application_End(Object sender, EventArgs e) { Application.RemoveAll(); } 31 [email protected] Global.asax (.Net 2005) <%@ Application Language="C#" %> <script runat="server"> void Application_Start(object sender, EventArgs e) { // Code that runs on application startup Application.Contents["name"] = "aaaaaa"; Application.Contents["major"] = "itmju"; } void Application_End(object sender, EventArgs e) { // Code that runs on application shutdown Application.RemoveAll(); } </script> 32 [email protected] Application Example – WebForm5.aspx <%@ Page Language="c#" … %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <title>WebForm5</title> </head> <body MS_POSITIONING="GridLayout"> Name = <%=Application.Contents["name"]%><br> Major = <%=Application.Contents["major"]%><br> </body> </html> 33 [email protected] Application Example – WebForm5.aspx 34 [email protected] Application Example 1 <% Option Explicit %> 2 <html> 3 <head> 4 <title> Simple counter </title> 5 </head> 6 <body> 7 <p>Page requests: <% 8 Application[“hits”] = Application[“hits”] + 1; 9 Response.Write(Application[“hits”]); 10 %></p> 11 </body> 12 </html> 35 [email protected] Application Example browser browser browser browser 1 2 1 2 reads "hit" reads "hit" stores "hit + 1" stores "hit + 1" 8 Application.Lock() 9 Application["hits”] = Application["hits”] + 1 10 Application.Unlock() 36 [email protected] Session ้ ละคนว่ามาจากเครือ Session ถูกใชส้ าหรับการแยกผู ้ใชแต่ ่ งไหน และใครเป็ น ผู ้สง่ Request อะไรมา และจะสง่ Response กลับไปยัง Client ถูกเครือ ่ งได ้ อย่างไร 37 [email protected] Session 38 [email protected] Session Method ทีส ่ าคัญได ้แก่ Abandon การทาลาย Object Session ื่ ตัวแปร”] Remove[“ชอ ลบตัวแปรนัน ้ ๆ ออก RemoveAll ลบตัวแปรทัง้ หมดออกจาก Session สร ้างโดยใช ้ Contents ื่ ตัวแปร”] Session.Contents[“ชอ Session.Contents[“count”] = 0 สร ้างโดยไม่ใช ้ Contents ื่ ตัวแปร”] Session[“ชอ Session[“count”] = 0 คุณสมบัตท ิ ส ี่ าคัญได ้แก่ SessionID ใชก้ าหนดรหัสของแต่ละเครือ ่ ง TimeOut กาหนดระยะเวลาของ Session (ค่า default = 20 นาที) 39 [email protected] Advantages : It helps to maintain user states and data to all over the application. It can easily be implemented and we can store any kind of object. Stores every client data separately. Session is secure and transparent from user. Disadvantages : Performance overhead in case of large volume of user, because of session data stored in server memory. Overhead involved in serializing and De-Serializing session Data. because In case of StateServer and SQLServer session mode we need to serialize the object before store. 40 [email protected] Global.asax (.Net 2003) using System; using System.Collections; using System.ComponentModel; using System.Web; using System.Web.SessionState; namespace Web_08 { public class Global : System.Web.HttpApplication { private System.ComponentModel.IContainer components = null; public Global() { InitializeComponent(); } protected void Session_Start(Object sender, EventArgs e) { Session["count"] = 0; } .... } } 41 [email protected] Global.asax (.Net 2005) <%@ Application Language="C#" %> <script runat="server"> void Session_Start(object sender, EventArgs e) { // Code that runs when a new session is started Session["count"] = 0; } void Session_End(object sender, EventArgs e) { // Code that runs when a session ends. // Note: The Session_End event is raised only when the sessionstate mode // is set to InProc in the Web.config file. If session mode is set to StateServer // or SQLServer, the event is not raised. } </script> 42 [email protected] Session Example – WebForm5.aspx <%@ Page Language="c#" AutoEventWireup="false" CodebeFile="WebForm5.aspx.cs" Inherits="Web_08_WebForm5"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <title>WebForm5</title> </head> <body MS_POSITIONING="GridLayout"> <form id="Form1" method="post" runat="server"> Name = <%=Application.Contents("name")%><br> Major = <%=Application.Contents("major")%><br> count = <%=Session["count"]%><br> <%Session["count"] = int.Parse(Session["count"].ToString()) +1; %> </form> </body> </html> 43 [email protected] Session Example – WebForm5.aspx 44 [email protected] Application & Session <body> <p>Your page requests: <% Session.Contents["hits"] = Convert.ToInt32(Session.Contents["hits"]) + 1; Response.Write(Session.Contents["hits"]) ; %></p> <p>Total page requests: <% Application.Lock() ; Application.Contents["hits"] = Convert.ToInt32(Application.Contents["hits"]) +1; Application.UnLock() ; Response.Write(Application.Contents["hits"]); %></p> </body> 45 [email protected] 46 [email protected]