CGI module & cookies

Discussion in 'Perl Misc' started by IanW, Jun 5, 2006.

  1. IanW

    IanW Guest

    Hi

    I know this is not strictly a Perl question (ie: more to do with the usage
    of the CGI module) but couldn't get anything from the ciwac group:

    I have a Perl script that facilitates the download of files in a non-web
    directory. when the follow the url to teh file they will first be presented
    with a login page. On that login page is an option to store the login
    details in a cookie. so asssuming they say yes to the cookie option the
    following code is activated:

    # store login details in cookie after manual login
    if($fd{store} eq "yes"){
    my $cookie = $query->cookie (
    -name => 'FALOGIN',
    -value => "$email-$password",
    -path => '/',
    -expires => '+1y'
    );
    print $query->header(-cookie => $cookie);
    }

    That works fine, except straight after that code is executed the script them
    attempts to send them the file:

    if(open(FH,"$fd{f}"){
    (my $filename = $fd{f}) =~ s/^.*\///;
    if($filename =~ /\.html*$/i){
    print "Content-type:text/html\n\n";
    elsif($filename =~ /\.jpg$/i){
    print "Content-type:image/jpeg\n\n";
    }
    else{
    print "Content-type: application/octet-stream\n";
    print "Content-disposition: attachment;
    filename=$filename\n\n";
    binmode FH;
    }
    print while <FH>;
    }

    So, the script decides what type of file it is and chooses the appropriate
    content type to send to the browser.

    However, it seems that the "print $query->header(-cookie => $cookie);" line
    includes a "print "Content-type:text/html\n\n";
    " because when I try to access a test document, I get something like this
    in my browser:

    "Content-type: application/octet-stream Content-disposition: attachment;
    filename=test.doc ÐÏࡱá>þÿ !#þÿÿÿ
    ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿì¥Á%
    ¿bjbjÞøÞø " ¼'¼'ÿÿÿÿÿÿ]'''''''¦¦¦¦¦ ²
    ¦U¶ÊÊÊÊÊÊÊÊ$"

    Is there a way round this? ie: saving the cookie without printing a
    content-type?

    Ian
     
    IanW, Jun 5, 2006
    #1
    1. Advertising

  2. IanW wrote:

    > I have a Perl script that facilitates the download of files in a non-web
    > directory. when the follow the url to teh file they will first be presented
    > with a login page. On that login page is an option to store the login
    > details in a cookie. so asssuming they say yes to the cookie option the
    > following code is activated:
    >
    > # store login details in cookie after manual login
    > if($fd{store} eq "yes"){
    > my $cookie = $query->cookie (
    > -name => 'FALOGIN',
    > -value => "$email-$password",
    > -path => '/',
    > -expires => '+1y'
    > );
    > print $query->header(-cookie => $cookie);
    > }
    >
    > That works fine, except straight after that code is executed the script them
    > attempts to send them the file:
    >
    > if(open(FH,"$fd{f}"){
    > (my $filename = $fd{f}) =~ s/^.*\///;
    > if($filename =~ /\.html*$/i){
    > print "Content-type:text/html\n\n";
    > elsif($filename =~ /\.jpg$/i){
    > print "Content-type:image/jpeg\n\n";
    > }
    > else{
    > print "Content-type: application/octet-stream\n";
    > print "Content-disposition: attachment;
    > filename=$filename\n\n";
    > binmode FH;
    > }
    > print while <FH>;
    > }
    >
    > So, the script decides what type of file it is and chooses the appropriate
    > content type to send to the browser.
    >
    > However, it seems that the "print $query->header(-cookie => $cookie);" line
    > includes a "print "Content-type:text/html\n\n";
    > " because when I try to access a test document, I get something like this
    > in my browser:
    >
    > "Content-type: application/octet-stream Content-disposition: attachment;
    > filename=test.doc [...]>
    > Is there a way round this? ie: saving the cookie without printing a
    > content-type?


    Sure. While CGI.pm offers an easy and reliable way to write cookies, it
    can be coded out by hand as well. Someting like this:

    -----------------------------------------------
    print <<"EndOfText";
    Set-Cookie: FALOGIN=abc; path=/; expires=Tue, 05-Jun-2007 09:39:13 GMT
    Date: Mon, 05 Jun 2006 09:39:13 GMT
    Content-type: application/octet-stream
    Content-disposition: attachment; filename=$filename

    [CONTENT GOES HERE]

    EndOfText
    -----------------------------------------------

    Hope this helps,

    --
    Bart
     
    Bart Van der Donck, Jun 5, 2006
    #2
    1. Advertising

  3. IanW

    IanW Guest

    "Bart Van der Donck" <> wrote in message
    news:...

    >> "Content-type: application/octet-stream Content-disposition: attachment;
    >> filename=test.doc [...]>
    >> Is there a way round this? ie: saving the cookie without printing a
    >> content-type?

    >
    > Sure. While CGI.pm offers an easy and reliable way to write cookies, it
    > can be coded out by hand as well. Someting like this:
    >
    > -----------------------------------------------
    > print <<"EndOfText";
    > Set-Cookie: FALOGIN=abc; path=/; expires=Tue, 05-Jun-2007 09:39:13 GMT
    > Date: Mon, 05 Jun 2006 09:39:13 GMT
    > Content-type: application/octet-stream
    > Content-disposition: attachment; filename=$filename
    >
    > [CONTENT GOES HERE]
    >
    > EndOfText
    > -----------------------------------------------
    >
    > Hope this helps,


    Yes it does thanks and just been looking at the CGI::Cookie docs which looks
    like it'll do the trick:
    http://search.cpan.org/~lds/CGI.pm-3.20/CGI/Cookie.pm

    Regards
    Ian
     
    IanW, Jun 5, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alex Nitulescu

    Response.Cookies vs Request.Cookies

    Alex Nitulescu, Feb 3, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    8,503
    Hans Kesting
    Feb 3, 2005
  2. _Who
    Replies:
    7
    Views:
    2,686
  3. fatted
    Replies:
    1
    Views:
    145
    Steve Grazzini
    Jul 25, 2003
  4. Mr I
    Replies:
    13
    Views:
    362
  5. shumsta
    Replies:
    1
    Views:
    274
    Fabian Pilkowski
    Jul 22, 2005
Loading...

Share This Page