Notices
Non Scooby Related Anything Non-Scooby related

Does anyone know how to write cookies?

Thread Tools
 
Search this Thread
 
Old 25 October 2001, 01:19 PM
  #1  
Wurzel
Scooby Senior
Thread Starter
iTrader: (1)
 
Wurzel's Avatar
 
Join Date: Nov 2000
Location: Wildberg, Germany/Reading, UK
Posts: 9,706
Likes: 0
Received 73 Likes on 54 Posts
Cool

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
Old 25 October 2001, 01:42 PM
  #2  
Fosters
Scooby Regular
 
Fosters's Avatar
 
Join Date: Jul 2000
Location: Islington
Posts: 2,145
Likes: 0
Received 0 Likes on 0 Posts
Post

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")
%>
Old 25 October 2001, 02:23 PM
  #3  
Captain Sensible
Scooby Regular
 
Captain Sensible's Avatar
 
Join Date: Jul 2001
Posts: 73
Likes: 0
Received 0 Likes on 0 Posts
Talking

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&lt ;/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...
Old 25 October 2001, 02:39 PM
  #4  
Captain Sensible
Scooby Regular
 
Captain Sensible's Avatar
 
Join Date: Jul 2001
Posts: 73
Likes: 0
Received 0 Likes on 0 Posts
Post

My first ever double post

[Edited by Captain Sensible - 25/10/2001 15:12:12]
Old 25 October 2001, 03:25 PM
  #5  
stevencotton
Scooby Regular
 
stevencotton's Avatar
 
Join Date: Jan 2001
Location: behind twin turbos
Posts: 2,710
Likes: 0
Received 1 Like on 1 Post
Post

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.
Old 25 October 2001, 03:36 PM
  #6  
stevencotton
Scooby Regular
 
stevencotton's Avatar
 
Join Date: Jan 2001
Location: behind twin turbos
Posts: 2,710
Likes: 0
Received 1 Like on 1 Post
Post

Double post, awfully slow site today.

[Edited by stevencotton - 25/10/2001 15:40:41]
Old 25 October 2001, 04:59 PM
  #7  
DavidRB
Scooby Regular
 
DavidRB's Avatar
 
Join Date: Apr 1999
Posts: 1,335
Likes: 0
Received 0 Likes on 0 Posts
Post

http://www.cookiecentral.com/
Related Topics
Thread
Thread Starter
Forum
Replies
Last Post
Frizzle-Dee
Essex Subaru Owners Club
13
09 March 2019 07:35 PM
Arkynsei
Wanted
0
23 September 2015 10:03 PM
philc
ScoobyNet General
4
10 June 2001 01:14 PM
Mr.Cookie
ScoobyNet General
8
28 May 2001 07:51 PM
AWD
ScoobyNet General
9
23 February 2001 09:23 PM



Quick Reply: Does anyone know how to write cookies?



All times are GMT +1. The time now is 09:10 AM.