Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

CGI and Redirect/URI/Location

by LeGo (Chaplain)
on Jun 18, 2001 at 07:13 UTC ( [id://89222]=perlquestion: print w/replies, xml ) Need Help??

LeGo has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to solve something simple. I have worked on it and tried to find the answer in many places. Such as CGI redirect, Redirect Problem, and of course, CPAN CGI::Cookie and CGI

Here is the code that I am using. I am trying to set a cookie then move along to another page...

$c = new CGI::Cookie(-name => 'login', -value => $login2, -expires => '+3M', -domain => '$domain', ); print header(-cookie=>[$c]); print "URI:http://$URL/"; #and tried print "Location:http://$URL/";

This is the snippet from my .pl that does not work. Any suggestions would help. I can set the cookie by itself and that works. I want the page to refresh after doing so though and I haven't figured out how to do that. I feel like I have tried them all, and know I haven't. LeGo

Replies are listed 'Best First'.
Re: CGI and Redirect/URI/Location
by btrott (Parson) on Jun 18, 2001 at 07:17 UTC
    The redirect has to go in the header, and after you've called 'header', you've already sent the header entirely. So you need to send the cookie as part of the redirect. And you can do that using the 'redirect' function/method:
    print redirect(-cookie => $c);
    If you have a CGI object, use
    print $cgi_object->redirect(-cookie => $c);
Re: CGI and Redirect/URI/Location
by Zaxo (Archbishop) on Jun 18, 2001 at 07:29 UTC

    You might try:

    # cookie stuff precedes use CGI; my $cgi = new CGI; $|=1; print $cgi->redirect(-cookie=>[$c], -uri=>"http://$URL/", # and possibly -nph=>1, );

    After Compline,
    Zaxo

      Just curious as to what difference $| being set to 1 makes in this code.

      LeGo

        That sets autoflush on STDOUT, meaning that 'print "foo";' is sent promptly. In most cases your script will end right after the redirect call, but for long-running scripts it's a courtesy to the client.

        After Compline,
        Zaxo

Re: CGI and Redirect/URI/Location
by Hero Zzyzzx (Curate) on Jun 18, 2001 at 18:13 UTC

    The italics below are my emphasis.

    From the "Creating the HTTP Header" section of the CGI.pm docs:

    Creating the Header for a Redirection Request

       print $query->redirect('http://somewhere.else/in/the/world');

    This generates a redirection request for the remote browser. It will immediately go to the indicated URL. You should exit soon after this. Nothing else will be displayed.

    You can add your own headers to this as in the header() method.

    You should always use absolute or full URLs in redirection requests. Relative URLs will not work correctly.

    An alternative syntax for redirect() is:

    print $query->redirect(-location=>'http://somewhere.else/', -nph=>1);

    So, you'd create your header like this:
    print $q->redirect(-location=>"http://$URL",-cookie=>$c);

      here is where I am now...
      This works in Netscape but not IE?
      $c = new CGI::Cookie(-name => 'login', -value => $login2, -expires => '+3M', ); $d = new CGI; print $d->redirect(-cookie=>$c, -location=>"$URL");
      This works in IE but not Netscape?
      $c = new CGI::Cookie(-name => 'login', -value => $login2, -expires => '+3M', -domain => '.mydomain.com' ); $d = new CGI; print $d->redirect(-cookie=>$c, -location=>"$URL");
      I didn't think that the domain should make a difference for IE but it did. So I tried adding -nph=>1 to both $c and $d seperately. When I added it to $c it did not do anything. When I added it to $d it gave an Internal Service Error.

      Are there any comments or suggestions that I might be missing on my code here? I would like for it to work in both IE and Netscape obviously.

      Also anyone know why netscape takes about 5 seconds to process and IE doesn't take any time?

      LeGo

        I'm confused. You seem to be mixing function and OO calls to CGI. I'm not sure if this is causing your problem, but it might. I've never experienced issues with cookies working with one browser and not the other.

        Some things to try:

        1. Use all OO calls to CGI, thusly:
          my $q = new CGI; my $c = $q->Cookie(-name => 'login', -value => $login2, -expires => '+3M', -domain => '.mydomain.com' ); print $q->redirect(-cookie=>$c, -location=>$URL);
        2. I doubt this will mean anything, but I don't think you need the double quotes around $URL.
        3. You can only set a cookie for the domain that the script was requested from. . .as a security measure. If your domain is "foo.com" you can only set a valid cookie for this domain. I think you're aware of that, given the way you set your domain in your cookie. So if your redirect is to a different domain, you can't set a cookie for that domain, only the old one.

        Good Luck!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (6)
As of 2024-04-19 18:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found