PHP: HTTP Basic - Log off

前端 未结 3 957
一生所求
一生所求 2021-01-06 04:25

I would to set it up where if someone sends in a request \"logout\" it will automatically take them to a page saying \"successful log out\". If the customer tries to press t

3条回答
  •  佛祖请我去吃肉
    2021-01-06 04:56

    I've found a way around it.

    I have 2 files: index.php and logout.php

    Here is my 'index.php' code:

    # CHECK LOGIN.
    if (!isset($_SESSION["loged"])) {
        $_SESSION["loged"] = false;
    } else {
        if (isset( $_SERVER['PHP_AUTH_USER'] ) && isset($_SERVER['PHP_AUTH_PW'])) {
            if (($_SERVER['PHP_AUTH_USER'] == L_USER) && (md5($_SERVER['PHP_AUTH_PW']) == L_PASS)) {
                $_SESSION["loged"] = true;
            }
        }
    }
    if ($_SESSION["loged"] === false) {
        header('WWW-Authenticate: Basic realm="Need authorization"');
        header('HTTP/1.0 401 Unauthorized');
        die('

    Need authorization

    '); }

    And here is my 'logout.php' code:

    session_start();
    $_SESSION["loged"] = false; // We can't use unset($_SESSION) when using HTTP_AUTH.
    session_destroy();
    

提交回复
热议问题