PDA

View Full Version : Báo lỗi khi chưa khai báo Cookie mà kiểm tra Cookie !



NgocHien
16-07-2003, 14:39
Em có 1 théc méc.

Em làm 1 trang trong đó nếu người truy cập vào thì mình sẽ kiểm tra họ đã vào truy cập chưa, nếu chưa thì tạo 1 Cookie và đặt 1 cookie với giá trị là Yes.

Bây giờ em muốn kiểm tra người đó vào hay chưa em kiểm tra giá trị của Cookie, nếu là = "" thì sẽ báo là chưa, và tạo Cookie cho người này

Nhưng khi chạy thì nó báo lỗi là Cookie này Null, vậy lèm sao ?

Cảm ơn các anh nhiều

1011
16-07-2003, 15:31
Cookies:
Ghi cookies


'Theo kiểu ASP
Response.Cookies("userName").Value = "test"
Response.Cookies("userName").Expires = DateTime.Now.AddDays(1)

'Theo kiểu ASP.NET
Dim mCookie As New HttpCookie("userName")
mCookie.Value = "test"
mCookie.Expires = DateTime.Now.AddDays(1)
Response.Cookies.Add(mCookie)


Đọc Cookies


'Kiểu ASP
If Not Request.Cookies("userName") Is Nothing Then
userName = Server.HtmlEncode(Request.Cookies("userName").Value)
End If

'Kiểu ASP.NET
If Not Request.Cookies("userName") Is Nothing Then
Dim mCookie As HttpCookie = Request.Cookies("userName")
userName = Server.HtmlEncode(mCookie.Value)
End If


Dùng Is Nothing để kiểm tra một cookies có giá trị hay chưa.

1011
16-07-2003, 17:03
Cookies là gì?
Cookies là các giá trị được lưu ở máy client, nó chứa một số thông tin của một ứng dụng Web. Nó có thể được đọc bất cứ khi nào có user truy cập vào site nhờ đối tượng Request.
Giá trị của Cookies được lưu ở một thư mục nào đó trên ổ cứng của user.
Cookies thường được dùng để lưu các thông tin về người dùng của một site. Hầu hết các browser hỗ trợ tối đa là 4096 byte dành cho cookies.

Ghi cookies:
Dùng thuộc tính Response của một Page.
Một cookies thường có: tên, giá trị và thời hạn của cookies.
Khi tạo một cookies mà không chỉ định thời hạn (expires), nó sẽ không được lưu trên ổ cứng của user; nó chỉ được duy trì như session thôi. Trong trường hợp này khi người dùng đóng trình duyệt hay session time out thì cookies cũng bị hủy.
Ví dụ:


'ghi lại thông tin người dùng
Response.Cookies("userName").Value = "1011"
Response.Cookies("userName").Expires = DateTime.Now.AddDays(1)
'hoặc
'ghi lại thời gian vào trang web
Dim aCookie As New HttpCookie("lastVisit")
aCookie.Value = DateTime.Now.ToString
aCookie.Expires = DateTime.Now.AddDays(1)
Response.Cookies.Add(aCookie)


Multi-valued cookies:
Chúng ta có thể lưu nhiều giá trị trong một cookies theo từng cặp name-value.
Ví dụ:


Response.Cookies("userInfo")("userName") = "1011"
Response.Cookies("userInfo")("lastVisit") = DateTime.Now.ToString
Response.Cookies("userInfo").Expires = DateTime.Now.AddDays(1)
'hoặc:
Dim aCookie As New HttpCookie("userInfo")
aCookie.Values("userName") = "1011"
aCookie.Values("lastVisit") = DateTime.Now.ToString
aCookie.Expires = DateTime.Now.AddDays(1)
Response.Cookies.Add(aCookie)


Khi muốn ghi Cookies cho một thư mục nào đó từ thư mục gốc của ứng dụng Web ta phải sử dụng thuộc tính Path của Cookies.
Ví dụ:


'để ghi cookies cho /sharelib (www.diendantinhoc.com/sharelib)
Dim appCookie As New HttpCookie("AppCookie")
appCookie.Value = "123"
appCookie.Expires = Now.AddDays(1)
appCookie.Path = "/sharelib"
Response.Cookies.Add(appCookie)

nếu ở đây không dùng thuộc tình Path thì khi đọc cookies chỉ nhận được các cookies của diendantinhoc.com mà thôi
Đọc cookies:
Dùng đối tượng Request để lấy các thông tin về cookies đã được lưu trên client
Ví dụ:


If Not Request.Cookies("userName") Is Nothing Then
userName = Server.HtmlEncode(Request.Cookies("userName").Value)
End If
'hoặc:
If Not Request.Cookies("userName") Is Nothing Then
Dim aCookie As HttpCookie = Request.Cookies("userName")
userName = Server.HtmlEncode(aCookie.Value)
End If

Đọc cookies multi-value:


If Not Request.Cookies("userInfo") Is Nothing Then
userName = Server.HtmlEncode(Request.Cookies("userInfo")("userName"))
lastVisit = Server.HtmlEncode(Request.Cookies("userInfo")("lastVisit"))
End If
'hoặc:
If Not Request.Cookies("userInfo") Is Nothing Then
Dim UserInfoCookieCollection As _
System.Collections.Specialized.NameValueCollection
UserInfoCookieCollection = Request.Cookies("userInfo").Values
userName = Server.HtmlEncode(UserInfoCookieCollection("userName"))
lastVisit = Server.HtmlEncode(UserInfoCookieCollection("lastVisit"))
End If

Một vấn đề thường gặp là không biết browser có hỗ trợ cho việc ghi cookies hay không. Vì vậy cần kiểm tra việc có hay không dùng cookies trong site của mình.

NgocHien
17-07-2003, 09:24
THật tuyệt, cảm ơn các anh nhiều