[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

How to "Write" and "Read" Cookies in VBScript ?

Posted on 2008-10-08
5
Medium Priority
?
1,055 Views
Last Modified: 2012-05-05
Hi,

I want to write a cookie called "UserName" and its value is "Sam". Can you please provide me complete code in VBScript - How to write that cookie and how to read that cookie ?

Appreciate your help for providing the code !

Thanks
0
Comment
Question by:milani_lucie
[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
5 Comments
 
LVL 8

Expert Comment

by:vsudip
ID: 22675843
0
 
LVL 4

Expert Comment

by:techvagabond
ID: 22675857
Try something like this


<%@ Page Language=VB Debug=true %>
<script runat=server>
Sub SubmitBtnWrite_Click(Sender As Object, E As EventArgs)
        Response.Cookies("UserName").Expires = "5/1/2010"
        Response.Cookies("UserName").Value = "Sam"
End Sub
Sub SubmitBtnRead_Click(Sender As Object, E As EventArgs)
    Dim I as integer
    For I = 0 to Request.Cookies.Count - 1
        lblMessage1.Text = lblMessage1.Text  _
            & Request.Cookies.Item(I).Name & ": " _
            & Request.Cookies.Item(I).Value & "<BR>"
    Next
End Sub
</SCRIPT>
<HTML>
<HEAD>
<TITLE>Cookies Sample Page</TITLE>
</HEAD>
<BODY >
<form runat="server">
<Font Face="Tahoma">
<asp:Label
    id="lblMessage1"
    runat="Server"
    Font-Bold="True"
/>
<BR><BR>
<asp:button
    id="butOK1"
    runat="server"
    text="Write Cookies to Browser"
    Type="Submit"
    OnClick="SubmitBtnWrite_Click"
/>
<asp:button
    id="butOK2"
    runat="server"
    text="Read Cookies from Browser"
    Type="Submit"
    OnClick="SubmitBtnRead_Click"
/>
</Font>
</Form>
</BODY>
</HTML>
0
 

Author Comment

by:milani_lucie
ID: 22675882
techvagabond:

Do we need to go to the server ? Can we do it on client side ? I think so we have something like this:

Document.Cookie = "UserName=Sam"

And we need NOT write

Response.Cookies("UserName").Value = "Sam"

Am i correct ? Please correct me if i am wrong !

Thanks
0
 
LVL 4

Expert Comment

by:techvagabond
ID: 22675903
yea you could give that a shot. Try and run it and see how you go. best way to learn is to fiddle :D
0
 
LVL 16

Accepted Solution

by:
sh0e earned 2000 total points
ID: 22699310

<script type="text/vbscript">
Function ReadCookie(name)
	Set re = New RegExp
	re.IgnoreCase = True
	re.Pattern = "(?:^|;) ?" & name & "=([^;]*)(?:;|$)"
	Set matches = re.Execute(document.cookie)
	If matches.Count > 0 Then
		ReadCookie = matches(0).SubMatches(0)
	Else
		ReadCookie = ""
	End If
End Function
 
Function WriteCookie(name, val)
	document.cookie = name & "=" & val
End Function
 
WriteCookie "attack","attack"
WriteCookie "tack","tack"
WriteCookie "stack","stack"
 
MsgBox document.cookie
 
MsgBox ReadCookie("attack")
MsgBox ReadCookie("tack")
MsgBox ReadCookie("stack")
If ReadCookie("ack") = "" Then
	MsgBox "No cookie data found for ack"
End If
 
</script>
 
<script type="text/javascript">
function readcookieJS(name){
	var re = new RegExp("(?:^|;) ?" + name + "=([^;]*)(?:;|$)", "i");
	var match = document.cookie.match(re);
	return match == null ? "" : match[1];
	/*If matches.Count > 0 Then
		ReadCookie = matches(0).SubMatches(0)
	Else
		ReadCookie = Null
	End If*/
}
function writecookieJS(name, val){
	document.cookie = name + "=" + val;
}
 
writecookieJS("attack1","attack1")
writecookieJS("tack1","tack1")
writecookieJS("stack1","stack1")
 
alert(document.cookie);
 
alert(readcookieJS("attack1"));
alert(readcookieJS("tack1"));
alert(readcookieJS("stack1"));
if(readcookieJS("ack1") == "")
	alert("No cookie data found for ack1");
</script>

Open in new window

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

650 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