Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Use VBScript write cookie and get cookie

Posted on 2001-07-18
10
Medium Priority
?
7,796 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Google Certified Professional - Cloud Architect

This course (1 of 3) is designed to help students who are interested in Google Cloud Platform (GCP) to become familiar with the platform, navigate the console and learn its capabilities. It will also prepare students for the Google Cloud Architect certification exam.

 
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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
When it comes to security, close monitoring is a must. According to WhiteHat Security annual report, a substantial number of all web applications are vulnerable always. Monitis offers a new product - fully-featured Website security monitoring and pr…
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

688 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