Click to See Complete Forum and Search --> : Creating and Reading Cookie


terry81
09-14-2004, 01:59 AM
Hi there, I try the follow write and read codes for cookie but the read cookie codes doesnt seem to work. someone know why?

Is there a need to use 'CGI::Carp(fatalsToBrowser)' for cookies?

what if I have a $sessionID containing the session ID and I want the session ID to be the value of the cookie, how do I do that?



Write Cookie:

%txtValues = ('Visit' => '2');
$this_cookie= cookie(-name=>'CookieMonster', -value=>\%txtValues, -path=>'/', -expire=>'+2h');



Read Cookie:

my %data = cookie('CookieMonster');
my @keys = keys %data;
my @values = values %data;

while(@keys)
{
print("<p>");
print(pop(@keys), " = ", pop(@values), "\n");
print("</p>");
}

exit(0);

Nedals
09-14-2004, 02:49 AM
#!/usr/bin/perl

# Reduce spelling error and control variable scope
use strict;

# Use CGI module to get all your form AND cookie data.
use CGI;
use CGI::Carp(fatalsToBrowser); # This is used to debug your script

my $q->new CGI;

# Read a cookie
my $cookie_value = $q->cookie('name');


# Write a cookie.
my $cookie = 'name=value'; # simplest form

# This can be done using the CGI module, but I prefer this method.
print "Content-Type: text/html\n";
print "Set-Cookie: $cookie\n\n";

terry81
09-14-2004, 03:27 AM
Hi Nedals,

thanks for your help.

but I will like to append the session id into the cookie as value,
how sld i write the code?

assuming i have a variable $sessionID containin auto generated numbers..how do I append to the cookie?

btw,
print "Content-Type: text/html\n";
print "Set-Cookie: $cookie\n\n";

is this supposed to be the same page as where the cookie is supposed to be read or write?

Nedals
09-14-2004, 11:18 AM
....but I will like to append the session id into the cookie as value, how sld i write the code?
my $cookie = "id=$sessionID";
## You can also add path, expires, secure as needed

print "Content-Type: text/html\n";
print "Set-Cookie: $cookie\n\n"; ### This WRITES the cookie
## HTML page follows this

terry81
09-14-2004, 09:19 PM
Hi Nedals,

The cookie u shown is actually a memory-only cookie which only delete when the broswer is closed. So how do I delete the cookie if the user click logout and yet not close the broswer?


and how do I check if the cookie exist when the user decided to come back again while the broswer is still open? What i did is to read for cookie and use the if else statement to check for null or exist, if so the scrip will bring the user to the profile page, if not to login page.

Nedals
09-14-2004, 09:37 PM
Write a cookie, with the same name, and with an expiration date earlier than today. I use...
my $cookie = "id='';expires=01 Jan 2004 00:00:00";

Slipped a bit in while I was posting, did you! :)
What i did is to read for cookie and use the if else statement to check for null or exist, if so the scrip will bring the user to the profile page, if not to login page.
That's good!

terry81
09-15-2004, 12:03 AM
so as long as I delete the cookie when user click logout, the cookie will be deleted since with expiration date, it is save in hard disk.
but then, what if user did not click logout? does it mean the cookie will only expire based on the hard coded dates?!! If so what if i put the date as 1 yr after? hahaa


btw, how to i compare the value in cookie when using if else statement?

example:
$readcookie = cookie('ABC');

if ( $readcookie eq ""){} else{};


but it seem it like the readcookie cant performace "" to see if it is null. get what i mean?

Nedals
09-15-2004, 02:41 AM
terry81,
You are beginning at ask some very basic questions.

When you write a cookie with no date, it persists as long as the browser is open.
When you write a cookie with a date later than today, it persists until that date.
When you write a cookie with a date earlier than today, the cookie is deleted.

It seems that you probably need to read up on using cookies. Perhaps someone here knows of a good tutorial, otherwise do a google search on 'cookie tutorial'.

As to your 'if else' question..
If you write this cookie.... my $cookie = "id=$sessionID";

if ($cookie_value eq $sessionID) {
go to profile page
} else {
goto login page.
}

It sounds as though you could use a tutorial on Perl basics.

terry81
09-15-2004, 03:24 AM
well, i have searched the web and found this forum to answer my basic queries =p

anyway, most of the books i read didnt tell me much about the reason behind this and that...for exmaple, creating, read and delete cookie is not further explained other than providin a sample of the codes and tellin us what is the struture like. well obviously i know how this and that work but need to ask others to confirm my understanding alright? :D

didnt i posted in my previous thread i just started learning perl?

maybe one day i can be a powerful perl writer than you. :p

have to tahnks you for yr effort and patience for me hehe

terry81
09-15-2004, 09:40 PM
Hi Mr Nedals,

it's me again!!:D

I have problem w my cookie again.

I am trying to create a cookie within a .pl

but it always print out : Set-Cookie: Shopping=1234568Content-TypeSet-Cookie: : text/html\n


sub createCookie
{
#creating a memory-only cookie
print "Set-Cookie: Shopping=$sessionID\n";
print "Content-Type: text/html\n";
print "Set-Cookie: $cookie\n\n";
}

I will only create a cookie once a user is verified. So it is not possible or maybe? to create the cookie on top of the script before executing other script?

Nedals
09-15-2004, 11:09 PM
sub createCookie
{
#creating a memory-only cookie. I think you mean 'read-only'
print "Set-Cookie: Shopping=$sessionID\n";
print "Content-Type: text/html\n";
print "Set-Cookie: $cookie\n\n";
}Here you are attempting to set the cookie twice, and in the wrong order.
But don't do it this way. First forget the sub. It's not needed here.

Ok!
Somewhere you are printing an HTML doc, so do it like this
#main program
..
..
my $cookie = "Shopping=$sessionID"; ## using strict and no print
&printHTML($cookie); ## call the subroutine and pass the cookie
exit;

sub printHTML {
my ($cookie) = @_; ## pass the cookie to the subroutine

print "Content-Type: text/html\n";
print "Set-Cookie: $cookie\n\n";
print "the rest of you html doc";
}So it is not possible or maybe? to create the cookie on top of the script before executing other script?I'm not sure what you are asking. :confused: