1 / 36

ทส 215 การเขียนโปรแกรมบนเว็บ 1

ทส 215 การเขียนโปรแกรมบนเว็บ 1. Cookie & Session. อาจารย์อรรถวิท ชังคมานนท์ สาขาวิชาเทคโนโลยีสารสนเทศ คณะวิทยาศาสตร์ www.itsci.mju.ac.th. Cookies. Cookies.

dana
Télécharger la présentation

ทส 215 การเขียนโปรแกรมบนเว็บ 1

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ทส215การเขียนโปรแกรมบนเว็บ 1 Cookie & Session อาจารย์อรรถวิท ชังคมานนท์ สาขาวิชาเทคโนโลยีสารสนเทศ คณะวิทยาศาสตร์ www.itsci.mju.ac.th

  2. Cookies

  3. Cookies • คือ collection ที่ใช้ในการเก็บข้อมูลชนิดหนึ่ง ในการสื่อสารระหว่าง client และ server ที่มีการเก็บรักษาข้อมูลบางอย่างไว้บนเครื่องของผู้ใช้ เพื่อจะนำไปใช้ใหม่ภายหลัง • Cookies ถูกจัดการผ่าน object Responseโดยตรง และ การกระทำผ่าน object HttpCookie • รูปแบบการบันทึก Cookies ผ่าน Response Response.Cookies[“CookieName”][[“SubKey”].Property] = Value

  4. Cookies ผ่าน Response • คุณสมบัติที่สำคัญได้แก่ • Expires ใช้อ่านหรือกำหนดวันหมดอายุของ Cookies • Domain กำหนดโดเมนที่เป็นผู้บันทึก Cookies นี้ • HasKeys ตรวจสอบว่ามีคีย์ย่อย ๆ อีกหรือไม่

  5. 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”];

  6. 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);

  7. 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"]; }

  8. 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:Buttonid="Button1" runat="server“ Text="Add"></asp:Button> </form> </body> </HTML>

  9. Cookies Example – WebForm3.aspx

  10. 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); } }

  11. 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:Labelid="Label1" runat="server" >Label</asp:Label> <asp:Label id="Label2" runat="server">Label</asp:Label> </form> </body> </HTML>

  12. 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; } }

  13. Cookies Example – WebForm4.aspx

  14. C:\Documents and Settings\Administrator\Cookies

  15. 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.

  16. 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.]

  17. Application • คือ ตัวแปรในระดับที่แต่ละเพจของเว็บต้องการใช้ในการทำงานภายใต้ขอบเขตเดียวกัน มี 2 ลักษณะคือ • สร้างโดยใช้ Contents Application.Contents[“ชื่อตัวแปร”] Application.Contents[“name”] = “aaaaa”; • สร้างโดยไม่ใช้ Contents Application [“ชื่อตัวแปร”] Application[“name”] = “aaaaa”; • โดยปกติแล้วจะสร้างไว้ในไฟล์ Global.asax

  18. Application • การนำตัวแปรไปใช้งาน • สร้างโดยใช้ Contents string n = “”; n = Application.Contents[“name”]; • สร้างโดยไม่ใช้ Contents string n = “”; n = Application[“name”];

  19. Application • Method ที่สำคัญได้แก่ • Lock สำหรับล็อคไม่ให้ผู้อื่นใช้งาน • UnLock สำหรับยกเลิกการล็อค • Remove[“ชื่อตัวแปร”] ลบตัวแปรนั้น ๆ ออก • RemoveAll ลบตัวแปรทั้งหมดออกไป • AllKeys แสดงชื่อตัวแปรทุกตัวของ HttpApplicationState • Count นับจำนวนตัวแปรทุกตัวของ HttpApplicationState • Add() เพิ่มตัวแปร *** • Clear() ลบตัวแปรทั้งหมดออกไป = RemoveAll

  20. 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(); } } }

  21. 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>

  22. 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>

  23. Application Example – WebForm5.aspx

  24. 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>

  25. Application Example • browser 1 reads "hit" • browser 2 reads "hit" • browser 1 stores "hit + 1" • browser 2 stores "hit + 1" 8    Application.Lock() 9    Application["hits”] = Application["hits”] + 110   Application.Unlock()

  26. Session • Session ถูกใช้สำหรับการแยกผู้ใช้แต่ละคนว่ามาจากเครื่องไหน และใครเป็นผู้ส่ง Request อะไรมา และจะส่ง Response กลับไปยัง Client ถูกเครื่องได้อย่างไร

  27. Session

  28. 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 นาที)

  29. 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.

  30. 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; } .... } }

  31. 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>

  32. 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>

  33. Session Example – WebForm5.aspx

  34. 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>

  35. Q&A

More Related