Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Use VBScript write cookie and get cookie

Posted on 2001-07-18
10
Medium Priority
?
7,915 Views
Last Modified: 2009-07-26
this is my code to write cookie using VBScript, but it can not work.

expiredate = date() + 360
document.cookie = "Ki=" & sel & ";expires=" & expiredate

the following is how I retieve the cookie:

chkcookie=split(document.cookie)
     
for i= 0 to ubound(chkcookie)
if instr(chkcookie(i),"Ki")>0 then
cki=mid(chkcookie(i),11,len(chkcookie(i))-11)
end if
next
0
Comment
Question by:jyx
10 Comments
 
LVL 10

Accepted Solution

by:
dij8 earned 200 total points
ID: 6297927
These two functions will set and get a cookie.

<script language="VBScript" type="text/vbscript">
<!-- // cloak
sub setcookie(cookiename,cookievalue,plusmonths)
  document.cookie = cookiename & "=" & cookievalue & "; expires " & dateAdd("M",plusmonths,Now)
  msgbox(document.cookie)
end sub

sub getcookie(cookiename)
  mycookies = document.cookie
  cstart = instr(mycookies,cookiename)
  if not cstart < 1 then
    clength = len(cookiename) + 1
    cend = instr(cstart,mycookies,";")
    cvalue = mid(mycookies,cstart+clength,cend-clength-cstart)
    msgbox(cvalue)
  else
    msgbox("Cookie not set")
  end if
end sub
// decloak -->
</script>
<a href="#" onclick="getcookie 'Ki'">Get cookie</a><br>
<a href="#" onclick="setcookie 'Ki','Value 2','12'">Set cookie</a>

After strugling with it myself I discovered you can't have "expires=".  It needs to be "expires ".  I also use the dateAdd function and not date() + 360.

I am assuming of course you mean client side.  Server side is much easier if you want to do it that way.
sub setcookie(cookiename,cookievalue,plusmonths)
  response.cookies(cookiename) = cookievalue
  response.cookies(cookiename).expires = dateAdd("M",plusmonths,Now)
end sub

sub getcookie(cookiename)
  request.cookies(cookiename)
end sub
0
 
LVL 4

Expert Comment

by:ravi_raj123
ID: 6298044
Hi,
Here is the code for setting and getting the cookie using javascript.

<html>
<head>
   <script language="Javascript">
      function setcookievalue(name,value){
               document.cookie=name+"="+value+";"+"expires=Wednesday, 19-Aug-01 23:12:40 GMT"
      }

      function getcookievalue(name){
         var temp=document.cookie+";";
         var Pos=temp.indexOf("=",temp.indexOf(name+"="));
         if (temp.indexOf(name+"=")==-1) return "";
         return temp.substring(Pos+1,temp.indexOf(";",Pos));
      }
   </script>
</head>
<body>
   <form name="Example">
      <center>
      Cookie name:<input name="CName" type="text" size=20><br>
      Cookie value:<input name="CVal" type="text" size=40><br>
      <input type="button" value="Set Cookie" onClick="setcookievalue(document.Example.CName.value,document.Example.CVal.value)">
      <input type="button" value="Get Cookie" onClick="document.Example.CVal.value=getcookievalue(document.Example.CName.value)">
      <input type="button" value="View document.cookie" onClick="document.Example.ViewCookie.value=document.cookie"><br>
      document.cookie:<input name="ViewCookie" type="text" size=50>
      </center>
   </form>
</body>
</html>


Hope this maybe helpful to you.
GOOD LUCK...:)
0
 

Author Comment

by:jyx
ID: 6298381
I modified my VBScript as this:
document.cookie = "KiPrinter=" & selprinter & ";expires=01-Jul-05 GMT"
now it can work on my machine, but can not work on another machine I tested. I am confused.
These 2 machines are not using same IE version.
thanks
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 10

Expert Comment

by:dij8
ID: 6298414
The testing I did and scripts I have seen do not have an "=" after expires.  Try:
document.cookie = "KiPrinter=" & selprinter & ";expires 01-Jul-05 GMT"

0
 

Author Comment

by:jyx
ID: 6298852
thanks for all your help.
Now it can work here.
document.cookie = "KiPrinter=" & selprinter & ";expires=Monday, 20-Jan-03 23:12:40 GMT"
0
 

Author Comment

by:jyx
ID: 6298868
The problem is whom should I give the points to? Thanks!
0
 
LVL 10

Expert Comment

by:dij8
ID: 6299107
Points can go to who you like.  Or you can split.  Go to Community Support (link on the left) and post a 0 point question saying you want to split points.  Say how you want to split them and they will give you instructions.  Remember to post a link to this question in your CS question.

I do suggest not going with a literal expiry if you can help it though.  The script I wrote lets you expire it however many months after now as you like.  I can explain if you like.
0
 

Author Comment

by:jyx
ID: 6299249
I tried that DateAdd thing but it just did not work. I will try it later. thanks.
0
 
LVL 10

Expert Comment

by:dij8
ID: 6299795
Glad to help.  Thanks for the A.
0
 
LVL 4

Expert Comment

by:justin-clarke
ID: 22522685
From the Accepted Solution, In the sub 'getcookie' I had an issue with the 'mid' statement when I only had one cookie created. This may help others if they have the same issue...

With only one cookie created, the variable 'cend' equals '0' because it can't find a ';' (semi-colon) in the 'document.cookie' output string.

This output would show 'ki=value 2' (notice no semi-colon at the end, as there is only one cookie no semi-colon separator is needed between cookies) so this meant that the 'cend = instr(cstart,mycookies,";")' part failed as it couldn't find the semi-colon.

The solution I came up with was to change the 1st line of this sub from:

mycookies = document.cookie

to

myCookies = document.cookie + ";"

Which adds the semi-colon onto the end of the 'document.cookie' string so that the 'cend = instr(cstart,mycookies,";")' part can actually find a semi-colon and return the correct end value. Therefore making the output string show 'ki=value 2;'.

I thought this maybe helpful to someone! Cheeers.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
CTAs encourage people to do something specific to show interest in your company, product or service. Keep reading to learn why CTAs should always be thought of as extremely important, albeit small, sections of websites.
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.
Suggested Courses

782 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question