Solved

Use VBScript write cookie and get cookie

Posted on 2001-07-18
10
7,266 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 50 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
 
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Uploading files to the web server has become common part of almost any kind of web application. People use different technologies to solve this, but regardless of the technology used, it is always useful to have some kind of progress indicator shown…
Get to know the ins and outs of building a web-based ERP system for your enterprise. Development timeline, technology, and costs outlined.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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…

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now