Loading ...
Sorry, an error occurred while loading the content.
 

Set Cookie through SSI

Expand Messages
  • Juni Adi
    Hello folks, This is my 1st time dealing with cookie. Basically I just want to know wether it s the first time a client accessing my index.shtml or they just
    Message 1 of 6 , Mar 4, 2002
      Hello folks,
      This is my 1st time dealing with
      cookie. Basically I just want to know wether it's
      the first time a client accessing my index.shtml
      or they just "refresh" the page. Thinking that it
      would be done through cookie thing, I wrote
      cookie.cgi that will be called from index.shtml
      through SSI.

      index.shtml looks like:
      <html>
      <!--#exec cgi="cgi-bin/cookie.cgi" -->
      </html>

      -----end of index.html----

      cookie.cgi looks like:

      #!/usr/bin/perl
      #cookie.cgi
      use CGI;
      $q=new CGI;

      #set cookie
      $me='skiper';
      $path='/';
      $cookie=$q->cookie(-name=>"user_name",-value=>$me, -path=>$path);
      print $q->header(-type=>'text/html',-cookie=>$cookie);

      #accessing cookie (if there is cookie)
      $user=$q->cookie(-name=>'user_name');

      if ($user){
      print "$user\n";
      }
      else{
      print "no cookie\n";
      }

      --- end of cookie.cgi----

      Now comes the problem.
      It seems like it failed to set cookie. When user
      "refresh" index.shtml page (that supposed to
      send back the cookie to the script), the script
      got no cookie at all.

      Then I try to call the script directly from
      URL. First time I call the script, I got "no
      cookie". Then "refresh" the page, and get the
      cookie printed out.

      It would be great if any of you could give me
      explanations.


      Regards
      Juni Adi
      www.arupa.or.id
    • b_harnish
      ... The problem is that when done through SSI, the header information (Content-type, length, ...) has already been sent to the browser, so your script can t do
      Message 2 of 6 , Mar 5, 2002
        --- In perl-beginner@y..., Juni Adi <skiper@a...> wrote:
        > Hello folks,
        > This is my 1st time dealing with
        > cookie. Basically I just want to know wether it's
        > the first time a client accessing my index.shtml
        > or they just "refresh" the page. Thinking that it
        > would be done through cookie thing, I wrote
        > cookie.cgi that will be called from index.shtml
        > through SSI.
        >
        > index.shtml looks like:
        > <html>
        > <!--#exec cgi="cgi-bin/cookie.cgi" -->
        > </html>
        >
        > -----end of index.html----
        >
        > cookie.cgi looks like:
        >
        > #!/usr/bin/perl
        > #cookie.cgi
        > use CGI;
        > $q=new CGI;
        >
        > #set cookie
        > $me='skiper';
        > $path='/';
        > $cookie=$q->cookie(-name=>"user_name",-value=>$me, -path=>$path);
        > print $q->header(-type=>'text/html',-cookie=>$cookie);
        >
        > #accessing cookie (if there is cookie)
        > $user=$q->cookie(-name=>'user_name');
        >
        > if ($user){
        > print "$user\n";
        > }
        > else{
        > print "no cookie\n";
        > }
        >
        > --- end of cookie.cgi----
        >
        > Now comes the problem.
        > It seems like it failed to set cookie. When user
        > "refresh" index.shtml page (that supposed to
        > send back the cookie to the script), the script
        > got no cookie at all.
        >
        > Then I try to call the script directly from
        > URL. First time I call the script, I got "no
        > cookie". Then "refresh" the page, and get the
        > cookie printed out.
        >
        > It would be great if any of you could give me
        > explanations.

        The problem is that when done through SSI, the header information (Content-type, length, ...) has already been sent to the browser, so your script can't do Set-Cookie.

        When you call the script directly, the header info has NOT been sent, and thus, your script can do Set-Cookie, and works fine.

        AFAIK, the only way to set a cookie after the header info has been sent is to use a clientside script (i.e. javascript).

        - Brian
      • Nguyen, David M
        What module do we use to mirror an entire website? Thanks, David
        Message 3 of 6 , Mar 6, 2002
          What module do we use to mirror an entire website?

          Thanks,
          David
        • Juni Adi
          Hello Brian, ... Yes, that s what I did after sending my email. Thanks for the explanations anyway. Juni Adi
          Message 4 of 6 , Mar 6, 2002
            Hello Brian,

            On Tue, 5 Mar 2002, b_harnish wrote:
            |
            |AFAIK, the only way to set a cookie after the
            |header info has been sent is to use a clientside
            |script (i.e. javascript).

            Yes, that's what I did after sending my email.
            Thanks for the explanations anyway.


            Juni Adi
          Your message has been successfully submitted and would be delivered to recipients shortly.