in reply to Re: redirect and cookie problems
in thread redirect and cookie problems

thank you, but were in the code should i put this "CGI->header()"? i do not see that in my program at all you make it sound like I do. also do I have to add anything to the () portion of it to make the cookie work. Also wht goes inside the () portion of redirect? is it just the address of the page you want to direct them to.

Replies are listed 'Best First'.
Re: Re: Re: redirect and cookie problems
by Masem (Monsignor) on Jun 14, 2001 at 06:10 UTC
    Well, you need to use "$cgi->header(-cookie=>cookie_stuff)" as the first thing that you print out to get the cookies to be set.

    Redirect just needs the URL as the call.

    Look at the docs for CGI.pm for more details.


    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
Re: Re: Re: redirect and cookie problems
by LD2 (Curate) on Jun 14, 2001 at 06:15 UTC
    For redirection, you may want to read up on this: Generating a Redirection Header.

    Update: I think I was wrong there, I apologize.

    The redirect() function redirects the browser to a different URL. If you use redirection like this, you should not print out a header as well. As of version 2.0, we produce both the unofficial Location: header and the official URI: header. This should satisfy most servers and browsers - from CGI documentation

    Update 2: Depending on what you want to do - I most likely misunderstood. If you want to completely redirect the user - then these nodes will help. CGI and redirect and Trying to redirect ... - I apologize for not pointing to this earlier.. my brain isn't/wasn't working. You want to use Location - to redirect the user to a new site. But, if you just want to inform the user of the new location - then use URI: header - I believe you used that correctly:
    print URI: http://www.storm.prohosting.com/cgi-bin/warropr.pl;

      I do not under stand what should i change the redirect line to? or are you saying not to do a header() if so then I can not use cookies can I?
        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
      Might just be me being pedantic (as ever), but according to my copy of RFC2068 (HTTP/1.1); the "correct" method to do redirects is to return HTTP status 301, and to give the new URI in a Location header. The RFC goes on to indicate that you should also include a "short hypertext note" with a hyperlink to the new URI

      --
      RatArsed