I
idog
i noticed weird cookie problems when clients sometimes visit my site with
the www. prefix and sometimes without the prefix. I decided to start
setting the .domain property, but that doesnt work for localhost. So here
is my solution hopefully it saves someone some grief. Let me know if I
missed something obvious:
Response.Cookies.Add(WriteCookie("testUID", TextBox1.Text,
Request.Url.Host))
Public Function WriteCookie(ByVal strKey As String, ByVal strValue As
String, ByVal strHost As String) As HttpCookie
Dim cook As New HttpCookie(strKey, strValue)
cook.Expires = Now.AddYears(1)
If strHost <> "localhost" Then
Dim myArray As String()
myArray = strHost.Split(".")
If myArray.Length > 2 Then
'we only want the last two segments of the host name
strHost = myArray(myArray.Length - 2) & "." & myArray(myArray.Length - 1)
End If
cook.Domain = strHost
End If
WriteCookie = cook
End Function
Public Function DeleteCookie(ByVal strKey As String, ByVal strHost As
String) As HttpCookie
Dim cook As New HttpCookie(strKey)
cook.Expires = Now.AddYears(-30)
If strHost <> "localhost" Then
Dim myArray As String()
myArray = strHost.Split(".")
If myArray.Length > 2 Then
'we only want the last two segments of the host name
strHost = myArray(myArray.Length - 2) & "." & myArray(myArray.Length - 1)
End If
cook.Domain = strHost
End If
DeleteCookie = cook
End Function
the www. prefix and sometimes without the prefix. I decided to start
setting the .domain property, but that doesnt work for localhost. So here
is my solution hopefully it saves someone some grief. Let me know if I
missed something obvious:
Response.Cookies.Add(WriteCookie("testUID", TextBox1.Text,
Request.Url.Host))
Public Function WriteCookie(ByVal strKey As String, ByVal strValue As
String, ByVal strHost As String) As HttpCookie
Dim cook As New HttpCookie(strKey, strValue)
cook.Expires = Now.AddYears(1)
If strHost <> "localhost" Then
Dim myArray As String()
myArray = strHost.Split(".")
If myArray.Length > 2 Then
'we only want the last two segments of the host name
strHost = myArray(myArray.Length - 2) & "." & myArray(myArray.Length - 1)
End If
cook.Domain = strHost
End If
WriteCookie = cook
End Function
Public Function DeleteCookie(ByVal strKey As String, ByVal strHost As
String) As HttpCookie
Dim cook As New HttpCookie(strKey)
cook.Expires = Now.AddYears(-30)
If strHost <> "localhost" Then
Dim myArray As String()
myArray = strHost.Split(".")
If myArray.Length > 2 Then
'we only want the last two segments of the host name
strHost = myArray(myArray.Length - 2) & "." & myArray(myArray.Length - 1)
End If
cook.Domain = strHost
End If
DeleteCookie = cook
End Function