Post on 21-Jan-2016
description
attawit_it@hotmail.com1
ทสทส215215 การเขียนโปรแกรมบนเว็�บ การเขียนโปรแกรมบนเว็�บ 11
อาจารย�อรรถว็�ท ชั�งคมานนท�สาขีาว็�ชัาเทคโนโลยสารสนเทศ
คณะว็�ทยาศาสตร�www.itsci.mju.ac.th
Cookie & Session
attawit_it@hotmail.com12
CookiesCookies
attawit_it@hotmail.com13
attawit_it@hotmail.com14
CookiesCookies
คื�อ collection ที่��ใช้ในการเก�บข้อมู�ลช้น�ดหน��ง ในการสื่��อสื่ารระหว่�าง client และ server ที่��มู�การเก�บร กษาข้อมู�ลบางอย่�างไว่บนเคืร��องข้องผู้�ใช้ เพื่��อจะน'าไปใช้ใหมู�ภาย่หล ง
Cookies ถู�กจ ดการผู้�าน object Response โดย่ตรง และ การกระที่'าผู้�าน object HttpCookie
ร�ปแบบการบ นที่�ก Cookies ผู้�าน ResponseResponse.Cookies[“CookieName”][[“SubKey”].Property] = Value
attawit_it@hotmail.com15
Cookies Cookies ผ่#าน ผ่#าน ResponseResponse
คื-ณสื่มูบ ต�ที่��สื่'าคื ญไดแก� Expires ใช้อ�านหร�อก'าหนดว่ นหมูดอาย่-ข้อง Cookies Domain ก'าหนดโดเมูนที่��เป0นผู้�บ นที่�ก Cookies น�1 HasKeys ตรว่จสื่อบว่�ามู�คื�ย่2ย่�อย่ ๆ อ�กหร�อไมู�
attawit_it@hotmail.com16
Cookies Cookies ผ่#าน ผ่#าน ResponseResponse
ต ว่อย่�างการสื่ราง CookiesResponse.Cookies[“Cookies1”][“name”] = “aaaaa”;Response.Cookies[“Cookies1”][“major”] = “itmju”;Response.Cookies[“Cookies1”].Expires=DateTime.Now.AddDays(30)
;คื�อการก'าหนดว่ นหมูดอาย่- 30 ว่ น
ต ว่อย่�างการอ�านข้อมู�ลจาก CookiesStr = Request.Cookies[“Cookies1”][“name”];
attawit_it@hotmail.com17
Cookies Cookies ผ่#าน ผ่#าน HttpCookieHttpCookie
การสื่ราง object Cookies 1HttpCookie myCookie = new HttpCookie("Cookies ");
.[""] = ""; myCookie.Values["major"] = "itmju";
myCookie.Domain = "iTech.mju.ac.th"; myCookie.Expires = DateTime.Now.AddDays(30);
..();
attawit_it@hotmail.com18
Cookies Cookies ผ่#าน ผ่#าน HttpCookieHttpCookie
การอ�าน object Cookies เช้�น
HttpCookie myCookie = new HttpCookie("Cookies1");myCookie = Request.Cookies["Cookies1"];if (myCookie != null){
Label1.Text = myCookie.Values["name"];Label2.Text = myCookie.Values["major"];
}
attawit_it@hotmail.com19
Cookies Example – Cookies Example – WebForm3.aspxWebForm3.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>
attawit_it@hotmail.com20
Cookies Example – Cookies Example – WebForm3.aspxWebForm3.aspx
attawit_it@hotmail.com21
Cookies Example – Cookies Example – WebForm3.aspx.csWebForm3.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);}
}
attawit_it@hotmail.com22
Cookies Example – Cookies Example – WebForm4.aspxWebForm4.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>
attawit_it@hotmail.com23
Cookies Example – Cookies Example – WebForm4.aspx.csWebForm4.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;
}}
attawit_it@hotmail.com24
Cookies Example – WebForm4.aspxCookies Example – WebForm4.aspx
attawit_it@hotmail.com
C:\Documents and Settings\Administrator\Cookies
25
attawit_it@hotmail.com
Advantages of CookiesAdvantages 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
attawit_it@hotmail.com
Disadvantages of CookiesDisadvantages 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
attawit_it@hotmail.com28
ApplicationApplication
คื�อ ต ว่แปรในระด บที่��แต�ละเพื่จข้องเว่�บตองการใช้ในการที่'างาน ภาย่ใตข้อบเข้ตเด�ย่ว่ก น มู� 2 ล กษณะคื�อ สื่รางโดย่ใช้ Contents
Application.Contents[“ช้��อต ว่แปร”] Application.Contents[“name”] = “aaaaa”;
สื่รางโดย่ไมู�ใช้ Contents Application [“ช้��อต ว่แปร”]
Application[“name”] = “aaaaa”;
โดย่ปกต�แลว่จะสื่รางไว่ในไฟล2 Global.asax
attawit_it@hotmail.com29
ApplicationApplication
การน'าต ว่แปรไปใช้งาน สื่รางโดย่ใช้ Contents
string n = “”;n = Application.Contents[“name”];
สื่รางโดย่ไมู�ใช้ Contentsstring n = “”;n = Application[“name”];
attawit_it@hotmail.com30
ApplicationApplication
Method ที่��สื่'าคื ญไดแก� Lock สื่'าหร บล�อคืไมู�ใหผู้�อ��นใช้งาน UnLock สื่'าหร บย่กเล�กการล�อคื Remove[“ช้��อต ว่แปร”] ลบต ว่แปรน 1น ๆ ออก RemoveAll ลบต ว่แปรที่ 1งหมูดออกไป AllKeys แสื่ดงช้��อต ว่แปรที่-กต ว่ข้อง HttpApplicationState Count น บจ'านว่นต ว่แปรที่-กต ว่ข้อง HttpApplicationState Add() เพื่��มูต ว่แปร *** Clear() ลบต ว่แปรที่ 1งหมูดออกไป = RemoveAll
attawit_it@hotmail.com31
Global.asax(.Net 2003)Global.asax(.Net 2003)
u sing System; using System.Collections; using System.ComponentModel; using System.Web; using System.Web.SessionState;
namespace Web_0 8 { SSSSSSSSSSSSSSSSSSSSSSSSSS S:..{ 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(); }
}}
attawit_it@hotmail.com32
Global.asax (.Net 2005)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>
attawit_it@hotmail.com33
Application Example – WebForm5.aspxApplication 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>
attawit_it@hotmail.com34
Application Example – WebForm5.aspxApplication Example – WebForm5.aspx
attawit_it@hotmail.com35
Application ExampleApplication 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>
attawit_it@hotmail.com36
Application ExampleApplication 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”] + 1 10 Application.Unlock()
attawit_it@hotmail.com37
SessionSession
Session ถู�กใช้สื่'าหร บการแย่กผู้�ใช้แต�ละคืนว่�ามูาจากเคืร��องไหน และใคืรเป0นผู้�สื่�ง Request อะไรมูา และจะสื่�ง Response กล บไปย่ ง Client ถู�กเคืร��องไดอย่�างไร
attawit_it@hotmail.com38
SessionSession
attawit_it@hotmail.com39
SessionSession
Method ที่��สื่'าคื ญไดแก� Abandon การที่'าลาย่ Object Session Remove[“ช้��อต ว่แปร”] ลบต ว่แปรน 1น ๆ ออก RemoveAll ลบต ว่แปรที่ 1งหมูดออกจาก Session
สื่รางโดย่ใช้ ContentsSession.Contents[“ช้��อต ว่แปร”]Session.Contents[“count”] = 0
สื่รางโดย่ไมู�ใช้ ContentsSession[“ช้��อต ว่แปร”]Session[“count”] = 0
คื-ณสื่มูบ ต�ที่��สื่'าคื ญไดแก� SessionID ใช้ก'าหนดรห สื่ข้องแต�ละเคืร��อง TimeOut ก'าหนดระย่ะเว่ลาข้อง Session
(คื�า default = 20 นาที่�)
attawit_it@hotmail.com
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
attawit_it@hotmail.com41
Global.asax (.Net 2003)Global.asax (.Net 2003)
u SSSSSSSSSSSSSSSSSSS;. ; using System.ComponentModel; using System.Web; using System.Web.SessionState;
namespace Web_0 8 { public class Global : System.Web.HttpApplication {
SSSSS.. =; public Global() { InitializeComponent(); } protected void Session_Start(Object sender, EventArgs e) {
Session["count"] = 0; } ....}
}
attawit_it@hotmail.com42
Global.asax (.Net 2005)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
0Session["count"] = ; } 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>
attawit_it@hotmail.com43
Session Example – Session Example – WebForm5.aspxWebForm5.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>
attawit_it@hotmail.com44
Session Example – Session Example – WebForm5.aspxWebForm5.aspx
attawit_it@hotmail.com45
Application & SessionApplication & 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>
attawit_it@hotmail.com46