
unable to set cookie when using template toolkit
Hi
In my script i am using template toolkit. I am trying to set the
cookie in my browser but all that it does is it prints the following
output on my broswer :
Set-Cookie: CGISESSID=f032fc8982a1ae022c4f51baa3bc4143; path=/ Date:
Wed, 04 May 2011 13:07:40 GMT Content-Type: text/html;
charset=ISO-8859-1
here is my script , how do is set cookie
=========================
#!/usr/bin/perl
use strict ;
use warnings ;
use Template;
use Data::Dumper;
use CGI::Session;
use CGI;
print "Content-type: text/html\n\n";
my $cgi = new CGI;
my $session = new CGI::Session(undef, $cgi, {Directory=>'/tmp'});
my $tt = Template->new( INCLUDE_PATH => "/var/www/html/template" ) ||
die "template process failed:$!";
my %tag;
$session->clear();
$session->expire('+2h');
my $evalue = init($cgi, $session);
if ( $evalue == 3 ) {
my $cookie = $cgi->cookie(CGISESSID => $session->id);
print $cgi->header( -cookie=>$cookie );
deploy_page();
}
unless ( $session->param("~logged-in") ) {
login_page();
$session->delete();
exit(0);
}
sub init {
my ($cgi,$session) = [at] _; # receive two args
if ( $session->param("~logged-in") ) {
return 3 ; # if logged in, don't bother going further
}
my $lg_name = $cgi->param("lg_name") or "dfdsfsd";
my $lg_psswd = $cgi->param("lg_password") or "dsfdsfsd";
# if we came this far, user did submit the login form
# so let's try to load his/her profile if name/psswds match
if ( my $profile = _load_profile($lg_name, $lg_psswd) ) {
$session->param("~profile", $profile);
$session->param("~logged-in", 1);
$session->clear(["~login-trials"]);
return 3;
}
}
sub _load_profile {
my ($lg_name, $lg_psswd) = [at] _;
local $/ = "\n";
unless (sysopen(PROFILES, "profiles.txt", O_RDONLY) ) {
die "Couldnt open profiles.txt: $!";
}
while ( <PROFILES> ) {
/^(\n|#)/ and next;
chomp;
my ($n, $p, $e) = split (/\s+/,$_) ;
if ( ($n eq $lg_name) && ($p eq $lg_psswd) ) {
my $p_mask = "x" . length($p);
return {username=>$n, password=>$p_mask, email=>$e};
}
}
close(PROFILES);
return undef;
}
sub deploy_page {
$tt->process("form/form.html") || die $tt->error();
}
sub login_page {
$tt->process("login.tmpl",\%tag) || die $tt->error();
}
==============================================
thanks for all the help !!
--
Regards
Agnello D'souza
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: unable to set cookie when using template toolkit
Remove this line:
print "Content-type: text/html\n\n";
The $cgi->header call is also outputting the HTTP header, but you have
already created it with the line above. That is why you can see the
generated header in your browser.
If $evalue != 3 you may want to call $cgi->header without the cookie
depending on the behaviour you want.
Cheers,
Pete
On 04/05/11 14:23, Agnello George wrote:
> Hi
>
> In my script i am using template toolkit. I am trying to set the
> cookie in my browser but all that it does is it prints the following
> output on my broswer :
>
> Set-Cookie: CGISESSID=f032fc8982a1ae022c4f51baa3bc4143; path=/ Date:
> Wed, 04 May 2011 13:07:40 GMT Content-Type: text/html;
> charset=ISO-8859-1
>
> here is my script , how do is set cookie
> =========================
>
> #!/usr/bin/perl
> use strict ;
> use warnings ;
> use Template;
> use Data::Dumper;
> use CGI::Session;
> use CGI;
>
> print "Content-type: text/html\n\n";
>
> my $cgi = new CGI;
> my $session = new CGI::Session(undef, $cgi, {Directory=>'/tmp'});
>
>
> my $tt = Template->new( INCLUDE_PATH => "/var/www/html/template" ) ||
> die "template process failed:$!";
>
> my %tag;
>
> $session->clear();
> $session->expire('+2h');
>
>
> my $evalue = init($cgi, $session);
>
> if ( $evalue == 3 ) {
> my $cookie = $cgi->cookie(CGISESSID => $session->id);
> print $cgi->header( -cookie=>$cookie );
> deploy_page();
>
> }
>
>
> unless ( $session->param("~logged-in") ) {
> login_page();
> $session->delete();
> exit(0);
>
> }
>
>
> sub init {
> my ($cgi,$session) = [at] _; # receive two args
>
> if ( $session->param("~logged-in") ) {
> return 3 ; # if logged in, don't bother going further
> }
>
>
>
> my $lg_name = $cgi->param("lg_name") or "dfdsfsd";
> my $lg_psswd = $cgi->param("lg_password") or "dsfdsfsd";
>
> # if we came this far, user did submit the login form
> # so let's try to load his/her profile if name/psswds match
> if ( my $profile = _load_profile($lg_name, $lg_psswd) ) {
>
> $session->param("~profile", $profile);
> $session->param("~logged-in", 1);
> $session->clear(["~login-trials"]);
>
> return 3;
> }
>
> }
>
>
> sub _load_profile {
> my ($lg_name, $lg_psswd) = [at] _;
> local $/ = "\n";
> unless (sysopen(PROFILES, "profiles.txt", O_RDONLY) ) {
> die "Couldnt open profiles.txt: $!";
> }
> while (<PROFILES> ) {
> /^(\n|#)/ and next;
> chomp;
> my ($n, $p, $e) = split (/\s+/,$_) ;
> if ( ($n eq $lg_name)&& ($p eq $lg_psswd) ) {
> my $p_mask = "x" . length($p);
>
> return {username=>$n, password=>$p_mask, email=>$e};
> }
> }
> close(PROFILES);
>
> return undef;
> }
>
>
> sub deploy_page {
>
> $tt->process("form/form.html") || die $tt->error();
> }
>
> sub login_page {
>
> $tt->process("login.tmpl",\%tag) || die $tt->error();
> }
> ==============================================
>
> thanks for all the help !!
>
NET-A-PORTER.COM
Irresistible fashion at your fingertips
____________________________________________________________ __________
CONFIDENTIALITY NOTICE
The information in this email is confidential and is intended solely for the addressee. Access to this email by anyone else is unauthorised. If you are not the intended recipient, you must not read, use or disseminate the information. Any views expressed in this message are those of the individual sender, except where the sender specifically states them to be the views of Net a Porter Ltd.
Net A Porter Ltd is a company registered in England & Wales Number: 3820604 Registered Office: 1 The Village Offices, Westfield, Ariel Way, London, W12 7GF
____________________________________________________________ _________
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: unable to set cookie when using template toolkit
On Wed, May 4, 2011 at 7:01 PM, Pete Smith <pete.smith [at] net-a-porter.com> wr=
ote:
> Remove this line:
>
> print "Content-type: text/html\n\n";
if i remvove this line for my script login.pl the web server log gives me
[Wed May 04 19:14:30 2011] [error] [client 192.168.2.94] malformed
header from script. Bad header=3D<form action=3D"/cgi-bin/login.p:
login.pl
and browser gives me internal server error .
>
> The $cgi->header call is also outputting the HTTP header, but you have
> already created it with the line above. That is why you can see the
> generated header in your browser.
>
> If $evalue !=3D 3 you may want to call $cgi->header without the cookie
> depending on the behaviour you want.
>
> Cheers,
> Pete
>
> On 04/05/11 14:23, Agnello George wrote:
>>
>> Hi
>>
>> In my script i am using template toolkit. I am trying to set the
>> cookie in my browser but all that it does is =A0it prints the following
>> output on my broswer :
>>
>> Set-Cookie: CGISESSID=3Df032fc8982a1ae022c4f51baa3bc4143; path=3D/ Date:
>> Wed, 04 May 2011 13:07:40 GMT Content-Type: text/html;
>> charset=3DISO-8859-1
>>
>> here is my script , how do is set =A0cookie
>> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D=3D=3D=
=3D
>>
>> #!/usr/bin/perl
>> use strict ;
>> use warnings ;
>> use Template;
>> use Data::Dumper;
>> use CGI::Session;
>> use CGI;
>>
>> print "Content-type: text/html\n\n";
>>
>> =A0my $cgi =3D new CGI;
>> =A0my $session =3D new CGI::Session(undef, $cgi, {Directory=3D>'/tmp'});
>>
>>
>> my $tt =3D Template->new( INCLUDE_PATH =3D> =A0"/var/www/html/template" =
) ||
>> die "template process failed:$!";
>>
>> my %tag;
>>
>> =A0 =A0 =A0 =A0 =A0 =A0 $session->clear();
>> =A0 =A0 =A0 =A0 =A0 =A0 $session->expire('+2h');
>>
>>
>> my $evalue =3D =A0init($cgi, $session);
>>
>> if ( $evalue =3D=3D 3 ) {
>> =A0my =A0$cookie =3D $cgi->cookie(CGISESSID =3D> =A0$session->id);
>> =A0 =A0 print $cgi->header( -cookie=3D>$cookie );
>> =A0 =A0 =A0 deploy_page();
>>
>> }
>>
>>
>> =A0 =A0 unless ( $session->param("~logged-in") ) {
>> =A0 =A0 =A0 =A0 login_page();
>> =A0 =A0 =A0 =A0 $session->delete();
>> =A0 =A0 =A0 =A0 exit(0);
>>
>> =A0 =A0 }
>>
>>
>> sub init {
>> =A0 =A0 =A0 =A0 my ($cgi,$session) =3D [at] _; # receive two args
>>
>> =A0 =A0 =A0 =A0 if ( $session->param("~logged-in") ) {
>> =A0 =A0 =A0 =A0 =A0 =A0 return 3 ; =A0# if logged in, don't bother going=
further
>> =A0 =A0 =A0 =A0 }
>>
>>
>>
>> =A0 =A0 =A0 =A0 my $lg_name =3D $cgi->param("lg_name") or "dfdsfsd";
>> =A0 =A0 =A0 =A0 my $lg_psswd =3D $cgi->param("lg_password") or "dsfdsfsd=
";
>>
>> =A0 =A0 =A0 =A0 # if we came this far, user did submit the login form
>> =A0 =A0 =A0 =A0 # so let's try to load his/her profile if name/psswds ma=
tch
>> =A0 =A0 =A0 =A0 if ( my $profile =3D _load_profile($lg_name, $lg_psswd) =
) {
>>
>> =A0 =A0 =A0 =A0 =A0 =A0 $session->param("~profile", $profile);
>> =A0 =A0 =A0 =A0 =A0 =A0 $session->param("~logged-in", 1);
>> =A0 =A0 =A0 =A0 =A0 =A0 $session->clear(["~login-trials"]);
>>
>> =A0 =A0 =A0 =A0 =A0 return 3;
>> =A0 =A0 =A0 =A0 }
>>
>> =A0 =A0 }
>>
>>
>> =A0sub _load_profile {
>> =A0 =A0 =A0 =A0 my ($lg_name, $lg_psswd) =3D [at] _;
>> =A0 =A0 =A0 =A0 local $/ =3D "\n";
>> =A0 =A0 =A0 =A0 unless (sysopen(PROFILES, "profiles.txt", O_RDONLY) ) {
>> =A0 =A0 =A0 =A0 =A0 =A0 die "Couldnt open profiles.txt: $!";
>> =A0 =A0 =A0 =A0 }
>> =A0 =A0 =A0 =A0 while (<PROFILES> =A0) {
>> =A0 =A0 =A0 =A0 =A0 =A0 /^(\n|#)/ and next;
>> =A0 =A0 =A0 =A0 =A0 =A0 chomp;
>> =A0 =A0 =A0 =A0 =A0 =A0 my ($n, $p, $e) =3D split (/\s+/,$_) ;
>> =A0 =A0 =A0 =A0 =A0 =A0 if ( ($n eq $lg_name)&& =A0($p eq $lg_psswd) ) {
>> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 my $p_mask =3D "x" . length($p);
>>
>> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 return {username=3D>$n, password=3D>$p_m=
ask, email=3D>$e};
>> =A0 =A0 =A0 =A0 =A0 =A0 }
>> =A0 =A0 =A0 =A0 }
>> =A0 =A0 =A0 =A0 close(PROFILES);
>>
>> =A0 =A0 =A0 =A0 return undef;
>> =A0 =A0 }
>>
>>
>> sub deploy_page {
>>
>> $tt->process("form/form.html") || die $tt->error();
>> }
>>
>> sub login_page {
>>
>> $tt->process("login.tmpl",\%tag) || die $tt->error();
>> }
>> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D
>>
>> thanks for all the help !!
>>
>
> NET-A-PORTER.COM Irresistible fashion at your fingertips
>
> ____________________________________________________________ __________
>
> CONFIDENTIALITY NOTICE
> The information in this email is confidential and is intended solely for =
the
> addressee. Access to this email by anyone else is unauthorised. If you ar=
e
> not the intended recipient, you must not read, use or disseminate the
> information. Any views expressed in this message are those of the individ=
ual
> sender, except where the sender specifically states them to be the views =
of
> Net a Porter Ltd.
> Net A Porter Ltd is a company registered in England & Wales Number: 38206=
04
> Registered Office: 1 The Village Offices, Westfield, Ariel Way, London, W=
12
> 7GF
> ____________________________________________________________ _________
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
> For additional commands, e-mail: beginners-help [at] perl.org
> http://learn.perl.org/
>
>
>
--
Regards
Agnello D'souza
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: unable to set cookie when using template toolkit
On 04/05/11 14:44, Agnello George wrote:
> if i remvove this line for my script login.pl the web server log gives me
>
> [Wed May 04 19:14:30 2011] [error] [client 192.168.2.94] malformed
> header from script. Bad header=<form action="/cgi-bin/login.p:
> login.pl
>
> and browser gives me internal server error .
This may be because $evalue !=3 and therefore the if block does not
execute. You need to send the header in all cases. Try changing your if
block to:
if ( $evalue == 3 ) {
my $cookie = $cgi->cookie(CGISESSID => $session->id);
print $cgi->header( -cookie=>$cookie );
deploy_page();
}
else {
print $cgi->header;
}
Pete
>>
>> The $cgi->header call is also outputting the HTTP header, but you have
>> already created it with the line above. That is why you can see the
>> generated header in your browser.
>>
>> If $evalue != 3 you may want to call $cgi->header without the cookie
>> depending on the behaviour you want.
>>
>> Cheers,
>> Pete
>>
>> On 04/05/11 14:23, Agnello George wrote:
>>>
>>> Hi
>>>
>>> In my script i am using template toolkit. I am trying to set the
>>> cookie in my browser but all that it does is it prints the following
>>> output on my broswer :
>>>
>>> Set-Cookie: CGISESSID=f032fc8982a1ae022c4f51baa3bc4143; path=/ Date:
>>> Wed, 04 May 2011 13:07:40 GMT Content-Type: text/html;
>>> charset=ISO-8859-1
>>>
>>> here is my script , how do is set cookie
>>> =========================
>>>
>>> #!/usr/bin/perl
>>> use strict ;
>>> use warnings ;
>>> use Template;
>>> use Data::Dumper;
>>> use CGI::Session;
>>> use CGI;
>>>
>>> print "Content-type: text/html\n\n";
>>>
>>> my $cgi = new CGI;
>>> my $session = new CGI::Session(undef, $cgi, {Directory=>'/tmp'});
>>>
>>>
>>> my $tt = Template->new( INCLUDE_PATH => "/var/www/html/template" ) ||
>>> die "template process failed:$!";
>>>
>>> my %tag;
>>>
>>> $session->clear();
>>> $session->expire('+2h');
>>>
>>>
>>> my $evalue = init($cgi, $session);
>>>
>>> if ( $evalue == 3 ) {
>>> my $cookie = $cgi->cookie(CGISESSID => $session->id);
>>> print $cgi->header( -cookie=>$cookie );
>>> deploy_page();
>>>
>>> }
>>>
>>>
>>> unless ( $session->param("~logged-in") ) {
>>> login_page();
>>> $session->delete();
>>> exit(0);
>>>
>>> }
>>>
>>>
>>> sub init {
>>> my ($cgi,$session) = [at] _; # receive two args
>>>
>>> if ( $session->param("~logged-in") ) {
>>> return 3 ; # if logged in, don't bother going further
>>> }
>>>
>>>
>>>
>>> my $lg_name = $cgi->param("lg_name") or "dfdsfsd";
>>> my $lg_psswd = $cgi->param("lg_password") or "dsfdsfsd";
>>>
>>> # if we came this far, user did submit the login form
>>> # so let's try to load his/her profile if name/psswds match
>>> if ( my $profile = _load_profile($lg_name, $lg_psswd) ) {
>>>
>>> $session->param("~profile", $profile);
>>> $session->param("~logged-in", 1);
>>> $session->clear(["~login-trials"]);
>>>
>>> return 3;
>>> }
>>>
>>> }
>>>
>>>
>>> sub _load_profile {
>>> my ($lg_name, $lg_psswd) = [at] _;
>>> local $/ = "\n";
>>> unless (sysopen(PROFILES, "profiles.txt", O_RDONLY) ) {
>>> die "Couldnt open profiles.txt: $!";
>>> }
>>> while (<PROFILES> ) {
>>> /^(\n|#)/ and next;
>>> chomp;
>>> my ($n, $p, $e) = split (/\s+/,$_) ;
>>> if ( ($n eq $lg_name)&& ($p eq $lg_psswd) ) {
>>> my $p_mask = "x" . length($p);
>>>
>>> return {username=>$n, password=>$p_mask, email=>$e};
>>> }
>>> }
>>> close(PROFILES);
>>>
>>> return undef;
>>> }
>>>
>>>
>>> sub deploy_page {
>>>
>>> $tt->process("form/form.html") || die $tt->error();
>>> }
>>>
>>> sub login_page {
>>>
>>> $tt->process("login.tmpl",\%tag) || die $tt->error();
>>> }
>>> ==============================================
>>>
>>> thanks for all the help !!
>>>
>>
>> NET-A-PORTER.COM Irresistible fashion at your fingertips
>>
>> ____________________________________________________________ __________
>>
>> CONFIDENTIALITY NOTICE
>> The information in this email is confidential and is intended solely for the
>> addressee. Access to this email by anyone else is unauthorised. If you are
>> not the intended recipient, you must not read, use or disseminate the
>> information. Any views expressed in this message are those of the individual
>> sender, except where the sender specifically states them to be the views of
>> Net a Porter Ltd.
>> Net A Porter Ltd is a company registered in England& Wales Number: 3820604
>> Registered Office: 1 The Village Offices, Westfield, Ariel Way, London, W12
>> 7GF
>> ____________________________________________________________ _________
>>
>> --
>> To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
>> For additional commands, e-mail: beginners-help [at] perl.org
>> http://learn.perl.org/
>>
>>
>>
>
>
>
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: unable to set cookie when using template toolkit
On Wed, May 4, 2011 at 9:23 AM, Agnello George <agnello.dsouza [at] gmail.com> w=
rote:
> In my script i am using template toolkit. I am trying to set the
> cookie in my browser but all that it does is =C2=A0it prints the followin=
g
> output on my broswer :
>
> Set-Cookie: CGISESSID=3Df032fc8982a1ae022c4f51baa3bc4143; path=3D/ Date:
> Wed, 04 May 2011 13:07:40 GMT Content-Type: text/html;
> charset=3DISO-8859-1
>
> here is my script , how do is set =C2=A0cookie
*snip*
> print "Content-type: text/html\n\n";
I'm not HTTP expert, but I do know that headers are supposed to end
with two new lines (technically, I think they're supposed to be
CRLFs). So effectively the HTTP response you're sending is:
<<<<
Content-type: text/html
[Output from $cgi->header(...)...]
[Whatever else you happen to output...]
>>>>
The user agent (e.g., browser) would only recognize the first
Content-type line as a header. The rest would be considered the
content, which the browser would do its best to interpret as HTML.
> my $tt =3D Template->new( INCLUDE_PATH =3D> "/var/www/html/template" ) ||
> die "template process failed:$!";
You probably mean ``or`` instead of ``||``. The result may be the
same, but technically the precedence is different so if you were
calling a subroutine that didn't terminate the program (like die does)
then the results might not be as expected.
--
Brandon McCaig <http://www.bamccaig.com/> <bamccaig [at] gmail.com>
V zrna gur orfg jvgu jung V fnl. Vg qbrfa'g nyjnlf fbhaq gung jnl.
Castopulence Software <http://www.castopulence.org/> <bamccaig [at] castopulence=
..org>
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: unable to set cookie when using template toolkit
>
> This may be because $evalue !=3D3 and therefore the if block does not exe=
cute.
> You need to send the header in all cases. Try changing your if block to:
>
> if ( $evalue =3D=3D 3 ) {
> =A0 =A0my =A0$cookie =3D $cgi->cookie(CGISESSID =3D> =A0$session->id);
> =A0 =A0print $cgi->header( -cookie=3D>$cookie );
> =A0 =A0deploy_page();
> }
> else {
> =A0 =A0print $cgi->header;
> }
yes this worked for me , thanks !!
--
Regards
Agnello D'souza
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/