Wrox Home  
Search P2P Archive for: Go

  Return to Index  

aspx thread: cookie problem?


Message #1 by "john hamman" <johnhamman@c...> on Fri, 24 May 2002 02:30:56
HELP, 
I have a problem here, When a user comes to the site I check to see if 
they have cookie("UID") and if they dont I write a new one with the GUID 
as the value and redirect them with the Cookies Value appended to the end 
of the URL. BUT for some reason It doesnt write the cookies or something 
because it seems to make a new GUID every time they visit. Below is my 
code. Can anyone see whats going on? You can visit the site that this 
works on at http://www.americangolfclassics.com.
Thanks
john

'Global.asax.vb
Imports System
Imports System.IO
Imports System.Web
Imports hi.redir
imports Microsoft.VisualBasic
imports System.Diagnostics

Public Class Global
    Inherits System.Web.HttpApplication

#Region " Component Designer Generated Code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Component Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Required by the Component Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Component Designer
    'It can be modified using the Component Designer.
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub 
InitializeComponent()
        components = New System.ComponentModel.Container()
    End Sub

#End Region

    Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
        ' Fires when the application is started
    End Sub

    Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
        Session.Timeout = 15
        Dim host As String = Request.ServerVariables("SERVER_NAME").ToLower
        Dim ServerDir As String = ""
        Dim PageURL As String = Request.ServerVariables("URL")

        'Dim obj As redir.Go_to
        'Call obj.newhost(host)
          Dim uGUID As String = ""

        If (GetCookie("UID") Is Nothing) Then
            Dim cookie As New HttpCookie("UID")
            uGUID = System.Guid.NewGuid().ToString.Replace("-", Nothing)
            SetCookie("UID", uGUID, 30)
        Else
            uGUID = Nothing
            uGUID = GetCookie("UID")
        End If

        Select Case host
            Case "RemovedforThisemail.com"
                ServerDir = checkURl(PageURL, "/RemovedforThisemail")
            Case "RemovedforThisemail.com"
                ServerDir = checkURl(PageURL, "RemovedforThisemail")
            Case "RemovedforThisemail.com"
                ServerDir = checkURl(PageURL, "/RemovedforThisemail")
            Case "www.RemovedforThisemail.com"
                ServerDir = checkURl(PageURL, "/RemovedforThisemail")
            Case "RemovedforThisemail.com"
                ServerDir = checkURl(PageURL, "/RemovedforThisemail")
            Case "www.americangolfclassics.com"
                ServerDir = checkURl(PageURL, "/c_AmGolf/Home/" + uGUID 
+ ".hi")
            Case "americangolfclassics.com"
                ServerDir = checkURl(PageURL, "/c_AmGolf/Home/" + uGUID 
+ ".hi")
           
            Case "test.RemovedforThisemail.com"
                ServerDir = checkURl(PageURL, "/c_test/Home/" + uGUID 
+ ".hi")
            Case "RemovedforThisemail.org"
                ServerDir = checkURl(PageURL, "/RemovedforThisemail")
            Case "www.RemovedforThisemail.org"
                ServerDir = checkURl(PageURL, "/RemovedforThisemail")
            Case "RemovedforThisemail.com"
                ServerDir = checkURl(PageURL, "/RemovedforThisemail")
            Case "www.RemovedforThisemail.com"
                ServerDir = checkURl(PageURL, "/RemovedforThisemail")
            Case "RemovedforThisemail.com"
                ServerDir = checkURl(PageURL, "/RemovedforThisemail")
            Case "www.RemovedforThisemail.com"
                ServerDir = checkURl(PageURL, "/RemovedforThisemail")
            Case "RemovedforThisemaile.com"
                ServerDir = checkURl(PageURL, "/RemovedforThisemail")
            Case Else
                ServerDir = checkURl(PageURL, "/iRemovedforThisemail")
        End Select

        Response.Redirect(ServerDir)
    End Sub

    Sub Application_BeginRequest(ByVal sender As Object, ByVal e As 
EventArgs)
        ' Fires at the beginning of each request
    End Sub

    Sub Application_AuthenticateRequest(ByVal sender As Object, ByVal e As 
EventArgs)
        ' Fires upon attempting to authenticate the use
    End Sub

    Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)

        Dim strError As String = "Error in: " + Request.Path + 
ControlChars.Lf + "Url: " + Request.RawUrl + ControlChars.Lf + 
ControlChars.Lf

        ' Get the exception object for the last error message that occured.
        Dim ErrorInfo As Exception = Server.GetLastError().GetBaseException
()
        strError += "Error Message: " + ErrorInfo.Message + 
ControlChars.Lf + "Error Source: " + ErrorInfo.Source + ControlChars.Lf 
+ "Target Site: " + ErrorInfo.TargetSite.ToString() + ControlChars.Lf 
+ "QueryString Data:" + ControlChars.Lf + "-----------------" + 
ControlChars.Lf

        'strError += "Error Target Site: " + ErrorInfo.TargetSite + 
ControlChars.Lf

        ' Gathering QueryString information
        Dim i As Integer
        For i = 0 To Context.Request.QueryString.Count - 1
            strError += Context.Request.QueryString.Keys(i) + ":" + 
ControlChars.Tab + ControlChars.Tab + Context.Request.QueryString(i) + 
ControlChars.Lf
        Next i
        strError += ControlChars.Lf + "Post Data:" + ControlChars.Lf + "---
-------" + ControlChars.Lf

        ' Gathering Post Data information

        For i = 0 To Context.Request.Form.Count - 1
            strError += Context.Request.Form.Keys(i) + ":" + 
ControlChars.Tab + ControlChars.Tab + Context.Request.Form(i) + 
ControlChars.Lf
        Next i
        strError += ControlChars.Lf

        If User.Identity.IsAuthenticated Then
            strError += "User:" + ControlChars.Tab + ControlChars.Tab + 
User.Identity.Name + ControlChars.Lf + ControlChars.Lf
        End If
        strError += "Exception Stack Trace:" + ControlChars.Lf + "---------
-------------" + ControlChars.Lf + Server.GetLastError().StackTrace + 
ControlChars.Lf + ControlChars.Lf + "Server Variables:" + ControlChars.Lf 
+ "-----------------" + ControlChars.Lf

        ' Gathering Server Variables information
        For i = 0 To Context.Request.ServerVariables.Count - 1
            strError += Context.Request.ServerVariables.Keys(i) + ":" + 
ControlChars.Tab + ControlChars.Tab + Context.Request.ServerVariables(i) + 
ControlChars.Lf
        Next i
        strError += ControlChars.Lf

        ' Sending error message to administration via e-mail
        System.Web.Mail.SmtpMail.Send
("Error@h...", "johnhamman@C...", "error has occured", 
strError)
    End Sub

    Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
        ' Fires when the session ends
    End Sub

    Sub Application_End(ByVal sender As Object, ByVal e As EventArgs)
        ' Fires when the application ends
    End Sub

    Public Function checkURl(ByVal url As String, ByVal ServerDir As 
String)
        If Not (url Is Nothing) Then
            If (url.IndexOf(ServerDir) > -1) Then
                Return url
            Else
                Return ServerDir
            End If
        Else
            Return ServerDir
        End If
    End Function
 Dim cookyexpires As String = ""

    Public Function SetCookie(ByVal cookiename As String, ByVal 
cookievalue As String, ByVal iDaysToExpire As Integer) As Boolean
        Try
            Dim objCookie As New HttpCookie(cookiename)
            Response.Cookies.Clear()
            Response.Cookies.Add(objCookie)
            objCookie.Values.Add(cookiename, cookievalue)
            Dim dtExpiry As DateTime = DateTime.Now.AddDays(iDaysToExpire)
            Response.Cookies(cookiename).Expires = dtExpiry
        Catch e As Exception
            Return False
        End Try
        Return True
    End Function 'SetCookie

    Public Function GetCookie(ByVal cookiename As String) As String
        Dim cookyval As String = ""
        Try
            cookyval = Request.Cookies(cookiename).Value
        Catch e As Exception
            cookyval = ""
        End Try
        Return cookyval
    End Function 'GetCookie


End Class
Message #2 by Feduke Cntr Charles R <FedukeCR@m...> on Fri, 24 May 2002 08:35:45 -0400
John,

	My guess is the Replace("-", Nothing) is your culprit.  Nothing
should be the equivalent of null, and when you append/insert a null value
into a string it (from our perspective) blanks the string.  Try one of the
following:

	Replace("-", String.Empty)
	-OR-
	Replace("-", "")

	Please let us know if this works.

HTH,
- Chuck

-----Original Message-----
From: john hamman [mailto:johnhamman@c...]
Sent: Thursday, May 23, 2002 10:31 PM
To: ASP+
Subject: [aspx] cookie problem?


HELP, 
I have a problem here, When a user comes to the site I check to see if 
they have cookie("UID") and if they dont I write a new one with the GUID 
as the value and redirect them with the Cookies Value appended to the end 
of the URL. BUT for some reason It doesnt write the cookies or something 
because it seems to make a new GUID every time they visit. Below is my 
code. Can anyone see whats going on? You can visit the site that this 
works on at http://www.americangolfclassics.com.
Thanks
john

'Global.asax.vb
Imports System
Imports System.IO
Imports System.Web
Imports hi.redir
imports Microsoft.VisualBasic
imports System.Diagnostics

Public Class Global
    Inherits System.Web.HttpApplication

#Region " Component Designer Generated Code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Component Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Required by the Component Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Component Designer
    'It can be modified using the Component Designer.
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub 
InitializeComponent()
        components = New System.ComponentModel.Container()
    End Sub

#End Region

    Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
        ' Fires when the application is started
    End Sub

    Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
        Session.Timeout = 15
        Dim host As String = Request.ServerVariables("SERVER_NAME").ToLower
        Dim ServerDir As String = ""
        Dim PageURL As String = Request.ServerVariables("URL")

        'Dim obj As redir.Go_to
        'Call obj.newhost(host)
          Dim uGUID As String = ""

        If (GetCookie("UID") Is Nothing) Then
            Dim cookie As New HttpCookie("UID")
            uGUID = System.Guid.NewGuid().ToString.Replace("-", Nothing)
            SetCookie("UID", uGUID, 30)
        Else
            uGUID = Nothing
            uGUID = GetCookie("UID")
        End If

        Select Case host
            Case "RemovedforThisemail.com"
                ServerDir = checkURl(PageURL, "/RemovedforThisemail")
            Case "RemovedforThisemail.com"
                ServerDir = checkURl(PageURL, "RemovedforThisemail")
            Case "RemovedforThisemail.com"
                ServerDir = checkURl(PageURL, "/RemovedforThisemail")
            Case "www.RemovedforThisemail.com"
                ServerDir = checkURl(PageURL, "/RemovedforThisemail")
            Case "RemovedforThisemail.com"
                ServerDir = checkURl(PageURL, "/RemovedforThisemail")
            Case "www.americangolfclassics.com"
                ServerDir = checkURl(PageURL, "/c_AmGolf/Home/" + uGUID 
+ ".hi")
            Case "americangolfclassics.com"
                ServerDir = checkURl(PageURL, "/c_AmGolf/Home/" + uGUID 
+ ".hi")
           
            Case "test.RemovedforThisemail.com"
                ServerDir = checkURl(PageURL, "/c_test/Home/" + uGUID 
+ ".hi")
            Case "RemovedforThisemail.org"
                ServerDir = checkURl(PageURL, "/RemovedforThisemail")
            Case "www.RemovedforThisemail.org"
                ServerDir = checkURl(PageURL, "/RemovedforThisemail")
            Case "RemovedforThisemail.com"
                ServerDir = checkURl(PageURL, "/RemovedforThisemail")
            Case "www.RemovedforThisemail.com"
                ServerDir = checkURl(PageURL, "/RemovedforThisemail")
            Case "RemovedforThisemail.com"
                ServerDir = checkURl(PageURL, "/RemovedforThisemail")
            Case "www.RemovedforThisemail.com"
                ServerDir = checkURl(PageURL, "/RemovedforThisemail")
            Case "RemovedforThisemaile.com"
                ServerDir = checkURl(PageURL, "/RemovedforThisemail")
            Case Else
                ServerDir = checkURl(PageURL, "/iRemovedforThisemail")
        End Select

        Response.Redirect(ServerDir)
    End Sub

    Sub Application_BeginRequest(ByVal sender As Object, ByVal e As 
EventArgs)
        ' Fires at the beginning of each request
    End Sub

    Sub Application_AuthenticateRequest(ByVal sender As Object, ByVal e As 
EventArgs)
        ' Fires upon attempting to authenticate the use
    End Sub

    Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)

        Dim strError As String = "Error in: " + Request.Path + 
ControlChars.Lf + "Url: " + Request.RawUrl + ControlChars.Lf + 
ControlChars.Lf

        ' Get the exception object for the last error message that occured.
        Dim ErrorInfo As Exception = Server.GetLastError().GetBaseException
()
        strError += "Error Message: " + ErrorInfo.Message + 
ControlChars.Lf + "Error Source: " + ErrorInfo.Source + ControlChars.Lf 
+ "Target Site: " + ErrorInfo.TargetSite.ToString() + ControlChars.Lf 
+ "QueryString Data:" + ControlChars.Lf + "-----------------" + 
ControlChars.Lf

        'strError += "Error Target Site: " + ErrorInfo.TargetSite + 
ControlChars.Lf

        ' Gathering QueryString information
        Dim i As Integer
        For i = 0 To Context.Request.QueryString.Count - 1
            strError += Context.Request.QueryString.Keys(i) + ":" + 
ControlChars.Tab + ControlChars.Tab + Context.Request.QueryString(i) + 
ControlChars.Lf
        Next i
        strError += ControlChars.Lf + "Post Data:" + ControlChars.Lf + "---
-------" + ControlChars.Lf

        ' Gathering Post Data information

        For i = 0 To Context.Request.Form.Count - 1
            strError += Context.Request.Form.Keys(i) + ":" + 
ControlChars.Tab + ControlChars.Tab + Context.Request.Form(i) + 
ControlChars.Lf
        Next i
        strError += ControlChars.Lf

        If User.Identity.IsAuthenticated Then
            strError += "User:" + ControlChars.Tab + ControlChars.Tab + 
User.Identity.Name + ControlChars.Lf + ControlChars.Lf
        End If
        strError += "Exception Stack Trace:" + ControlChars.Lf + "---------
-------------" + ControlChars.Lf + Server.GetLastError().StackTrace + 
ControlChars.Lf + ControlChars.Lf + "Server Variables:" + ControlChars.Lf 
+ "-----------------" + ControlChars.Lf

        ' Gathering Server Variables information
        For i = 0 To Context.Request.ServerVariables.Count - 1
            strError += Context.Request.ServerVariables.Keys(i) + ":" + 
ControlChars.Tab + ControlChars.Tab + Context.Request.ServerVariables(i) + 
ControlChars.Lf
        Next i
        strError += ControlChars.Lf

        ' Sending error message to administration via e-mail
        System.Web.Mail.SmtpMail.Send
("Error@h...", "johnhamman@C...", "error has occured", 
strError)
    End Sub

    Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
        ' Fires when the session ends
    End Sub

    Sub Application_End(ByVal sender As Object, ByVal e As EventArgs)
        ' Fires when the application ends
    End Sub

    Public Function checkURl(ByVal url As String, ByVal ServerDir As 
String)
        If Not (url Is Nothing) Then
            If (url.IndexOf(ServerDir) > -1) Then
                Return url
            Else
                Return ServerDir
            End If
        Else
            Return ServerDir
        End If
    End Function
 Dim cookyexpires As String = ""

    Public Function SetCookie(ByVal cookiename As String, ByVal 
cookievalue As String, ByVal iDaysToExpire As Integer) As Boolean
        Try
            Dim objCookie As New HttpCookie(cookiename)
            Response.Cookies.Clear()
            Response.Cookies.Add(objCookie)
            objCookie.Values.Add(cookiename, cookievalue)
            Dim dtExpiry As DateTime = DateTime.Now.AddDays(iDaysToExpire)
            Response.Cookies(cookiename).Expires = dtExpiry
        Catch e As Exception
            Return False
        End Try
        Return True
    End Function 'SetCookie

    Public Function GetCookie(ByVal cookiename As String) As String
        Dim cookyval As String = ""
        Try
            cookyval = Request.Cookies(cookiename).Value
        Catch e As Exception
            cookyval = ""
        End Try
        Return cookyval
    End Function 'GetCookie


End Class
Message #3 by "John Hamman {Hamman Interactive}" <johnhamman@C...> on Fri, 24 May 2002 11:07:44 -0400
Thank you so much, I have had this code posted all over the place but have
yet to find the problem. Unfortunatly it still didnt work. when you go to
the page it still writes a new cookie for the new sesion.

john

-----Original Message-----
From: Feduke Cntr Charles R [mailto:FedukeCR@m...]
Sent: Friday, May 24, 2002 8:36 AM
To: ASP+
Subject: [aspx] RE: cookie problem?


John,

	My guess is the Replace("-", Nothing) is your culprit.  Nothing
should be the equivalent of null, and when you append/insert a null value
into a string it (from our perspective) blanks the string.  Try one of the
following:

	Replace("-", String.Empty)
	-OR-
	Replace("-", "")

	Please let us know if this works.

HTH,
- Chuck

-----Original Message-----
From: john hamman [mailto:johnhamman@c...]
Sent: Thursday, May 23, 2002 10:31 PM
To: ASP+
Subject: [aspx] cookie problem?


HELP,
I have a problem here, When a user comes to the site I check to see if
they have cookie("UID") and if they dont I write a new one with the GUID
as the value and redirect them with the Cookies Value appended to the end
of the URL. BUT for some reason It doesnt write the cookies or something
because it seems to make a new GUID every time they visit. Below is my
code. Can anyone see whats going on? You can visit the site that this
works on at http://www.americangolfclassics.com.
Thanks
john

'Global.asax.vb
Imports System
Imports System.IO
Imports System.Web
Imports hi.redir
imports Microsoft.VisualBasic
imports System.Diagnostics

Public Class Global
    Inherits System.Web.HttpApplication

#Region " Component Designer Generated Code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Component Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Required by the Component Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Component Designer
    'It can be modified using the Component Designer.
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
        components = New System.ComponentModel.Container()
    End Sub

#End Region

    Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
        ' Fires when the application is started
    End Sub

    Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
        Session.Timeout = 15
        Dim host As String = Request.ServerVariables("SERVER_NAME").ToLower
        Dim ServerDir As String = ""
        Dim PageURL As String = Request.ServerVariables("URL")

        'Dim obj As redir.Go_to
        'Call obj.newhost(host)
          Dim uGUID As String = ""

        If (GetCookie("UID") Is Nothing) Then
            Dim cookie As New HttpCookie("UID")
            uGUID = System.Guid.NewGuid().ToString.Replace("-", Nothing)
            SetCookie("UID", uGUID, 30)
        Else
            uGUID = Nothing
            uGUID = GetCookie("UID")
        End If

        Select Case host
            Case "RemovedforThisemail.com"
                ServerDir = checkURl(PageURL, "/RemovedforThisemail")
            Case "RemovedforThisemail.com"
                ServerDir = checkURl(PageURL, "RemovedforThisemail")
            Case "RemovedforThisemail.com"
                ServerDir = checkURl(PageURL, "/RemovedforThisemail")
            Case "www.RemovedforThisemail.com"
                ServerDir = checkURl(PageURL, "/RemovedforThisemail")
            Case "RemovedforThisemail.com"
                ServerDir = checkURl(PageURL, "/RemovedforThisemail")
            Case "www.americangolfclassics.com"
                ServerDir = checkURl(PageURL, "/c_AmGolf/Home/" + uGUID
+ ".hi")
            Case "americangolfclassics.com"
                ServerDir = checkURl(PageURL, "/c_AmGolf/Home/" + uGUID
+ ".hi")

            Case "test.RemovedforThisemail.com"
                ServerDir = checkURl(PageURL, "/c_test/Home/" + uGUID
+ ".hi")
            Case "RemovedforThisemail.org"
                ServerDir = checkURl(PageURL, "/RemovedforThisemail")
            Case "www.RemovedforThisemail.org"
                ServerDir = checkURl(PageURL, "/RemovedforThisemail")
            Case "RemovedforThisemail.com"
                ServerDir = checkURl(PageURL, "/RemovedforThisemail")
            Case "www.RemovedforThisemail.com"
                ServerDir = checkURl(PageURL, "/RemovedforThisemail")
            Case "RemovedforThisemail.com"
                ServerDir = checkURl(PageURL, "/RemovedforThisemail")
            Case "www.RemovedforThisemail.com"
                ServerDir = checkURl(PageURL, "/RemovedforThisemail")
            Case "RemovedforThisemaile.com"
                ServerDir = checkURl(PageURL, "/RemovedforThisemail")
            Case Else
                ServerDir = checkURl(PageURL, "/iRemovedforThisemail")
        End Select

        Response.Redirect(ServerDir)
    End Sub

    Sub Application_BeginRequest(ByVal sender As Object, ByVal e As
EventArgs)
        ' Fires at the beginning of each request
    End Sub

    Sub Application_AuthenticateRequest(ByVal sender As Object, ByVal e As
EventArgs)
        ' Fires upon attempting to authenticate the use
    End Sub

    Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)

        Dim strError As String = "Error in: " + Request.Path +
ControlChars.Lf + "Url: " + Request.RawUrl + ControlChars.Lf +
ControlChars.Lf

        ' Get the exception object for the last error message that occured.
        Dim ErrorInfo As Exception = Server.GetLastError().GetBaseException
()
        strError += "Error Message: " + ErrorInfo.Message +
ControlChars.Lf + "Error Source: " + ErrorInfo.Source + ControlChars.Lf
+ "Target Site: " + ErrorInfo.TargetSite.ToString() + ControlChars.Lf
+ "QueryString Data:" + ControlChars.Lf + "-----------------" +
ControlChars.Lf

        'strError += "Error Target Site: " + ErrorInfo.TargetSite +
ControlChars.Lf

        ' Gathering QueryString information
        Dim i As Integer
        For i = 0 To Context.Request.QueryString.Count - 1
            strError += Context.Request.QueryString.Keys(i) + ":" +
ControlChars.Tab + ControlChars.Tab + Context.Request.QueryString(i) +
ControlChars.Lf
        Next i
        strError += ControlChars.Lf + "Post Data:" + ControlChars.Lf + "---
-------" + ControlChars.Lf

        ' Gathering Post Data information

        For i = 0 To Context.Request.Form.Count - 1
            strError += Context.Request.Form.Keys(i) + ":" +
ControlChars.Tab + ControlChars.Tab + Context.Request.Form(i) +
ControlChars.Lf
        Next i
        strError += ControlChars.Lf

        If User.Identity.IsAuthenticated Then
            strError += "User:" + ControlChars.Tab + ControlChars.Tab +
User.Identity.Name + ControlChars.Lf + ControlChars.Lf
        End If
        strError += "Exception Stack Trace:" + ControlChars.Lf + "---------
-------------" + ControlChars.Lf + Server.GetLastError().StackTrace +
ControlChars.Lf + ControlChars.Lf + "Server Variables:" + ControlChars.Lf
+ "-----------------" + ControlChars.Lf

        ' Gathering Server Variables information
        For i = 0 To Context.Request.ServerVariables.Count - 1
            strError += Context.Request.ServerVariables.Keys(i) + ":" +
ControlChars.Tab + ControlChars.Tab + Context.Request.ServerVariables(i) +
ControlChars.Lf
        Next i
        strError += ControlChars.Lf

        ' Sending error message to administration via e-mail
        System.Web.Mail.SmtpMail.Send
("Error@h...", "johnhamman@C...", "error has occured",
strError)
    End Sub

    Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
        ' Fires when the session ends
    End Sub

    Sub Application_End(ByVal sender As Object, ByVal e As EventArgs)
        ' Fires when the application ends
    End Sub

    Public Function checkURl(ByVal url As String, ByVal ServerDir As
String)
        If Not (url Is Nothing) Then
            If (url.IndexOf(ServerDir) > -1) Then
                Return url
            Else
                Return ServerDir
            End If
        Else
            Return ServerDir
        End If
    End Function
 Dim cookyexpires As String = ""

    Public Function SetCookie(ByVal cookiename As String, ByVal
cookievalue As String, ByVal iDaysToExpire As Integer) As Boolean
        Try
            Dim objCookie As New HttpCookie(cookiename)
            Response.Cookies.Clear()
            Response.Cookies.Add(objCookie)
            objCookie.Values.Add(cookiename, cookievalue)
            Dim dtExpiry As DateTime = DateTime.Now.AddDays(iDaysToExpire)
            Response.Cookies(cookiename).Expires = dtExpiry
        Catch e As Exception
            Return False
        End Try
        Return True
    End Function 'SetCookie

    Public Function GetCookie(ByVal cookiename As String) As String
        Dim cookyval As String = ""
        Try
            cookyval = Request.Cookies(cookiename).Value
        Catch e As Exception
            cookyval = ""
        End Try
        Return cookyval
    End Function 'GetCookie


End Class


---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.363 / Virus Database: 201 - Release Date: 5/21/2002

Message #4 by Feduke Cntr Charles R <FedukeCR@m...> on Fri, 24 May 2002 11:33:28 -0400
John,

	My next suggestion is that there is a logic problem in your code.
Remove the "Try ... Catch" block from GetCookie and see if any exception
fires.  Also, don't compare GetCookie("UID") to Nothing; you use Nothing to
catch null references (as you do in C#).  So if a variable was never
initialized then its Nothing (null); in your case you seem to be returning a
String.Empty (or "").  So try this:

If (GetCookie("UID").Equals(String.Empty))
	' always use an object's .Equals() method
	' when its available (esp. since VB doesn't
	' offer operator overloading)
	...
Else
	...
End If

	And by removing the Try ... Catch you should be able to determine
the error.  Ultimately I'd make GetCookie look something like this:

Public Function GetCookie(ByVal cookieName As String) As String
	Dim cookieVal As String = String.Emtpy ' forces default
	
	' if the cookie is there, set the value
	If Not (Request.Cookies(cookieName) Is Nothing) Then
		cookieVal = Request.Cookies(cookieName).Value

	' returning either the value or String.Empty, no
	' "Is Nothing" checks should be required, just
	' .Length > 0 checks now.
      Return cookieVal
End Function 'GetCookie

HTH,
- Chuck

-----Original Message-----
From: John Hamman {Hamman Interactive} [mailto:johnhamman@C...]
Sent: Friday, May 24, 2002 11:08 AM
To: ASP+
Subject: [aspx] RE: cookie problem?


Thank you so much, I have had this code posted all over the place but have
yet to find the problem. Unfortunatly it still didnt work. when you go to
the page it still writes a new cookie for the new sesion.

john

-----Original Message-----
From: Feduke Cntr Charles R [mailto:FedukeCR@m...]
Sent: Friday, May 24, 2002 8:36 AM
To: ASP+
Subject: [aspx] RE: cookie problem?


John,

	My guess is the Replace("-", Nothing) is your culprit.  Nothing
should be the equivalent of null, and when you append/insert a null value
into a string it (from our perspective) blanks the string.  Try one of the
following:

	Replace("-", String.Empty)
	-OR-
	Replace("-", "")

	Please let us know if this works.

HTH,
- Chuck

-----Original Message-----
From: john hamman [mailto:johnhamman@c...]
Sent: Thursday, May 23, 2002 10:31 PM
To: ASP+
Subject: [aspx] cookie problem?


HELP,
I have a problem here, When a user comes to the site I check to see if
they have cookie("UID") and if they dont I write a new one with the GUID
as the value and redirect them with the Cookies Value appended to the end
of the URL. BUT for some reason It doesnt write the cookies or something
because it seems to make a new GUID every time they visit. Below is my
code. Can anyone see whats going on? You can visit the site that this
works on at http://www.americangolfclassics.com.
Thanks
john

'Global.asax.vb
Imports System
Imports System.IO
Imports System.Web
Imports hi.redir
imports Microsoft.VisualBasic
imports System.Diagnostics

Public Class Global
    Inherits System.Web.HttpApplication

#Region " Component Designer Generated Code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Component Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Required by the Component Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Component Designer
    'It can be modified using the Component Designer.
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
        components = New System.ComponentModel.Container()
    End Sub

#End Region

    Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
        ' Fires when the application is started
    End Sub

    Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
        Session.Timeout = 15
        Dim host As String = Request.ServerVariables("SERVER_NAME").ToLower
        Dim ServerDir As String = ""
        Dim PageURL As String = Request.ServerVariables("URL")

        'Dim obj As redir.Go_to
        'Call obj.newhost(host)
          Dim uGUID As String = ""

        If (GetCookie("UID") Is Nothing) Then
            Dim cookie As New HttpCookie("UID")
            uGUID = System.Guid.NewGuid().ToString.Replace("-", Nothing)
            SetCookie("UID", uGUID, 30)
        Else
            uGUID = Nothing
            uGUID = GetCookie("UID")
        End If

        Select Case host
            Case "RemovedforThisemail.com"
                ServerDir = checkURl(PageURL, "/RemovedforThisemail")
            Case "RemovedforThisemail.com"
                ServerDir = checkURl(PageURL, "RemovedforThisemail")
            Case "RemovedforThisemail.com"
                ServerDir = checkURl(PageURL, "/RemovedforThisemail")
            Case "www.RemovedforThisemail.com"
                ServerDir = checkURl(PageURL, "/RemovedforThisemail")
            Case "RemovedforThisemail.com"
                ServerDir = checkURl(PageURL, "/RemovedforThisemail")
            Case "www.americangolfclassics.com"
                ServerDir = checkURl(PageURL, "/c_AmGolf/Home/" + uGUID
+ ".hi")
            Case "americangolfclassics.com"
                ServerDir = checkURl(PageURL, "/c_AmGolf/Home/" + uGUID
+ ".hi")

            Case "test.RemovedforThisemail.com"
                ServerDir = checkURl(PageURL, "/c_test/Home/" + uGUID
+ ".hi")
            Case "RemovedforThisemail.org"
                ServerDir = checkURl(PageURL, "/RemovedforThisemail")
            Case "www.RemovedforThisemail.org"
                ServerDir = checkURl(PageURL, "/RemovedforThisemail")
            Case "RemovedforThisemail.com"
                ServerDir = checkURl(PageURL, "/RemovedforThisemail")
            Case "www.RemovedforThisemail.com"
                ServerDir = checkURl(PageURL, "/RemovedforThisemail")
            Case "RemovedforThisemail.com"
                ServerDir = checkURl(PageURL, "/RemovedforThisemail")
            Case "www.RemovedforThisemail.com"
                ServerDir = checkURl(PageURL, "/RemovedforThisemail")
            Case "RemovedforThisemaile.com"
                ServerDir = checkURl(PageURL, "/RemovedforThisemail")
            Case Else
                ServerDir = checkURl(PageURL, "/iRemovedforThisemail")
        End Select

        Response.Redirect(ServerDir)
    End Sub

    Sub Application_BeginRequest(ByVal sender As Object, ByVal e As
EventArgs)
        ' Fires at the beginning of each request
    End Sub

    Sub Application_AuthenticateRequest(ByVal sender As Object, ByVal e As
EventArgs)
        ' Fires upon attempting to authenticate the use
    End Sub

    Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)

        Dim strError As String = "Error in: " + Request.Path +
ControlChars.Lf + "Url: " + Request.RawUrl + ControlChars.Lf +
ControlChars.Lf

        ' Get the exception object for the last error message that occured.
        Dim ErrorInfo As Exception = Server.GetLastError().GetBaseException
()
        strError += "Error Message: " + ErrorInfo.Message +
ControlChars.Lf + "Error Source: " + ErrorInfo.Source + ControlChars.Lf
+ "Target Site: " + ErrorInfo.TargetSite.ToString() + ControlChars.Lf
+ "QueryString Data:" + ControlChars.Lf + "-----------------" +
ControlChars.Lf

        'strError += "Error Target Site: " + ErrorInfo.TargetSite +
ControlChars.Lf

        ' Gathering QueryString information
        Dim i As Integer
        For i = 0 To Context.Request.QueryString.Count - 1
            strError += Context.Request.QueryString.Keys(i) + ":" +
ControlChars.Tab + ControlChars.Tab + Context.Request.QueryString(i) +
ControlChars.Lf
        Next i
        strError += ControlChars.Lf + "Post Data:" + ControlChars.Lf + "---
-------" + ControlChars.Lf

        ' Gathering Post Data information

        For i = 0 To Context.Request.Form.Count - 1
            strError += Context.Request.Form.Keys(i) + ":" +
ControlChars.Tab + ControlChars.Tab + Context.Request.Form(i) +
ControlChars.Lf
        Next i
        strError += ControlChars.Lf

        If User.Identity.IsAuthenticated Then
            strError += "User:" + ControlChars.Tab + ControlChars.Tab +
User.Identity.Name + ControlChars.Lf + ControlChars.Lf
        End If
        strError += "Exception Stack Trace:" + ControlChars.Lf + "---------
-------------" + ControlChars.Lf + Server.GetLastError().StackTrace +
ControlChars.Lf + ControlChars.Lf + "Server Variables:" + ControlChars.Lf
+ "-----------------" + ControlChars.Lf

        ' Gathering Server Variables information
        For i = 0 To Context.Request.ServerVariables.Count - 1
            strError += Context.Request.ServerVariables.Keys(i) + ":" +
ControlChars.Tab + ControlChars.Tab + Context.Request.ServerVariables(i) +
ControlChars.Lf
        Next i
        strError += ControlChars.Lf

        ' Sending error message to administration via e-mail
        System.Web.Mail.SmtpMail.Send
("Error@h...", "johnhamman@C...", "error has occured",
strError)
    End Sub

    Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
        ' Fires when the session ends
    End Sub

    Sub Application_End(ByVal sender As Object, ByVal e As EventArgs)
        ' Fires when the application ends
    End Sub

    Public Function checkURl(ByVal url As String, ByVal ServerDir As
String)
        If Not (url Is Nothing) Then
            If (url.IndexOf(ServerDir) > -1) Then
                Return url
            Else
                Return ServerDir
            End If
        Else
            Return ServerDir
        End If
    End Function
 Dim cookyexpires As String = ""

    Public Function SetCookie(ByVal cookiename As String, ByVal
cookievalue As String, ByVal iDaysToExpire As Integer) As Boolean
        Try
            Dim objCookie As New HttpCookie(cookiename)
            Response.Cookies.Clear()
            Response.Cookies.Add(objCookie)
            objCookie.Values.Add(cookiename, cookievalue)
            Dim dtExpiry As DateTime = DateTime.Now.AddDays(iDaysToExpire)
            Response.Cookies(cookiename).Expires = dtExpiry
        Catch e As Exception
            Return False
        End Try
        Return True
    End Function 'SetCookie

    Public Function GetCookie(ByVal cookiename As String) As String
        Dim cookyval As String = ""
        Try
            cookyval = Request.Cookies(cookiename).Value
        Catch e As Exception
            cookyval = ""
        End Try
        Return cookyval
    End Function 'GetCookie


End Class


---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.363 / Virus Database: 201 - Release Date: 5/21/2002


Message #5 by "John Hamman {Hamman Interactive}" <johnhamman@C...> on Fri, 24 May 2002 13:11:31 -0400
Chuck,
thanks so much for your help. its looking like I may have to rewrite it from
scratch. i cant seem to get it to work nor get it to fire an error.
I will post my code in a while.
john

-----Original Message-----
From: Feduke Cntr Charles R [mailto:FedukeCR@m...]
Sent: Friday, May 24, 2002 11:33 AM
To: ASP+
Subject: [aspx] RE: cookie problem?


John,

	My next suggestion is that there is a logic problem in your code.
Remove the "Try ... Catch" block from GetCookie and see if any exception
fires.  Also, don't compare GetCookie("UID") to Nothing; you use Nothing to
catch null references (as you do in C#).  So if a variable was never
initialized then its Nothing (null); in your case you seem to be returning a
String.Empty (or "").  So try this:

If (GetCookie("UID").Equals(String.Empty))
	' always use an object's .Equals() method
	' when its available (esp. since VB doesn't
	' offer operator overloading)
	...
Else
	...
End If

	And by removing the Try ... Catch you should be able to determine
the error.  Ultimately I'd make GetCookie look something like this:

Public Function GetCookie(ByVal cookieName As String) As String
	Dim cookieVal As String = String.Emtpy ' forces default

	' if the cookie is there, set the value
	If Not (Request.Cookies(cookieName) Is Nothing) Then
		cookieVal = Request.Cookies(cookieName).Value

	' returning either the value or String.Empty, no
	' "Is Nothing" checks should be required, just
	' .Length > 0 checks now.
      Return cookieVal
End Function 'GetCookie

HTH,
- Chuck

-----Original Message-----
From: John Hamman {Hamman Interactive} [mailto:johnhamman@C...]
Sent: Friday, May 24, 2002 11:08 AM
To: ASP+
Subject: [aspx] RE: cookie problem?


Thank you so much, I have had this code posted all over the place but have
yet to find the problem. Unfortunatly it still didnt work. when you go to
the page it still writes a new cookie for the new sesion.

john

-----Original Message-----
From: Feduke Cntr Charles R [mailto:FedukeCR@m...]
Sent: Friday, May 24, 2002 8:36 AM
To: ASP+
Subject: [aspx] RE: cookie problem?


John,

	My guess is the Replace("-", Nothing) is your culprit.  Nothing
should be the equivalent of null, and when you append/insert a null value
into a string it (from our perspective) blanks the string.  Try one of the
following:

	Replace("-", String.Empty)
	-OR-
	Replace("-", "")

	Please let us know if this works.

HTH,
- Chuck

-----Original Message-----
From: john hamman [mailto:johnhamman@c...]
Sent: Thursday, May 23, 2002 10:31 PM
To: ASP+
Subject: [aspx] cookie problem?


HELP,
I have a problem here, When a user comes to the site I check to see if
they have cookie("UID") and if they dont I write a new one with the GUID
as the value and redirect them with the Cookies Value appended to the end
of the URL. BUT for some reason It doesnt write the cookies or something
because it seems to make a new GUID every time they visit. Below is my
code. Can anyone see whats going on? You can visit the site that this
works on at http://www.americangolfclassics.com.
Thanks
john

'Global.asax.vb
Imports System
Imports System.IO
Imports System.Web
Imports hi.redir
imports Microsoft.VisualBasic
imports System.Diagnostics

Public Class Global
    Inherits System.Web.HttpApplication

#Region " Component Designer Generated Code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Component Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Required by the Component Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Component Designer
    'It can be modified using the Component Designer.
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
        components = New System.ComponentModel.Container()
    End Sub

#End Region

    Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
        ' Fires when the application is started
    End Sub

    Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
        Session.Timeout = 15
        Dim host As String = Request.ServerVariables("SERVER_NAME").ToLower
        Dim ServerDir As String = ""
        Dim PageURL As String = Request.ServerVariables("URL")

        'Dim obj As redir.Go_to
        'Call obj.newhost(host)
          Dim uGUID As String = ""

        If (GetCookie("UID") Is Nothing) Then
            Dim cookie As New HttpCookie("UID")
            uGUID = System.Guid.NewGuid().ToString.Replace("-", Nothing)
            SetCookie("UID", uGUID, 30)
        Else
            uGUID = Nothing
            uGUID = GetCookie("UID")
        End If

        Select Case host
            Case "RemovedforThisemail.com"
                ServerDir = checkURl(PageURL, "/RemovedforThisemail")
            Case "RemovedforThisemail.com"
                ServerDir = checkURl(PageURL, "RemovedforThisemail")
            Case "RemovedforThisemail.com"
                ServerDir = checkURl(PageURL, "/RemovedforThisemail")
            Case "www.RemovedforThisemail.com"
                ServerDir = checkURl(PageURL, "/RemovedforThisemail")
            Case "RemovedforThisemail.com"
                ServerDir = checkURl(PageURL, "/RemovedforThisemail")
            Case "www.americangolfclassics.com"
                ServerDir = checkURl(PageURL, "/c_AmGolf/Home/" + uGUID
+ ".hi")
            Case "americangolfclassics.com"
                ServerDir = checkURl(PageURL, "/c_AmGolf/Home/" + uGUID
+ ".hi")

            Case "test.RemovedforThisemail.com"
                ServerDir = checkURl(PageURL, "/c_test/Home/" + uGUID
+ ".hi")
            Case "RemovedforThisemail.org"
                ServerDir = checkURl(PageURL, "/RemovedforThisemail")
            Case "www.RemovedforThisemail.org"
                ServerDir = checkURl(PageURL, "/RemovedforThisemail")
            Case "RemovedforThisemail.com"
                ServerDir = checkURl(PageURL, "/RemovedforThisemail")
            Case "www.RemovedforThisemail.com"
                ServerDir = checkURl(PageURL, "/RemovedforThisemail")
            Case "RemovedforThisemail.com"
                ServerDir = checkURl(PageURL, "/RemovedforThisemail")
            Case "www.RemovedforThisemail.com"
                ServerDir = checkURl(PageURL, "/RemovedforThisemail")
            Case "RemovedforThisemaile.com"
                ServerDir = checkURl(PageURL, "/RemovedforThisemail")
            Case Else
                ServerDir = checkURl(PageURL, "/iRemovedforThisemail")
        End Select

        Response.Redirect(ServerDir)
    End Sub

    Sub Application_BeginRequest(ByVal sender As Object, ByVal e As
EventArgs)
        ' Fires at the beginning of each request
    End Sub

    Sub Application_AuthenticateRequest(ByVal sender As Object, ByVal e As
EventArgs)
        ' Fires upon attempting to authenticate the use
    End Sub

    Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)

        Dim strError As String = "Error in: " + Request.Path +
ControlChars.Lf + "Url: " + Request.RawUrl + ControlChars.Lf +
ControlChars.Lf

        ' Get the exception object for the last error message that occured.
        Dim ErrorInfo As Exception = Server.GetLastError().GetBaseException
()
        strError += "Error Message: " + ErrorInfo.Message +
ControlChars.Lf + "Error Source: " + ErrorInfo.Source + ControlChars.Lf
+ "Target Site: " + ErrorInfo.TargetSite.ToString() + ControlChars.Lf
+ "QueryString Data:" + ControlChars.Lf + "-----------------" +
ControlChars.Lf

        'strError += "Error Target Site: " + ErrorInfo.TargetSite +
ControlChars.Lf

        ' Gathering QueryString information
        Dim i As Integer
        For i = 0 To Context.Request.QueryString.Count - 1
            strError += Context.Request.QueryString.Keys(i) + ":" +
ControlChars.Tab + ControlChars.Tab + Context.Request.QueryString(i) +
ControlChars.Lf
        Next i
        strError += ControlChars.Lf + "Post Data:" + ControlChars.Lf + "---
-------" + ControlChars.Lf

        ' Gathering Post Data information

        For i = 0 To Context.Request.Form.Count - 1
            strError += Context.Request.Form.Keys(i) + ":" +
ControlChars.Tab + ControlChars.Tab + Context.Request.Form(i) +
ControlChars.Lf
        Next i
        strError += ControlChars.Lf

        If User.Identity.IsAuthenticated Then
            strError += "User:" + ControlChars.Tab + ControlChars.Tab +
User.Identity.Name + ControlChars.Lf + ControlChars.Lf
        End If
        strError += "Exception Stack Trace:" + ControlChars.Lf + "---------
-------------" + ControlChars.Lf + Server.GetLastError().StackTrace +
ControlChars.Lf + ControlChars.Lf + "Server Variables:" + ControlChars.Lf
+ "-----------------" + ControlChars.Lf

        ' Gathering Server Variables information
        For i = 0 To Context.Request.ServerVariables.Count - 1
            strError += Context.Request.ServerVariables.Keys(i) + ":" +
ControlChars.Tab + ControlChars.Tab + Context.Request.ServerVariables(i) +
ControlChars.Lf
        Next i
        strError += ControlChars.Lf

        ' Sending error message to administration via e-mail
        System.Web.Mail.SmtpMail.Send
("Error@h...", "johnhamman@C...", "error has occured",
strError)
    End Sub

    Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
        ' Fires when the session ends
    End Sub

    Sub Application_End(ByVal sender As Object, ByVal e As EventArgs)
        ' Fires when the application ends
    End Sub

    Public Function checkURl(ByVal url As String, ByVal ServerDir As
String)
        If Not (url Is Nothing) Then
            If (url.IndexOf(ServerDir) > -1) Then
                Return url
            Else
                Return ServerDir
            End If
        Else
            Return ServerDir
        End If
    End Function
 Dim cookyexpires As String = ""

    Public Function SetCookie(ByVal cookiename As String, ByVal
cookievalue As String, ByVal iDaysToExpire As Integer) As Boolean
        Try
            Dim objCookie As New HttpCookie(cookiename)
            Response.Cookies.Clear()
            Response.Cookies.Add(objCookie)
            objCookie.Values.Add(cookiename, cookievalue)
            Dim dtExpiry As DateTime = DateTime.Now.AddDays(iDaysToExpire)
            Response.Cookies(cookiename).Expires = dtExpiry
        Catch e As Exception
            Return False
        End Try
        Return True
    End Function 'SetCookie

    Public Function GetCookie(ByVal cookiename As String) As String
        Dim cookyval As String = ""
        Try
            cookyval = Request.Cookies(cookiename).Value
        Catch e As Exception
            cookyval = ""
        End Try
        Return cookyval
    End Function 'GetCookie


End Class


---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.363 / Virus Database: 201 - Release Date: 5/21/2002



---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.363 / Virus Database: 201 - Release Date: 5/21/2002

Message #6 by "John Hamman {Hamman Interactive}" <johnhamman@C...> on Fri, 24 May 2002 14:43:09 -0400
I finaly found it, In the root folder i have a default page set up to
redirect and it has the same code. Even thought we changed it in the
global.asax page it didnt change cause of the default. It works now. Thanks
so much.
God Bless
john hamman


-----Original Message-----
From: Feduke Cntr Charles R [mailto:FedukeCR@m...]
Sent: Friday, May 24, 2002 11:33 AM
To: ASP+
Subject: [aspx] RE: cookie problem?


John,

	My next suggestion is that there is a logic problem in your code.
Remove the "Try ... Catch" block from GetCookie and see if any exception
fires.  Also, don't compare GetCookie("UID") to Nothing; you use Nothing to
catch null references (as you do in C#).  So if a variable was never
initialized then its Nothing (null); in your case you seem to be returning a
String.Empty (or "").  So try this:

If (GetCookie("UID").Equals(String.Empty))
	' always use an object's .Equals() method
	' when its available (esp. since VB doesn't
	' offer operator overloading)
	...
Else
	...
End If

	And by removing the Try ... Catch you should be able to determine
the error.  Ultimately I'd make GetCookie look something like this:

Public Function GetCookie(ByVal cookieName As String) As String
	Dim cookieVal As String = String.Emtpy ' forces default

	' if the cookie is there, set the value
	If Not (Request.Cookies(cookieName) Is Nothing) Then
		cookieVal = Request.Cookies(cookieName).Value

	' returning either the value or String.Empty, no
	' "Is Nothing" checks should be required, just
	' .Length > 0 checks now.
      Return cookieVal
End Function 'GetCookie

HTH,
- Chuck

-----Original Message-----
From: John Hamman {Hamman Interactive} [mailto:johnhamman@C...]
Sent: Friday, May 24, 2002 11:08 AM
To: ASP+
Subject: [aspx] RE: cookie problem?


Thank you so much, I have had this code posted all over the place but have
yet to find the problem. Unfortunatly it still didnt work. when you go to
the page it still writes a new cookie for the new sesion.

john

-----Original Message-----
From: Feduke Cntr Charles R [mailto:FedukeCR@m...]
Sent: Friday, May 24, 2002 8:36 AM
To: ASP+
Subject: [aspx] RE: cookie problem?


John,

	My guess is the Replace("-", Nothing) is your culprit.  Nothing
should be the equivalent of null, and when you append/insert a null value
into a string it (from our perspective) blanks the string.  Try one of the
following:

	Replace("-", String.Empty)
	-OR-
	Replace("-", "")

	Please let us know if this works.

HTH,
- Chuck

-----Original Message-----
From: john hamman [mailto:johnhamman@c...]
Sent: Thursday, May 23, 2002 10:31 PM
To: ASP+
Subject: [aspx] cookie problem?


HELP,
I have a problem here, When a user comes to the site I check to see if
they have cookie("UID") and if they dont I write a new one with the GUID
as the value and redirect them with the Cookies Value appended to the end
of the URL. BUT for some reason It doesnt write the cookies or something
because it seems to make a new GUID every time they visit. Below is my
code. Can anyone see whats going on? You can visit the site that this
works on at http://www.americangolfclassics.com.
Thanks
john

'Global.asax.vb
Imports System
Imports System.IO
Imports System.Web
Imports hi.redir
imports Microsoft.VisualBasic
imports System.Diagnostics

Public Class Global
    Inherits System.Web.HttpApplication

#Region " Component Designer Generated Code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Component Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Required by the Component Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Component Designer
    'It can be modified using the Component Designer.
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
        components = New System.ComponentModel.Container()
    End Sub

#End Region

    Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
        ' Fires when the application is started
    End Sub

    Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
        Session.Timeout = 15
        Dim host As String = Request.ServerVariables("SERVER_NAME").ToLower
        Dim ServerDir As String = ""
        Dim PageURL As String = Request.ServerVariables("URL")

        'Dim obj As redir.Go_to
        'Call obj.newhost(host)
          Dim uGUID As String = ""

        If (GetCookie("UID") Is Nothing) Then
            Dim cookie As New HttpCookie("UID")
            uGUID = System.Guid.NewGuid().ToString.Replace("-", Nothing)
            SetCookie("UID", uGUID, 30)
        Else
            uGUID = Nothing
            uGUID = GetCookie("UID")
        End If

        Select Case host
            Case "RemovedforThisemail.com"
                ServerDir = checkURl(PageURL, "/RemovedforThisemail")
            Case "RemovedforThisemail.com"
                ServerDir = checkURl(PageURL, "RemovedforThisemail")
            Case "RemovedforThisemail.com"
                ServerDir = checkURl(PageURL, "/RemovedforThisemail")
            Case "www.RemovedforThisemail.com"
                ServerDir = checkURl(PageURL, "/RemovedforThisemail")
            Case "RemovedforThisemail.com"
                ServerDir = checkURl(PageURL, "/RemovedforThisemail")
            Case "www.americangolfclassics.com"
                ServerDir = checkURl(PageURL, "/c_AmGolf/Home/" + uGUID
+ ".hi")
            Case "americangolfclassics.com"
                ServerDir = checkURl(PageURL, "/c_AmGolf/Home/" + uGUID
+ ".hi")

            Case "test.RemovedforThisemail.com"
                ServerDir = checkURl(PageURL, "/c_test/Home/" + uGUID
+ ".hi")
            Case "RemovedforThisemail.org"
                ServerDir = checkURl(PageURL, "/RemovedforThisemail")
            Case "www.RemovedforThisemail.org"
                ServerDir = checkURl(PageURL, "/RemovedforThisemail")
            Case "RemovedforThisemail.com"
                ServerDir = checkURl(PageURL, "/RemovedforThisemail")
            Case "www.RemovedforThisemail.com"
                ServerDir = checkURl(PageURL, "/RemovedforThisemail")
            Case "RemovedforThisemail.com"
                ServerDir = checkURl(PageURL, "/RemovedforThisemail")
            Case "www.RemovedforThisemail.com"
                ServerDir = checkURl(PageURL, "/RemovedforThisemail")
            Case "RemovedforThisemaile.com"
                ServerDir = checkURl(PageURL, "/RemovedforThisemail")
            Case Else
                ServerDir = checkURl(PageURL, "/iRemovedforThisemail")
        End Select

        Response.Redirect(ServerDir)
    End Sub

    Sub Application_BeginRequest(ByVal sender As Object, ByVal e As
EventArgs)
        ' Fires at the beginning of each request
    End Sub

    Sub Application_AuthenticateRequest(ByVal sender As Object, ByVal e As
EventArgs)
        ' Fires upon attempting to authenticate the use
    End Sub

    Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)

        Dim strError As String = "Error in: " + Request.Path +
ControlChars.Lf + "Url: " + Request.RawUrl + ControlChars.Lf +
ControlChars.Lf

        ' Get the exception object for the last error message that occured.
        Dim ErrorInfo As Exception = Server.GetLastError().GetBaseException
()
        strError += "Error Message: " + ErrorInfo.Message +
ControlChars.Lf + "Error Source: " + ErrorInfo.Source + ControlChars.Lf
+ "Target Site: " + ErrorInfo.TargetSite.ToString() + ControlChars.Lf
+ "QueryString Data:" + ControlChars.Lf + "-----------------" +
ControlChars.Lf

        'strError += "Error Target Site: " + ErrorInfo.TargetSite +
ControlChars.Lf

        ' Gathering QueryString information
        Dim i As Integer
        For i = 0 To Context.Request.QueryString.Count - 1
            strError += Context.Request.QueryString.Keys(i) + ":" +
ControlChars.Tab + ControlChars.Tab + Context.Request.QueryString(i) +
ControlChars.Lf
        Next i
        strError += ControlChars.Lf + "Post Data:" + ControlChars.Lf + "---
-------" + ControlChars.Lf

        ' Gathering Post Data information

        For i = 0 To Context.Request.Form.Count - 1
            strError += Context.Request.Form.Keys(i) + ":" +
ControlChars.Tab + ControlChars.Tab + Context.Request.Form(i) +
ControlChars.Lf
        Next i
        strError += ControlChars.Lf

        If User.Identity.IsAuthenticated Then
            strError += "User:" + ControlChars.Tab + ControlChars.Tab +
User.Identity.Name + ControlChars.Lf + ControlChars.Lf
        End If
        strError += "Exception Stack Trace:" + ControlChars.Lf + "---------
-------------" + ControlChars.Lf + Server.GetLastError().StackTrace +
ControlChars.Lf + ControlChars.Lf + "Server Variables:" + ControlChars.Lf
+ "-----------------" + ControlChars.Lf

        ' Gathering Server Variables information
        For i = 0 To Context.Request.ServerVariables.Count - 1
            strError += Context.Request.ServerVariables.Keys(i) + ":" +
ControlChars.Tab + ControlChars.Tab + Context.Request.ServerVariables(i) +
ControlChars.Lf
        Next i
        strError += ControlChars.Lf

        ' Sending error message to administration via e-mail
        System.Web.Mail.SmtpMail.Send
("Error@h...", "johnhamman@C...", "error has occured",
strError)
    End Sub

    Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
        ' Fires when the session ends
    End Sub

    Sub Application_End(ByVal sender As Object, ByVal e As EventArgs)
        ' Fires when the application ends
    End Sub

    Public Function checkURl(ByVal url As String, ByVal ServerDir As
String)
        If Not (url Is Nothing) Then
            If (url.IndexOf(ServerDir) > -1) Then
                Return url
            Else
                Return ServerDir
            End If
        Else
            Return ServerDir
        End If
    End Function
 Dim cookyexpires As String = ""

    Public Function SetCookie(ByVal cookiename As String, ByVal
cookievalue As String, ByVal iDaysToExpire As Integer) As Boolean
        Try
            Dim objCookie As New HttpCookie(cookiename)
            Response.Cookies.Clear()
            Response.Cookies.Add(objCookie)
            objCookie.Values.Add(cookiename, cookievalue)
            Dim dtExpiry As DateTime = DateTime.Now.AddDays(iDaysToExpire)
            Response.Cookies(cookiename).Expires = dtExpiry
        Catch e As Exception
            Return False
        End Try
        Return True
    End Function 'SetCookie

    Public Function GetCookie(ByVal cookiename As String) As String
        Dim cookyval As String = ""
        Try
            cookyval = Request.Cookies(cookiename).Value
        Catch e As Exception
            cookyval = ""
        End Try
        Return cookyval
    End Function 'GetCookie


End Class


---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.363 / Virus Database: 201 - Release Date: 5/21/2002



---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.363 / Virus Database: 201 - Release Date: 5/21/2002

Message #7 by "Mingkun Goh" <mangokun@h...> on Sun, 26 May 2002 05:01:08
Replace your SetCookie/GetCookie functions with the functions below which 
I modified from yours:

    Public Function SetCookie(ByVal cookiename As String, ByVal
cookievalue As String, ByVal iDaysToExpire As Integer) As Boolean
        Try
            Dim objCookie As New HttpCookie(cookiename)
            objCookie.Values.Add(cookiename, cookievalue)
            objCookie.Expires = Now.AddDays(iDaysToExpire)
            Response.Cookies.Add(objCookie)
        Catch e As Exception
            Return False
        End Try
        Return True
    End Function 'SetCookie

    Public Function GetCookie(ByVal cookiename As String) As String
        'the returned value will be Nothing by default, unless it is being 
assigned a value
        Try
            Dim objCookieAs HttpCookie = Request.Cookies(cookiename)
            If Not IsNothing(objCookie) Then
                Return objCookie.Values(cookiename)
            End If
        Catch e As Exception
        End Try
    End Function 'GetCookie


As for your if-else statements above, you can remove the lines with REMOVE 
in front:

        If (GetCookie("UID") Is Nothing) Then
   REMOVE   Dim cookie As New HttpCookie("UID")
            uGUID = System.Guid.NewGuid().ToString.Replace("-", Nothing)
            SetCookie("UID", uGUID, 30)
        Else
   REMOVE   uGUID = Nothing
            uGUID = GetCookie("UID")
        End If


  Return to Index