how to set cookie,there are some problems.

Z

zhanye815

I want ot set cookie,but it can't do.

use CGI::Cookie;
my $c = new CGI::Cookie
-name => 'login',
-value => $keyid,
-expires => '+15m');
print "Set-Cookie: $c\n";
print "Content-Type: text/html\n\n";

it can print the set_cookie string on the screen,but when i use the
"print $ENV{'HTTP_COOKIE'}" to get the cookie,but it show nothing.

I use php to set cookie then use the perl to get cookie ,it work
well.but how to use perl to set cookie ?
my browser is ie6 not the netscape.

thank you .
 
G

Gunnar Hjalmarsson

I want ot set cookie,but it can't do.

use CGI::Cookie;
my $c = new CGI::Cookie
-name => 'login',
-value => $keyid,
-expires => '+15m');
print "Set-Cookie: $c\n";
print "Content-Type: text/html\n\n";

it can print the set_cookie string on the screen,

If it prints the string to screen, you have probably printed two
consecutive newlines before, which makes the string part of the content
and not a CGI header.
but when i use the
"print $ENV{'HTTP_COOKIE'}" to get the cookie,but it show nothing.

If the above hint doesn't help you solve the problem, please post a
short but _complete_ script that attempts to set the cookie. Don't
retype it, but copy and paste the code.
 
B

Bart Van der Donck

I want ot set cookie,but it can't do.

use CGI::Cookie;
my $c = new CGI::Cookie
-name => 'login',
-value => $keyid,
-expires => '+15m');
print "Set-Cookie: $c\n";
print "Content-Type: text/html\n\n";

it can print the set_cookie string on the screen,but when i use the
"print $ENV{'HTTP_COOKIE'}" to get the cookie,but it show nothing.

Out of
http://perl.apache.org/docs/1.0/guide/snippets.html#Handling_Cookies:

sub get_cookies {
map { split /=/, $_, 2 } split /; /, $ENV{'HTTP_COOKIE'} ;
}

I'm thinking you're trying to read out your cookie in the same CGI
session as when your wrote it. That is not possible; read and write
actions need to be separate HTTP requests.
I use php to set cookie then use the perl to get cookie ,it work
well but how to use perl to set cookie ?

One way:

#!/usr/bin/perl
use strict;
use warnings;
use CGI;
my $query = new CGI;
my $cookiename = 'somename';
my $cookievalue = 'somevalue';
my $cookie = $query->cookie(-name=>$cookiename,
-value=>$cookievalue,
-expires=>'+15m',
);
# leave out '-expires'-line for session cookies
print $query->header(-cookie=>$cookie);
print "Cookie was sent to the browser.";
my browser is ie6 not the netscape.

Doesn't matter. Cookies go back to Netscape 1.1 and have been
implemented in all versions of Microsoft Internet Explorer.

Together with the previous code example, here is a perl script that
reads the cookie back:

#!/usr/bin/perl
use strict;
use warnings;
use CGI;
print "Content-Type: text/html\n\n";
my $query = new CGI;
my $cookiename = 'somename';
my $cookieval = $query->cookie($cookiename);
if ( defined $cookieval ) {
print "Value of cookie '$cookiename' is: $cookieval";
}
else {
print "Couldn't read cookie '$cookiename':
no such cookie or cookies disabled in browser";
}

Hope this helps,
 
Z

zhanye815

thank you very much.I use your code to set cookie,it print this in the
screen:
Set-Cookie: somename=somevalue; path=/; expires=Sun, 23-Apr-2006
08:36:02 GMT
Date: Sun, 23 Apr 2006 08:21:02 GMT
Content-Type: text/html; charset=ISO-8859-1

but when i use the code to get the code ,it is the same."Couldn't read
cookie somename:
no such cookie or cookies disabled in browser".
why?do it have something with the path?
 
B

Bart Van der Donck

thank you very much.

You're welcome.
I use your code to set cookie,it print this in the screen:
Set-Cookie: somename=somevalue; path=/; expires=Sun, 23-Apr-2006
08:36:02 GMT
Date: Sun, 23 Apr 2006 08:21:02 GMT
Content-Type: text/html; charset=ISO-8859-1

That would be the output on 'screen' (e.g. terminal), yes. But as I
suppose you mean 'browser', this shouldn't normally happen.

The cookie was not sent to the browser here.
but when i use the code to get the code ,it is the same."Couldn't read
cookie somename:
no such cookie or cookies disabled in browser".

Because the cookie was never sent to the browser, it can't be read out
again.

Following your logic, I'm surprised your output doesn't say:

Content-Type: text/html

Couldn't read cookie 'somename':
no such cookie or cookies disabled in browser

I am also surprised that the single quotes did not appear around
'somename' in your output (typo?), and that the line-ends/spaces appear
exactly as in the source (where did you copy/paste from?). You are
running it as a CGI, right ? Are you viewing the output result with a
common browser in a default client/server environment ?
why?do it have something with the path?

No, that should be okay. Basically, you still have the same problem as
in your original post. I think there is something going on with your
page headers. Are you sure you don't have 2 consecutive newlines in
your output before the cookie, as Gunnar suggested ?

In order to narrow down the problem, what does the following script say
(as CGI):

#!/usr/bin/perl
print 'test';

Maybe your CGI configuration (or CGI.pm?) is configured so that it
automatically starts each output with "Content-Type: text/html\n\n".
 
Z

zhanye815

I am a chinese,so sometimes i can't use the appropriate word .I use ie6
and Dzsoft perl editor to test:
#!/usr/bin/perl
print 'test';

the result is this:
this page can't display.The chinese is 该页无法显示
 
Z

zhanye815

I test this code in ie6+iis6+active perl+win2003

print "Content-type: text/html\n\n";
print "<html><h1>Hello!</h1></html>\n";

the browser show this:

Content-type: text/html
Hello!

i think the header:"Content-type: text/html " should not show in the
browser, i use the notepad to see the original code of this page,it is
the same as the browser show ,that is say it have only one
"Content-type: text/html ".
 
B

Bart Van der Donck

I test this code in ie6+iis6+active perl+win2003

print "Content-type: text/html\n\n";
print "<html><h1>Hello!</h1></html>\n";

Please be as accurate as possible. Is there no line #!perl or
#!/usr/bin/perl as first line of your script ?
the browser show this:

Content-type: text/html
Hello!

i think the header:"Content-type: text/html " should not show in the
browser

Yes, that's is the purpose.
, i use the notepad to see the original code of this page,it is
the same as the browser show ,that is say it have only one
"Content-type: text/html ".

What is the name and extension of your file ? (test.pl - test.cgi -
test.html... )

What happens for the following code:

#!perl
print "Content-Type: text/html\n\n";
print "<html><h1>Hello!</h1></html>\n";

(Note the T-capital in Content-Type)
And

#!perl
print "<html><h1>Hello!</h1></html>\n";

And

print "<html><h1>Hello!</h1></html>\n";

And

#!perl
use CGI::Carp qw(fatalsToBrowser);
print 'test';

http://babelfish.altavista.com can help for English/Chinese/English
translations.
 
Z

zhanye815

#!perl
print "Content-Type: text/html\n\n";
print "<html><h1>Hello!</h1></html>\n";

the result is is:
Content-Type: text/html
Hello!
#!perl
print "<html><h1>Hello!</h1></html>\n"; and the code
print "<html><h1>Hello!</h1></html>\n";

the result is same:
Hello!
#!perl
use CGI::Carp qw(fatalsToBrowser);
print 'test';
the result is :
Content-type: text/html 'c:\inetpub\wwwroot\perl\test.cgi' script
produced no output.
 
B

Bart Van der Donck


Let me summarize your CGI tests.

----------------------------------------------

CASE 1. Full script:

#!perl
print "<html><h1>Hello!</h1></html>\n";

prints

Hello!

Expected behaviour would be: error, because no content-type header was
specified.

----------------------------------------------

CASE 2. Full script:

print "<html><h1>Hello!</h1></html>\n";

prints

Hello!

Expected behaviour would be: error, because no content-type header was
specified + no shebang line was specified.

----------------------------------------------

CASE 3. Full script:

#!perl
use CGI::Carp qw(fatalsToBrowser);
print 'test';

prints

Content-type: text/html
'c:\inetpub\wwwroot\perl\test.cgi' script
produced no output.

This is the expected behaviour.

----------------------------------------------

CASE 4. Full script:

print "Content-type: text/html\n\n";
print "<html><h1>Hello!</h1></html>\n";

prints

Content-Type: text/html
Hello!

Expected behaviour would be: error, because no shebang line was
specified.

----------------------------------------------

CASE 5. Full script:

#!/usr/bin/perl
print 'test';

prints

"This page can't display." (internal server error)

This is the expected behaviour.

----------------------------------------------

I'ld suggest to stop using the DZsoft Perl editor. It might add code to
your script you're not aware of. Better use Notepad to find the
problem. Especially when you're talking about scripts without shebang
(=the first #!...-line). They shouldn't compile at all and I don't
believe you could run those anyhow.

What about the following (full) scripts:

#!perl
print 'test';

or

#!/usr/bin/perl
print "<html><h1>Hello!</h1></html>\n";

or

print 'test';

If you're absolutely sure your testing results are correct, there must
be something very awkward going on with your CGI configuration. I don't
see any other possibility.

Maybe someone else in this group has a clearer crystal ball than I.
 
B

Bart Van der Donck

Bart said:
Let me summarize your CGI tests.

----------------------------------------------

CASE 1. Full script:

#!perl
print "<html><h1>Hello!</h1></html>\n";

prints

Hello!

Expected behaviour would be: error, because no content-type header was
specified.

----------------------------------------------

CASE 2. Full script:

print "<html><h1>Hello!</h1></html>\n";

prints

Hello!

Expected behaviour would be: error, because no content-type header was
specified + no shebang line was specified.

----------------------------------------------

CASE 3. Full script:

#!perl
use CGI::Carp qw(fatalsToBrowser);
print 'test';

prints

Content-type: text/html
'c:\inetpub\wwwroot\perl\test.cgi' script
produced no output.

This is the expected behaviour.

----------------------------------------------

CASE 4. Full script:

print "Content-type: text/html\n\n";
print "<html><h1>Hello!</h1></html>\n";

prints

Content-Type: text/html
Hello!

Expected behaviour would be: error, because no shebang line was
specified.

----------------------------------------------

CASE 5. Full script:

#!/usr/bin/perl
print 'test';

prints

"This page can't display." (internal server error)

This is the expected behaviour.

One conclusion could be:

You're somehow using a wrong single quote character. I'm not sure how a
Chinese keyboard looks like (aren't there 6,000 basic characters +
50,000 extensions? :)). In addition, each script would have an
'auto-shebang' which automatically presets the shebang line plus prints
"Content-type: text/html\n\n" by default. Maybe this could be done
silently by the DZsoft editor. The eventual next shebang-line in a
script will then be considered as a comment.

Maybe that could explain your problem.
 
Z

zhanye815

I am very glad with your help,thanks!

The fatal problem is the cgi configuration.The path to parse the .cgi
used to be
C:\perl\bin\perlis.dll

it works well,i don't know why...
you said maybe my cgi configuration's problem,let me check my iis
configuration through i think it work well because i use the perl to
operate MySql for a long time.I google it, then i find somebady say the
path to parse .cgi must be:
C:\perl\bin\perl.exe -T "%s" %s

I try it,the browse can not parse the .cgi,then i delete the "-T",it
work well,and the cookie can set and get.

You said the chinese keyboard,it is the standard 101/102-key ,the
differentness is the character encode. Our chinese have thousands of
characters,so chinese character and symbol use 2 bytes to express and
we divide our usual chinese characters to a 94*94 matrix,so the row
number with the col number represent a character or symbol,we call it
zone bit code.We use the row 16 and col 1 to put the first chinese
character,and for distinguish with Ascall,we use the two byte's highest
bit as 1.I know only these.

thank you again!!
 
Z

zhanye815

I am very glad with your help,thanks!

The fatal problem is the cgi configuration.The path to parse the .cgi
used to be
C:\perl\bin\perlis.dll

it works well,i don't know why...
you said maybe my cgi configuration's problem,let me check my iis
configuration through i think it work well because i use the perl to
operate MySql for a long time.I google it, then i find somebady say the
path to parse .cgi must be:
C:\perl\bin\perl.exe -T "%s" %s

I try it,the browse can not parse the .cgi,then i delete the "-T",it
work well,and the cookie can set and get.

You said the chinese keyboard,it is the standard 101/102-key ,the
differentness is the character encode. Our chinese have thousands of
characters,so chinese character and symbol use 2 bytes to express and
we divide our usual chinese characters to a 94*94 matrix,so the row
number with the col number represent a character or symbol,we call it
zone bit code.We use the row 16 and col 1 to put the first chinese
character,and for distinguish with Ascall,we use the two byte's highest
bit as 1.I know only these.

thank you again!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,737
Latest member
Georgeengab

Latest Threads

Top