
May 23rd, 2002, 08:23 PM
|
|
Contributing User
|
|
Join Date: Dec 2002
Posts: 14,575
  
Time spent in forums: < 1 sec
Reputation Power: 22
|
|
|
Cookie problem and page start!
<i><b>Originally posted by : john (johnhamman@cox.net)</b></i><br /><br />HELP,<br />I have a problem here, When a user comes to the site I check to see if they<br />have cookie("UID") and if they dont I write a new one with the GUID as the<br />value and redirect them with the Cookies Value appended to the end of the<br />URL. BUT for some reason It doesnt write the cookies or something because it<br />seems to make a new GUID every time they visit. Below is my code. Can anyone<br />see whats going on? You can visit the site that this works on at<br />http://www.americangolfclassics.com.<br />Thanks<br />john<br /><br />'Global.asax.vb<br />Imports System<br />Imports System.IO<br />Imports System.Web<br />Imports hi.redir<br />imports Microsoft.VisualBasic<br />imports System.Diagnostics<br /><br />Public Class Global<br /> Inherits System.Web.HttpApplication<br /><br />#Region " Component Designer Generated Code "<br /><br /> Public Sub New()<br /> MyBase.New()<br /><br /> 'This call is required by the Component Designer.<br /> InitializeComponent()<br /><br /> 'Add any initialization after the InitializeComponent() call<br /><br /> End Sub<br /><br /> 'Required by the Component Designer<br /> Private components As System.ComponentModel.IContainer<br /><br /> 'NOTE: The following procedure is required by the Component Designer<br /> 'It can be modified using the Component Designer.<br /> 'Do not modify it using the code editor.<br /> <System.Diagnostics.DebuggerStepThrough()> Private Sub<br />InitializeComponent()<br /> components = New System.ComponentModel.Container()<br /> End Sub<br /><br />#End Region<br /><br /> Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)<br /> ' Fires when the application is started<br /> End Sub<br /><br /> Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)<br /> Session.Timeout = 15<br /> Dim host As String = Request.ServerVariables("SERVER_NAME").ToLower<br /> Dim ServerDir As String = ""<br /> Dim PageURL As String = Request.ServerVariables("URL")<br /><br /> 'Dim obj As redir.Go_to<br /> 'Call obj.newhost(host)<br /> Dim uGUID As String = ""<br /><br /> If (GetCookie("UID") Is Nothing) Then<br /> Dim cookie As New HttpCookie("UID")<br /> uGUID = System.Guid.NewGuid().ToString.Replace("-", Nothing)<br /> SetCookie("UID", uGUID, 30)<br /> Else<br /> uGUID = Nothing<br /> uGUID = GetCookie("UID")<br /> End If<br /><br /> Select Case host<br /> Case "RemovedforThisemail.com"<br /> ServerDir = checkURl(PageURL, "/RemovedforThisemail")<br /> Case "RemovedforThisemail.com"<br /> ServerDir = checkURl(PageURL, "RemovedforThisemail")<br /> Case "RemovedforThisemail.com"<br /> ServerDir = checkURl(PageURL, "/RemovedforThisemail")<br /> Case "www.RemovedforThisemail.com"<br /> ServerDir = checkURl(PageURL, "/RemovedforThisemail")<br /> Case "RemovedforThisemail.com"<br /> ServerDir = checkURl(PageURL, "/RemovedforThisemail")<br /> Case "www.americangolfclassics.com"<br /> ServerDir = checkURl(PageURL, "/c_AmGolf/Home/" + uGUID +<br />".hi")<br /> Case "americangolfclassics.com"<br /> ServerDir = checkURl(PageURL, "/c_AmGolf/Home/" + uGUID +<br />".hi")<br /><br /> Case "test.RemovedforThisemail.com"<br /> ServerDir = checkURl(PageURL, "/c_test/Home/" + uGUID +<br />".hi")<br /> Case "RemovedforThisemail.org"<br /> ServerDir = checkURl(PageURL, "/RemovedforThisemail")<br /> Case "www.RemovedforThisemail.org"<br /> ServerDir = checkURl(PageURL, "/RemovedforThisemail")<br /> Case "RemovedforThisemail.com"<br /> ServerDir = checkURl(PageURL, "/RemovedforThisemail")<br /> Case "www.RemovedforThisemail.com"<br /> ServerDir = checkURl(PageURL, "/RemovedforThisemail")<br /> Case "RemovedforThisemail.com"<br /> ServerDir = checkURl(PageURL, "/RemovedforThisemail")<br /> Case "www.RemovedforThisemail.com"<br /> ServerDir = checkURl(PageURL, "/RemovedforThisemail")<br /> Case "RemovedforThisemaile.com"<br /> ServerDir = checkURl(PageURL, "/RemovedforThisemail")<br /> Case Else<br /> ServerDir = checkURl(PageURL, "/iRemovedforThisemail")<br /> End Select<br /><br /> Response.Redirect(ServerDir)<br /> End Sub<br /><br /> Sub Application_BeginRequest(ByVal sender As Object, ByVal e As<br />EventArgs)<br /> ' Fires at the beginning of each request<br /> End Sub<br /><br /> Sub Application_AuthenticateRequest(ByVal sender As Object, ByVal e As<br />EventArgs)<br /> ' Fires upon attempting to authenticate the use<br /> End Sub<br /><br /> Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)<br /><br /> Dim strError As String = "Error in: " + Request.Path +<br />ControlChars.Lf + "Url: " + Request.RawUrl + ControlChars.Lf +<br />ControlChars.Lf<br /><br /> ' Get the exception object for the last error message that occured.<br /> Dim ErrorInfo As Exception =<br />Server.GetLastError().GetBaseException()<br /> strError += "Error Message: " + ErrorInfo.Message + ControlChars.Lf<br />+ "Error Source: " + ErrorInfo.Source + ControlChars.Lf + "Target Site: " +<br />ErrorInfo.TargetSite.ToString() + ControlChars.Lf + "QueryString Data:" +<br />ControlChars.Lf + "-----------------" + ControlChars.Lf<br /><br /> 'strError += "Error Target Site: " + ErrorInfo.TargetSite +<br />ControlChars.Lf<br /><br /> ' Gathering QueryString information<br /> Dim i As Integer<br /> For i = 0 To Context.Request.QueryString.Count - 1<br /> strError += Context.Request.QueryString.Keys(i) + ":" +<br />ControlChars.Tab + ControlChars.Tab + Context.Request.QueryString(i) +<br />ControlChars.Lf<br /> Next i<br /> strError += ControlChars.Lf + "Post Data:" + ControlChars.Lf +<br />"----------" + ControlChars.Lf<br /><br /> ' Gathering Post Data information<br /><br /> For i = 0 To Context.Request.Form.Count - 1<br /> strError += Context.Request.Form.Keys(i) + ":" +<br />ControlChars.Tab + ControlChars.Tab + Context.Request.Form(i) +<br />ControlChars.Lf<br /> Next i<br /> strError += ControlChars.Lf<br /><br /> If User.Identity.IsAuthenticated Then<br /> strError += "User:" + ControlChars.Tab + ControlChars.Tab +<br />User.Identity.Name + ControlChars.Lf + ControlChars.Lf<br /> End If<br /> strError += "Exception Stack Trace:" + ControlChars.Lf +<br />"----------------------" + ControlChars.Lf +<br />Server.GetLastError().StackTrace + ControlChars.Lf + ControlChars.Lf +<br />"Server Variables:" + ControlChars.Lf + "-----------------" +<br />ControlChars.Lf<br /><br /> ' Gathering Server Variables information<br /> For i = 0 To Context.Request.ServerVariables.Count - 1<br /> strError += Context.Request.ServerVariables.Keys(i) + ":" +<br />ControlChars.Tab + ControlChars.Tab + Context.Request.ServerVariables(i) +<br />ControlChars.Lf<br /> Next i<br /> strError += ControlChars.Lf<br /><br /> ' Sending error message to administration via e-mail<br /> System.Web.Mail.SmtpMail.Send("Error@hammaninteractive.com",<br />"johnhamman@Cox.net", "error has occured", strError)<br /> End Sub<br /><br /> Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)<br /> ' Fires when the session ends<br /> End Sub<br /><br /> Sub Application_End(ByVal sender As Object, ByVal e As EventArgs)<br /> ' Fires when the application ends<br /> End Sub<br /><br /> Public Function checkURl(ByVal url As String, ByVal ServerDir As String)<br /> If Not (url Is Nothing) Then<br /> If (url.IndexOf(ServerDir) > -1) Then<br /> Return url<br /> Else<br /> Return ServerDir<br /> End If<br /> Else<br /> Return ServerDir<br /> End If<br /> End Function<br /> Dim cookyexpires As String = ""<br /><br /> Public Function SetCookie(ByVal cookiename As String, ByVal cookievalue<br />As String, ByVal iDaysToExpire As Integer) As Boolean<br /> Try<br /> Dim objCookie As New HttpCookie(cookiename)<br /> Response.Cookies.Clear()<br /> Response.Cookies.Add(objCookie)<br /> objCookie.Values.Add(cookiename, cookievalue)<br /> Dim dtExpiry As DateTime = DateTime.Now.AddDays(iDaysToExpire)<br /> Response.Cookies(cookiename).Expires = dtExpiry<br /> Catch e As Exception<br /> Return False<br /> End Try<br /> Return True<br /> End Function 'SetCookie<br /><br /> Public Function GetCookie(ByVal cookiename As String) As String<br /> Dim cookyval As String = ""<br /> Try<br /> cookyval = Request.Cookies(cookiename).Value<br /> Catch e As Exception<br /> cookyval = ""<br /> End Try<br /> Return cookyval<br /> End Function 'GetCookie<br /><br /><br />End Class<br /><br />
|