title: Perl CGI redirect after cancelling on a 401
body: <p>I've currently got a small script running that sends a 401 to the client, upon cancelling and not providing user details the script will return nothing.</p>
  
  <p>I'd like to send a redirect to the page they have come from instead.</p>
  
  <p>The main subroutine looks like this;</p>
  
  <pre><code>#!usr/bin/perl
  use strict;
  use CGI;
  
  sub checkAuth {
    my ($user, $pass) = &amp;getAuthUsers(); # Get the user and pass of already authenticated users.
    unless ($user) {
      &amp;sendAuthenticationHeader(); # Send 401
    }
  
    # Check user against DB and return 1 for success.
    if ( &amp;checkUser($user, $pass) eq 'Y') { return 1 };
    else { # This is the redirect I'm trying to issue.
      my $cgi = CGI-&gt;new();
      print $cgi-&gt;redirect($ENV{HTTP_REFERER}); # Redirect to the referer url
      exit;
    }
  }
  </code></pre>
  
  <p>Unfortunately whenever I try to send new headers it's just received as plain text.</p>
  
  <p>Any help is appreciated, thanks in advance.</p>