faqts : Computers : Programming : Languages : PHP : Common Problems : Files : Tips and Tricks

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

5 of 24 people (21%) answered Yes
Recently 2 of 10 people (20%) answered Yes

Entry

How can I reset $PHP_AUTH_USER to log out a user?

Oct 30th, 2002 11:44
Niels Asmussen, http://www.faqts.com/knowledge_base/view.phtml/aid/456


I simply added a logout button to the page and had it link back to that
page, with a variable being set to logout. I then made the
authentication check to see if people were logging out, and if they were
force the authentication to turn up false, which pops up another login
window. This window won't work, and as long as they don't enter in
correct login information the PHP_AUTH_USER and PHP_AUTH_PW vars have
been reset for that realm and they're fine. It is a little dirty, but it
does work, just make sure to make it clear that they shouldn't enter in
their correct information, and instead to click cancel...

        if ($op == "logout") {
            if (mysql_num_rows(mysql_query($query)) == 2) {
                echo ("You have been logged in to the server!!!");
            } else {
                header('WWW-Authenticate: Basic realm="Private"');
                header('HTTP/1.0 401 Unauthorized');
                echo ("You are unauthorized to enter this area.");
                exit;
            }
        } else {
            if (mysql_num_rows(mysql_query($query)) == 1) {
                echo ("You have been logged in to the server!!!");
            } else {
                header('WWW-Authenticate: Basic realm="Private"');
                header('HTTP/1.0 401 Unauthorized');
                echo ("You are unauthorized to enter this area.");
                exit;
            }
        }

         echo ("
            <a href=\"$PHP_SELF?op=logout\">Log Out</A>
         ");