English 中文(简体)
PHP: HTTP Basic - 伐木
原标题:PHP: HTTP Basic - Log off

我要指出,如果有人在请求中发出“道歉”的话,那将自动带给一页“成功记录”。 如果客户试图在 but子或进入禁区,将再次要求吉大港山区。

我迄今所做的是:

例如:com/restricted/index.php:

<?php   
    session_start();

    if(isset($_GET[ logout ]))
    {
        unset($_SESSION["login"]);
        header("location: ../logout.php");
        exit;
    }

    if (!isset($_SERVER[ PHP_AUTH_USER ]) || !isset($_SERVER[ PHP_AUTH_PW ]) || !isset($_SESSION["login"]))
    {

        header("HTTP/1.0 401 Unauthorized");
        header("WWW-authenticate: Basic realm="Tets"");
        header("Content-type: text/html");
        $_SESSION["login"] = true;
        // Print HTML that a password is required
        exit;
    }
?>
// The rest of the page is then displayed like normal

用户成功访问example.com/logout.php,请访问example.com/restricted/index.php?logout。 当用户试图收回随机情况时,有时会两次要求吉大港定居地保有权认证(吗?) ,有时会经常要求认证(?) ,有时会让我回头来,就像我从来不gged。

我是裁谈会工作的新鲜事,但我的理解是: 如果验证人,则在称为标识的届会上储存一个变数...... 如果每项申请都附有标书,它将删除该届会议的变量,并重新登录。 ...... 当我点击指数时,为什么会让我回头来,而不必要求认证,因为据说会议没有确定。

赞赏对这一购买力平价代码的任何改进。 我知道我应该使用《吉大港山区基本计划》,它应当包含在内,但我知道。 这是一个临时解决办法。

Edit:如果列入附有指示的例子,我将接受与MySQL的解决办法。 我没有MySQL或PHP数据库知识(yet)。

最佳回答

start:

<?php   
  session_start();

  if( isset( $_GET[ logout ] ) )
  {
    session_destroy();
    header( Location: ../logout.php );
    exit;
  }

  if( !isset( $_SESSION[ login ] ) )
  {
    if( !isset( $_SERVER[ PHP_AUTH_USER ] ) || !isset( $_SERVER[ PHP_AUTH_PW ] ) )
    {
      header("HTTP/1.0 401 Unauthorized");
      header("WWW-authenticate: Basic realm="Tets"");
      header("Content-type: text/html");
      // Print HTML that a password is required
      exit;
    }
    else
    {
      // Validate the $_SERVER[ PHP_AUTH_USER ] & $_SERVER[ PHP_AUTH_PW ]
      if( $_SERVER[ PHP_AUTH_USER ]!= TheUsername 
          || $_SERVER[ PHP_AUTH_PW ]!= ThePassword  )
      {
        // Invalid: 401 Error & Exit
        header("HTTP/1.0 401 Unauthorized");
        header("WWW-authenticate: Basic realm="Tets"");
        header("Content-type: text/html");
        // Print HTML that a username or password is not valid
        exit;
      }
      else
      {
        // Valid
        $_SESSION[ login ]=true;
      }
    }
  }
?>
// The rest of the page is then displayed like normal
问题回答

我找到了一条路。

页: 1

页: 1 法典:

# 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( <br /><br />
    <div style="text-align:center;">
       <h1 style="color:gray; margin-top:-30px;">Need authorization</h1>
    </div> );
}

这里是我的logout.php。 法典:

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

您可使用最短的答复时间(例如content=”1"。 这一更新将显示任何<代码>_POST。

if ( !isset($_SERVER[ PHP_AUTH_USER ]) || $_SERVER[ PHP_AUTH_USER ]!= myusername  || $_SERVER[ PHP_AUTH_PW ]!= mypassword  || isset($_POST[ logout ]) ) {
    header( WWW-Authenticate: Basic realm="My protected area" );
    header( HTTP/1.0 401 Unauthorized );
    echo  <html><head><title>401 Unauthorized</title><meta http-equiv="refresh" content="1"></head><body><h1>401 Unauthorized</h1><p>You are not allowed to see this page. Reload the page to try again.</p></body></html> ;
    exit();
} 




相关问题
why the session in iis automatically log out?

I used iis6, and when i called a function Directory.delete(), the all the session relate this website will be logged out. and i debugged the website, i found no exception. any one have ideas on this ? ...

Check session from a view in CodeIgniter

What is the best way to check session from a view in CodeIgniter, it shows no way in their user guide, otherwise I will have to make two views on everything, which is kinda weird...still a newbie to ...

Can I get the size of a Session object in bytes in c#?

Is it possible to get the size(in bytes) of a Session object after storing something such as a datatable inside it? I want to get the size of a particular Session object, such as Session["table1"], ...

提供严格分类的出席会议物体

提供严格分类的与会机会的最佳方式是什么? 我正计划转而选择矩阵,这正在促使汇编者抱怨我的幻觉方案拟订方法......

PHP Session is not destroying after user logout

I m trying to create an authentication mechanism for my PHP Application and I m having difficulty destroying the session. I ve tried unsetting the authentication token which was previously set within ...

热门标签