Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^4: Cookie and Session

by devarishi (Initiate)
on Nov 03, 2011 at 18:44 UTC ( [id://935746]=note: print w/replies, xml ) Need Help??


in reply to Re^3: Cookie and Session
in thread Cookie and Session

I have removed that line and now I am printing nothing before the "header":

use CGI qw/:standard/; use CGI::Cookie; if ($ENV{'REQUEST_METHOD'} eq "POST") { read(STDIN, $request,$ENV{'CONTENT_LENGTH'}); } @parameters = split(/&/,$request); foreach $p (@parameters){ $p =~ s/\+/ /g; ($para,$value) = split(/=/,$p); $para =~ s/%([0-9A-F][0-9A-F])/pack("c",hex($1))/ge; $value =~ s/%([0-9A-F][0-9A-F])/pack("c",hex($1))/ge; $PAIRS{$para} = $value; } if($PAIRS{"login_id"} eq("admin") && $PAIRS{"login_password"} eq("go") +){ $login_cookie = new CGI::Cookie(-name=>'loginID',-value=>'a +dmin'); $password_cookie = new CGI::Cookie(-name=>'loginPassword',-valu +e=>'go'); print header(-cookie=>[$login_cookie,$password_cookie]); print "<script type='text/javascript'> window.location = 'work_with_cookies.pl'; </script>"; } else{ print "<script type='text/javascript'> <!-- window.location = 'admin_login.html'; //--> </script>"; }

Still it does not work. The "Set cookie" lines are displaying and the page is not being redirected as well. If I remove the "print" operator from the "header" statement then the page gets redirected but nothing appears thereon. I mean the cookies are not retrieved thereon.

Replies are listed 'Best First'.
Re^5: Cookie and Session
by aaron_baugher (Curate) on Nov 03, 2011 at 22:48 UTC

    If you're seeing the headers when you print them, then something is getting printed ahead of them. If your script isn't doing it, it's possible that your web server is sending something ahead of what your CGI script prints.

    Try fetching the page with a utility like wget, so you can see exactly what's being output. Another option is to talk directly to the HTTP server with telnet, entering a GET command and the Host header like below, followed by a blank line. That'll let you see exactly what your browser sees (assuming your server doesn't do any funny browser-header-specific stuff), with all headers first and then the text of the page.

    $ telnet my.server.com 80 Trying.... Connected to.... Escape character is '^]'. GET /cgi-bin/myscript.cgi HTTP/1.1 Host: my.server.com # output will come here
Re^5: Cookie and Session
by Anonymous Monk on Nov 04, 2011 at 02:31 UTC
      Sir, Your codes give this output:
      Content-Type: text/html; charset=UTF-8 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-U +S"> <head> <title>Untitled Document</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> </head> <body> <b>861183026.613647 Fri Nov 4 05:18:53 2011</b><table border="1" wi +dth="%100"><tr><td><ul></ul></td><td><div style="white-space: pre-wra +p; overflow: scroll;">$VAR1 = bless( { &quot;.parameters&quot; =&gt; [], &quot;use_tempfile&quot; =&gt; 1, &quot;.etab&quot; =&gt; 1, &quot;.charset&quot; =&gt; &quot;UTF-8&quot;, &quot;.elid&quot; =&gt; 1, &quot;.fieldnames&quot; =&gt; {}, &quot;.cookies&quot; =&gt; undef, &quot;param&quot; =&gt; {}, &quot;escape&quot; =&gt; 1, &quot;.header_printed&quot; =&gt; 1 }, 'CGI' ); </div></td></tr><tr><td>CGI::Cookie-&gt;fetch</td><td><div style="whit +e-space: pre-wrap; overflow: scroll;">$VAR1 = {}; </div></td></tr></table><h1>And now %ENV</h1><ul> <li><strong>USERPROFILE</strong></li> <ul> <li>C:\Documents and Settings\Default User</li> </ul> <li><strong>PSMODULEPATH</strong></li> <ul> <li>C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\</li> ... Output Truncated Here... ... <li><strong>TMP</strong></li> <ul> <li>C:\WINDOWS\TEMP</li> </ul> </ul> </body> </html>

      Is working with Cookies/CGI in Perl that much difficult a task?

        If you copied his script and you're seeing all that in your browser, then go back to what I said above: your server is sending headers ahead of what your CGI scripts are sending (and apparently encoding your output as well). You'll have to fix that before your CGIs can work at all. Your web server shouldn't send any headers when it runs a CGI; it should let the CGI take care of that.

        Try this extremely simple CGI script. If you see anything in your browser other than "Hello, world!", you have a server problem. You should NOT see the Content-type line.

        #!/usr/bin/perl use warnings; use strict; print "Content-type: text/html\n\n"; print "Hello, world!";

        Sir, Your codes give this output:

        Why do you think I would need to see it?

        Is working with Cookies/CGI in Perl that much difficult a task?

        Not if you have a basic understanding of the internet/computer programs

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://935746]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2024-04-23 11:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found