Wrox Home  
Search P2P Archive for: Go

  Return to Index  

aspx_beginners thread: my cookies are not sticking!


Message #1 by "John Hamman {Hamman Interactive}" <johnhamman@C...> on Thu, 23 May 2002 08:22:49 -0400
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


  Return to Index