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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cookies - Setting and Getting

Status
Not open for further replies.

SlowPoker

Programmer
May 19, 1999
2
US
I keep finding allusions to vbscript being able to set and get cookie info in asp pages, but when I try the suggested coding I get zip. Does the code have to come before the <html> tag? Can anyone provide me with a fully coded asp page that sets and retrieves a cookie using vbscript?
 
Try the code below. This is a demo from a MS web site. It can be used on asp or htm web pages. Rgds, Nic.<br><br><br><br>&lt;HTML&gt;<br>&lt;HEAD&gt;<br>&lt;/HEAD&gt;<br><br>&lt;SCRIPT LANGUAGE=&quot;VBScript&quot;&gt;<br>&lt;!--<br><br>&nbsp;'****************************************************************<br>&nbsp;'Dimension and set the NOT_FOUND constant for the entire page <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Dim NOT_FOUND<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NOT_FOUND = &quot;NOT_FOUND&quot;<br><br>&nbsp;'****************************************************************<br>&nbsp;' Purpose: Creates or modifies the value assigned to a given<br>&nbsp;' &nbsp;&nbsp;&nbsp;&nbsp;variable.<br>&nbsp;' Inputs:&nbsp;&nbsp;strVariableName:&nbsp;&nbsp;The name of the variable that will<br>&nbsp;' &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;have its value set.<br>&nbsp;'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;varVariableValue: The value that strVariableName<br>&nbsp;' &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;should be set to.<br>&nbsp;' Returns: Nothing<br>&nbsp;' Notes:&nbsp;&nbsp;&nbsp;This function could be expanded to include other<br>&nbsp;'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cookie attributes, such as expire date and valid<br>&nbsp;'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;domains.&nbsp;&nbsp;Cookies set with this implementation expire<br>&nbsp;'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at the end of the user's session.&nbsp;&nbsp;If the cookie<br>&nbsp;'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;should remain valid for a longer period of time,<br>&nbsp;'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;an expires section can be added to the string.&nbsp;&nbsp;For<br>&nbsp;'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;example:<br>&nbsp;'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;... & varVariableName & &quot;;expires=01-Jul-96 GMT&quot;<br><br>&nbsp;'****************************************************************<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Sub SetVariable(strVariableName, varVariableValue)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Document.Cookie = strVariableName & &quot;=&quot; & varVariableValue<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;End Sub<br><br><br>&nbsp;'****************************************************************<br>&nbsp;' Purpose: Delete the variable with the name held in<br>&nbsp;'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;strVariableName<br>&nbsp;' Inputs:&nbsp;&nbsp;strVariableName:&nbsp;&nbsp;The name of the variable to delete&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;' Returns: Nothing<br>&nbsp;' Notes:&nbsp;&nbsp;&nbsp;The cookie is deleted by setting the expires attribute<br>&nbsp;' &nbsp;&nbsp;&nbsp;&nbsp;to a date that has already occurred.&nbsp;&nbsp;If one went back<br>&nbsp;' &nbsp;&nbsp;&nbsp;&nbsp;time, this method would break, so be sure to take that<br>&nbsp;'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;into account.<br>&nbsp;'****************************************************************<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Sub KillVariable(strVariableName)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SetVariable strVariableName, &quot;NULL;expires=Monday, 01-Jan-95 12:00:00 GMT&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;End Sub<br><br>&nbsp;'****************************************************************<br>&nbsp;' Purpose: Finds and returns the value of of the variable with<br>&nbsp;'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;the name held in strVariableName<br>&nbsp;' Inputs:&nbsp;&nbsp;strVariableName:&nbsp;&nbsp;The name of the variable to return<br>&nbsp;' &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;the value of.<br>&nbsp;' Returns: The value of the variable with the name of<br>&nbsp;'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;strVariableName.<br>&nbsp;' &nbsp;&nbsp;&nbsp;&nbsp;If the variable is not found, returns NOT_FOUND.<br>&nbsp;' Notes:&nbsp;&nbsp;&nbsp;This function could be greatly simplified if the<br>&nbsp;'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bounds-checking code was removed.<br>&nbsp;'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;The NOT_FOUND constant should be set<br>&nbsp;'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;once for the entire page.<br>&nbsp;'****************************************************************<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Function ReadVariable(strVariableName)<br> 'these five variables are used in the string manipulation<br> 'code that finds the variable in the cookie.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Dim intLocation<br> Dim intNameLength<br> Dim intValueLength<br> Dim intNextSemicolon<br> Dim strTemp<br><br> 'calculate length and location of variable name<br> intNameLength = Len(strVariableName)<br> intLocation = Instr(Document.Cookie, strVariableName)<br> <br> 'check for existence of variable name <br> If intLocation = 0 Then<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'variable not found, so it can't be read<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ReadVariable = NOT_FOUND<br> Else<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'get a smaller substring to work with<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;strTemp = Right(Document.Cookie, Len(Document.Cookie) - intLocation + 1)<br><br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'check to make sure we found the full string, not just a substring<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;If Mid(strTemp, intNameLength + 1, 1) &lt;&gt; &quot;=&quot; Then<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'oops, only found substring, not good enough<br> ReadVariable = NOT_FOUND<br><br> 'note that this will incorrectly give a not found result if and only if<br> 'a search for a variable whose name is a substring of a preceding<br> 'variable is undertaken.&nbsp;&nbsp;For example, this will fail:<br> '<br> 'search for: MyVar<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'cookie contains: MyVariable=2;MyVar=1<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Else<br> 'found full string<br> intNextSemicolon = Instr(strTemp, &quot;;&quot;)<br><br> 'if not found, then we need the last element of the cookie<br> If intNextSemicolon = 0 Then intNextSemicolon = Len(strTemp) + 1<br> <br> 'check for empty variable (Var1=;)<br> If intNextSemicolon = (intNameLength + 2) Then<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'variable is empty<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ReadVariable = &quot;&quot;<br> Else<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'calculate value normally<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;intValueLength = intNextSemicolon - intNameLength - 2<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ReadVariable = Mid(strTemp, intNameLength + 2, intValueLength)<br> End If<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;End If<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;End if<br>&nbsp;&nbsp;&nbsp;&nbsp;End Function<br><br>' ***********************************************<br>' Code behind buttons<br>' ***********************************************<br><br>&nbsp;&nbsp;&nbsp;&nbsp;Sub btnSaveVariable_onClick<br> Dim strVariableName<br> Dim varVariableValue<br><br> strVariableName = InputBox(&quot;Enter variable name&quot;)<br> varVariableValue = InputBox(&quot;Enter value for '&quot; & strVariableName & &quot;'&quot;)<br> <br> SetVariable strVariableName, varVariableValue<br>&nbsp;&nbsp;&nbsp;&nbsp;End Sub <br><br>&nbsp;&nbsp;&nbsp;&nbsp;Sub btnReadVariable_onClick<br> Dim strVariableName<br> Dim varVariableValue<br> <br> strVariableName = InputBox(&quot;Enter variable name to read&quot;)<br> varVariableValue = ReadVariable(strVariableName)<br><br> If varVariableValue = NOT_FOUND Then<br> &nbsp;&nbsp;&nbsp;&nbsp;MsgBox &quot;'&quot; & strVariableName & &quot;' not found.&quot;<br> Else<br> &nbsp;&nbsp;&nbsp;&nbsp;MsgBox &quot;'&quot; & strVariableName & &quot;' has a value of '&quot; & varVariableValue & &quot;'.&quot;<br> End If<br>&nbsp;&nbsp;&nbsp;&nbsp;End sub<br><br>&nbsp;&nbsp;&nbsp;&nbsp;Sub btnShowCookie_onClick<br> MsgBox Document.Cookie<br>&nbsp;&nbsp;&nbsp;&nbsp;End Sub<br><br>&nbsp;&nbsp;&nbsp;&nbsp;Sub btnNextPage_onClick<br> Location.HRef = &quot;extcookie2.htm&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;End Sub<br><br>&nbsp;&nbsp;&nbsp;&nbsp;Sub btnKillVariable_onClick<br> Dim strVariableName<br> Dim varVariableValue<br> <br> strVariableName = InputBox(&quot;Enter variable name to delete&quot;)<br> varVariableValue = ReadVariable(strVariableName)<br><br> If varVariableValue = NOT_FOUND Then<br> &nbsp;&nbsp;&nbsp;&nbsp;MsgBox &quot;'&quot; & strVariableName & &quot;' not found.&quot;<br> Else<br> &nbsp;&nbsp;&nbsp;&nbsp;KillVariable(strVariableName)<br> &nbsp;&nbsp;&nbsp;&nbsp;MsgBox &quot;Variable deleted.&quot;<br> End If<br>&nbsp;&nbsp;&nbsp;&nbsp;End Sub<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>--&gt;<br>&lt;/SCRIPT&gt;<br><br>&lt;table&gt;<br><br>&lt;TD VALIGN=TOP BGCOLOR=&quot;#FFFFFF&quot; width=&quot;759&quot;&gt;<br><br>&lt;FONT FACE=&quot;verdana, arial, helvetica&quot; SIZE=2&gt;<br>&lt;P&gt;<br>&lt;form Name=&quot;Form1&quot; action=&quot;&quot;&gt;<br>&lt;INPUT TYPE=&quot;BUTTON&quot; NAME=&quot;btnSaveVariable&quot; VALUE=&quot;Save Variable&quot;&gt;<br>&lt;INPUT TYPE=&quot;BUTTON&quot; NAME=&quot;btnReadVariable&quot; VALUE=&quot;Read Variable&quot;&gt;<br>&lt;INPUT TYPE=&quot;BUTTON&quot; NAME=&quot;btnKillVariable&quot; VALUE=&quot;Kill Variable&quot;&gt;<br>&lt;INPUT TYPE=&quot;BUTTON&quot; NAME=&quot;btnShowCookie&quot; VALUE=&quot;Show Cookie&quot;&gt;<br>&lt;INPUT TYPE=&quot;BUTTON&quot; NAME=&quot;btnNextPage&quot; VALUE=&quot;Next Page&quot;&gt;<br>&lt;/FORM&gt;<br>&lt;p&gt;<br>&lt;/TR&gt;<br><br>&lt;/TABLE&gt;<br><br>&lt;/FONT&gt;<br>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top