ScoobyNet.com - Subaru Enthusiast Forum

ScoobyNet.com - Subaru Enthusiast Forum (https://www.scoobynet.com/)
-   Non Scooby Related (https://www.scoobynet.com/non-scooby-related-4/)
-   -   Does anyone know how to write cookies? (https://www.scoobynet.com/non-scooby-related-4/51035-does-anyone-know-how-to-write-cookies.html)

Wurzel 25 October 2001 01:19 PM

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

Fosters 25 October 2001 01:42 PM

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")
%>

Captain Sensible 25 October 2001 02:23 PM

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...:)

Captain Sensible 25 October 2001 02:39 PM

My first ever double post:)

[Edited by Captain Sensible - 25/10/2001 15:12:12]

stevencotton 25 October 2001 03:25 PM

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.

stevencotton 25 October 2001 03:36 PM

Double post, awfully slow site today.

[Edited by stevencotton - 25/10/2001 15:40:41]

DavidRB 25 October 2001 04:59 PM

http://www.cookiecentral.com/


All times are GMT +1. The time now is 12:00 PM.


© 2024 MH Sub I, LLC dba Internet Brands