Here's a quick review of the HTTP protocol:

After the browser and web server have handshook over TCPIP, the first text that the server sends is the HTTP header, which usually is as simple as:

Content-Type: text/html
(Note that that it ends with "\n\n"; this indicates the end of the header). This block of text tells the browser that what will follow is HTML, and the browser should react to it as such; most browsers will render that HTML (obviously).

The header is the only point where you can tell the browser to do special operations unrelated to displaying the web page. This means that if you want to set cookies, they have to be in the header; attempting to send them anytime else will simply have the browser treat them as regular text in the page's text stream. So the HTTP header with a cookie being set will looking something like :

Content-Type: text/html Cookie: cookie-data-here
Alternatively, you may cause the page to be redirected to another site, basically having the browser immediately jump to the new page upon seeing the redirect header:
Redirect: http://www.my.other.url/
But note that Redirect and Content-Type are mututally exclusive; you can only use one or the other in the HTTP header.

Coming back to CGI, you print a standard HTTP header (optionally with cookies) by having the first thing that is printed out by your script being the $cgi->header( -cookie=>$cookie ) line. Alternatively, if you want to redirect, the first thing that your script must print out is $cgi->redirect( "http://my.other.url" ). Once you have printed either the header() or redirect() lines, any further printing of these lines will simply be treated as part of the HTML (or other data) stream that you are sending, as opposed to being interpreted as HTTP header data.

Note that you can set a cookie in the redirect() step just as easily as you can in the header() step.

What you are problem looking to do is redirect users that have validated their log in to a second page, with unvalidated users being given the login box. Here's psuedocode that describes one way to do it:

Create $cgi variable Check for existing and valid cookie --> if so, print $cgi->redirect() to new page, optionally refreshing t +heir cookie Check for CGI parameters from login form --> if valid, print $cgi->redirect() to new page, and create new cookie for them # At this point, if no valid cookie and no valid login, they will need + to relogin print $cgi->header(); print login form; print optional link to creating account
And that's it. Note that no matter what path they follow, the is only one call to either of redirect() or header().


Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain

In reply to Re: Re: Re: Re: Re: redirect and cookie problems by Masem
in thread redirect and cookie problems by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.