Solved

Use VBScript write cookie and get cookie

Posted on 2001-07-18
10
7,676 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 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Although a lot of people devote their energy toward marketing for specific industries, there are some basic principles that can be applied to any sector imaginable. We’ll look at four steps to take and examine how those steps were put into action fo…
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.
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

623 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