While your "round peg into a square hole" solution might work, you could actually just do it by the book and be done with it. Square peg, square hole and all that.

The gist of it is that you need a page or script that generates a "401 Unauthorized" error. This is a message to your browser that it should prompt for authentication, and then you can go back to doing whatever you wanted.

This can be done through CGI.pm using the standard header method, such as a script called "logout.cgi" which has the following statement inside it:
print $q->header ( -status => 401 );
When they run the script again, you should redirect them using $q->redirect() to the main page.

To figure out when this script has been run the first time (i.e. logout request) and the second (i.e. redirect request), you will have to track who ran it, and from where, using some method, which could be as basic as temporary files, or a database column if you have such a thing available.

One trick that I once used was something like this. The page had a link to "logout.cgi?sess=4190141041&page=/home" where the stuff on the end was some random number. The "logout program" looked something like:
use strict; use CGI; my $q = new CGI; my $lock_dir = "/tmp/lock"; my $other_url = my ($sess) = $q->param{sess} =~ /^(\d+)/; if (-f "$lock_dir/$sess.logout") { # Check for stale lock files here, too, # such as all those over 1 day old. # Remove this particular lock file unlink ("$lock_dir/$sess.logout"); print $q->redirect ($q->param{page}); } else { # Create a zero byte lock file for this session open (LOCK, ">$lock_dir/$sess.logout"); close (LOCK); # Return a refused message which causes the # browser to prompt for authentication. print $q->header ( -status => 401 ); }

In reply to Re: undef-ing $ENV{'REMOTE_USER'} from within Perl? by tadman
in thread undef-ing $ENV{'REMOTE_USER'} from within Perl? by hibernian

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.