Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cookie creation problem

Status
Not open for further replies.

daveok

Programmer
Mar 21, 2001
2
GB
I am unable to create cookies for some reason using ASP and VBScript. My browser preferences are not the problem as every other site (including this one) creates cookies in my C:\Windows\Cookies folder without any problems. It must therefore be something to do with my code...

Response.Buffer=True
blah blah blah...
Response.Cookies("Details")("Email") = strEmail
Response.Cookies("Details")("Mobile") = strMobile
Response.Cookies("Details")("ForeName") = strForeName
Response.Cookies("Details")("LoginID") = strLoginID
Response.Cookies("Details").Expires = Date + 365


No errors are generated. The page displays as normal. All is OK except the cookie creation fails.

I read somewhere else on this forum that the code has to be in between the <HEAD> and </HEAD> tags. I've not seen that anywhere else but even that hasn't helped.

Any advice greatly appreciated...

Dave


 
Here is the code from Microsoft's site --


<SCRIPT LANGUAGE=&quot;VBScript&quot;>
<!--

'****************************************************************
'Dimension and set the NOT_FOUND constant for the entire page
Dim NOT_FOUND
NOT_FOUND = &quot;NOT_FOUND&quot;

'****************************************************************
' Purpose: Creates or modifies the value assigned to a given
' variable.
' Inputs: strVariableName: The name of the variable that will
' have its value set.
' varVariableValue: The value that strVariableName
' should be set to.
' Returns: Nothing
' Notes: This function could be expanded to include other
' cookie attributes, such as expire date and valid
' domains. Cookies set with this implementation expire
' at the end of the user's session. If the cookie
' should remain valid for a longer period of time,
' an expires section can be added to the string. For
' example:
' ... & varVariableName & &quot;;expires=01-Jul-96 GMT&quot;

'****************************************************************

Sub SetVariable(strVariableName, varVariableValue)
Document.Cookie = strVariableName & &quot;=&quot; & varVariableValue
End Sub

'****************************************************************
' Purpose: Delete the variable with the name held in
' strVariableName
' Inputs: strVariableName: The name of the variable to delete
' Returns: Nothing
' Notes: The cookie is deleted by setting the expires attribute
' to a date that has already occurred. If one went back
' time, this method would break, so be sure to take that
' into account.
'****************************************************************

Sub KillVariable(strVariableName)
SetVariable strVariableName, &quot;NULL;expires=Monday, 01-Jan-95 12:00:00 GMT&quot;
End Sub

'****************************************************************
' Purpose: Finds and returns the value of of the variable with
' the name held in strVariableName
' Inputs: strVariableName: The name of the variable to return
' the value of.
' Returns: The value of the variable with the name of
' strVariableName.
' If the variable is not found, returns NOT_FOUND.
' Notes: This function could be greatly simplified if the
' bounds-checking code was removed.
' The NOT_FOUND constant should be set
' once for the entire page.
'****************************************************************

Function ReadVariable(strVariableName)
'these five variables are used in the string manipulation
'code that finds the variable in the cookie.
Dim intLocation
Dim intNameLength
Dim intValueLength
Dim intNextSemicolon
Dim strTemp

'calculate length and location of variable name
intNameLength = Len(strVariableName)
intLocation = Instr(Document.Cookie, strVariableName)

'check for existence of variable name
If intLocation = 0 Then
'variable not found, so it can't be read
ReadVariable = NOT_FOUND
Else
'get a smaller substring to work with
strTemp = Right(Document.Cookie, Len(Document.Cookie) - intLocation + 1)

'check to make sure we found the full string, not just a substring
If Mid(strTemp, intNameLength + 1, 1) <> &quot;=&quot; Then
'oops, only found substring, not good enough
ReadVariable = NOT_FOUND

'note that this will incorrectly give a not found result if and only if
'a search for a variable whose name is a substring of a preceding
'variable is undertaken. For example, this will fail:
'
'search for: MyVar
'cookie contains: MyVariable=2;MyVar=1
Else
'found full string
intNextSemicolon = Instr(strTemp, &quot;;&quot;)

'if not found, then we need the last element of the cookie
If intNextSemicolon = 0 Then intNextSemicolon = Len(strTemp) + 1

'check for empty variable (Var1=;)
If intNextSemicolon = (intNameLength + 2) Then
'variable is empty
ReadVariable = &quot;&quot;
Else
'calculate value normally
intValueLength = intNextSemicolon - intNameLength - 2
ReadVariable = Mid(strTemp, intNameLength + 2, intValueLength)
End If
End If
End if
End Function

' ***********************************************
' Code behind buttons
' ***********************************************

Sub btnSaveVariable_onClick
Dim strVariableName
Dim varVariableValue

strVariableName = InputBox(&quot;Enter variable name&quot;)
varVariableValue = InputBox(&quot;Enter value for '&quot; & strVariableName & &quot;'&quot;)

SetVariable strVariableName, varVariableValue
End Sub

Sub btnReadVariable_onClick
Dim strVariableName
Dim varVariableValue

strVariableName = InputBox(&quot;Enter variable name to read&quot;)
varVariableValue = ReadVariable(strVariableName)

If varVariableValue = NOT_FOUND Then
MsgBox &quot;'&quot; & strVariableName & &quot;' not found.&quot;
Else
MsgBox &quot;'&quot; & strVariableName & &quot;' has a value of '&quot; & varVariableValue & &quot;'.&quot;
End If
End sub

Sub btnShowCookie_onClick
MsgBox Document.Cookie
End Sub

Sub btnNextPage_onClick
Location.HRef = &quot;extcookie2.htm&quot;
End Sub

Sub btnKillVariable_onClick
Dim strVariableName
Dim varVariableValue

strVariableName = InputBox(&quot;Enter variable name to delete&quot;)
varVariableValue = ReadVariable(strVariableName)

If varVariableValue = NOT_FOUND Then
MsgBox &quot;'&quot; & strVariableName & &quot;' not found.&quot;
Else
KillVariable(strVariableName)
MsgBox &quot;Variable deleted.&quot;
End If
End Sub
-->
</SCRIPT>
 
Yes, I suppose that it is... didn't think of that.

Only will work in IE... sorry. I'm guessing that means he is going to have to use javascript to write the cookies, then, yes???

thx Rob --
 
DaveOK,

try generating your cookies before rendering ANY html. I know you have a response.buffer = true in there somewhere bug may some other piece of code flushes the buffer. Yours,

Rob.
 
Thanks Guys. It's working now. It was server side and putting the code before HTML strangely fixed the problem despite the response.buffer setting.

Cheers...

Dave
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top