Does anyone know how to write cookies?
#1
Scooby Senior
Thread Starter
iTrader: (1)
Join Date: Nov 2000
Location: Wildberg, Germany/Reading, UK
Posts: 9,706
Likes: 0
Received 73 Likes
on
54 Posts
As the title says, what I want to do is add a cookies to our service request system so that it remembers people from the last time they sent a service request. We Have apache running on a HPUX 10.20 server if this is any help.
Cheers
Steve
Cheers
Steve
#2
Scooby Regular
Join Date: Jul 2000
Location: Islington
Posts: 2,145
Likes: 0
Received 0 Likes
on
0 Posts
here is the basic cookie access/write/delete code...
Fosters
mail me if you want more information
put this in cookie.asp:
<BR>
<!-- Cookie display table -->
<TABLE BORDER="2">
<THEAD>
<TH>Cookie Name</TH>
<TH>Cookie Value</TH>
<TH>Delete Cookie</TH>
</THEAD>
<%
Dim Item
For Each Item in Request.Cookies
%>
<TR>
<TD><% = Item %></TD>
<TD><% = Request.Cookies(Item) %></TD>
<TD><A HREF="cookie_process.asp?name=<%= Server.URLEncode(Item) %>">Delete this cookie!</A></TD>
</TR>
<%
Next
%>
</TABLE>
<!-- Cookie adding form -->
<FORM ACTION="cookie_process.asp" METHOD="get">
<TABLE BORDER="0" CELLSPACING="0" CELLPADDING="0">
<TR>
<TD>Cookie Name:</TD>
<TD>Cookie Value:</TD>
<TD></TD>
</TR>
<TR>
<TD><INPUT TYPE="text" NAME="name"></INPUT></TD>
<TD><INPUT TYPE="text" NAME="value"></INPUT></TD>
<TD><INPUT TYPE="submit" VALUE="Add Cookie!"></TD>
</TR>
</TABLE>
and put this is cookie_process.asp:
<%
Response.Buffer = True
Dim strCookieName
Dim strCookieValue
strCookieName = CStr(Request.QueryString("name"))
strCookieValue = CStr(Request.QueryString("value"))
If strCookieName <> "" Then
If strCookieValue <> "" Then
Response.Cookies(strCookieName) = strCookieValue
Response.Cookies(strCookieName).Expires = Date() + 1
Else
Response.Cookies(strCookieName) = "must be something!"
Response.Cookies(strCookieName).Expires = Date() - 1
End If
End If
Response.Redirect("cookie.asp")
%>
Fosters
mail me if you want more information
put this in cookie.asp:
<BR>
<!-- Cookie display table -->
<TABLE BORDER="2">
<THEAD>
<TH>Cookie Name</TH>
<TH>Cookie Value</TH>
<TH>Delete Cookie</TH>
</THEAD>
<%
Dim Item
For Each Item in Request.Cookies
%>
<TR>
<TD><% = Item %></TD>
<TD><% = Request.Cookies(Item) %></TD>
<TD><A HREF="cookie_process.asp?name=<%= Server.URLEncode(Item) %>">Delete this cookie!</A></TD>
</TR>
<%
Next
%>
</TABLE>
<!-- Cookie adding form -->
<FORM ACTION="cookie_process.asp" METHOD="get">
<TABLE BORDER="0" CELLSPACING="0" CELLPADDING="0">
<TR>
<TD>Cookie Name:</TD>
<TD>Cookie Value:</TD>
<TD></TD>
</TR>
<TR>
<TD><INPUT TYPE="text" NAME="name"></INPUT></TD>
<TD><INPUT TYPE="text" NAME="value"></INPUT></TD>
<TD><INPUT TYPE="submit" VALUE="Add Cookie!"></TD>
</TR>
</TABLE>
and put this is cookie_process.asp:
<%
Response.Buffer = True
Dim strCookieName
Dim strCookieValue
strCookieName = CStr(Request.QueryString("name"))
strCookieValue = CStr(Request.QueryString("value"))
If strCookieName <> "" Then
If strCookieValue <> "" Then
Response.Cookies(strCookieName) = strCookieValue
Response.Cookies(strCookieName).Expires = Date() + 1
Else
Response.Cookies(strCookieName) = "must be something!"
Response.Cookies(strCookieName).Expires = Date() - 1
End If
End If
Response.Redirect("cookie.asp")
%>
#3
I don't know if this helps...
Here is a perl script to set a cookie (pinched off the net of course...)
#!/usr/local/perl5/bin/perl
srand(time);
$expdate = mygmtime();
$cid = int(rand(1000000));
print "Set-Cookie: NAME=$cid; expires=$expdate\n";
print "Content-type:text/html\n\n";
print <<EndOfHTML;
<html><head><title>Welcome< ;/title></head>
<body>
<h2>Welcome!</h2>
Your cookie is $cid.<p>
</body></html>
EndOfHTML
sub mygmtime {
@months = ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug ",
"Sep","Oct","Nov","Dec");
@days = ("Sun","Mon","Tue","Wed","Thu","Fri","Sat");
# (time) would be now, so we want to set it to expire sometime
# later. here we expire it in 7 days.
($sec,$min,$hr,$mday,$mon,$yr,$wday,$yday,$isdst) =
gmtime(time + (86400*7));
# format must be Wed, DD-Mon-YYYY HH:MM:SS GMT
$timestr = sprintf("%3s, %02d-%3s-%4d %02d:%02d:%02d GMT",
$days[$wday],$mday,$months[$mon],$yr+1900,$hr,$min,$sec);
return $timestr;
}
And here's one to read a cookie that works with apache, again pinched off net. The cookies are supplied in the HTTP_COOKIE environment variable.
#!/usr/local/perl5/bin/perl
print "Content-type:text/html\n\n";
print <<EndOfHTML;
<html><head><title>Welcome back!</title></head>
<body>
<h2>Welcome back!</h2>
EndOfHTML
$cdata = $ENV{'HTTP_COOKIE'};
print $cdata;
$s="hello";
print $s;
print "Your cookies are:<p>\n";
@cookies = split(/;/,$cdata);
foreach $i (@cookies) {
($name,$cid) = split(/=/,$i);
print "$name = $cid<br>\n";
}
print "</body></html>\n";
See here for spec: http://home.netscape.com/newsref/std/cookie_spec.html
Just an example of how it can be done...
Here is a perl script to set a cookie (pinched off the net of course...)
#!/usr/local/perl5/bin/perl
srand(time);
$expdate = mygmtime();
$cid = int(rand(1000000));
print "Set-Cookie: NAME=$cid; expires=$expdate\n";
print "Content-type:text/html\n\n";
print <<EndOfHTML;
<html><head><title>Welcome< ;/title></head>
<body>
<h2>Welcome!</h2>
Your cookie is $cid.<p>
</body></html>
EndOfHTML
sub mygmtime {
@months = ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug ",
"Sep","Oct","Nov","Dec");
@days = ("Sun","Mon","Tue","Wed","Thu","Fri","Sat");
# (time) would be now, so we want to set it to expire sometime
# later. here we expire it in 7 days.
($sec,$min,$hr,$mday,$mon,$yr,$wday,$yday,$isdst) =
gmtime(time + (86400*7));
# format must be Wed, DD-Mon-YYYY HH:MM:SS GMT
$timestr = sprintf("%3s, %02d-%3s-%4d %02d:%02d:%02d GMT",
$days[$wday],$mday,$months[$mon],$yr+1900,$hr,$min,$sec);
return $timestr;
}
And here's one to read a cookie that works with apache, again pinched off net. The cookies are supplied in the HTTP_COOKIE environment variable.
#!/usr/local/perl5/bin/perl
print "Content-type:text/html\n\n";
print <<EndOfHTML;
<html><head><title>Welcome back!</title></head>
<body>
<h2>Welcome back!</h2>
EndOfHTML
$cdata = $ENV{'HTTP_COOKIE'};
print $cdata;
$s="hello";
print $s;
print "Your cookies are:<p>\n";
@cookies = split(/;/,$cdata);
foreach $i (@cookies) {
($name,$cid) = split(/=/,$i);
print "$name = $cid<br>\n";
}
print "</body></html>\n";
See here for spec: http://home.netscape.com/newsref/std/cookie_spec.html
Just an example of how it can be done...
#5
Scooby Regular
Apache doesn't know how to deal with ASP unless you use a seperate module (Apache::ASP for mod_perl enabled Apache's). A better way is to use the CGI module that comes as standard with any Perl installation. CGI.pm can deal with cookies internally and you should use that instead of rolling your own. To get information on any installed Perl module, use perldoc;
$ perldoc CGI
Will tell you all about the CGI module, it does a lot more than just cookies. Any probs, let me know.
Steve.
$ perldoc CGI
Will tell you all about the CGI module, it does a lot more than just cookies. Any probs, let me know.
Steve.
#7
Thread
Thread Starter
Forum
Replies
Last Post