PHP - HTTP认证或$_SESSION认证用户


PHP - HTTP Authentication or $_SESSION to authenticate user?

在登录表单上验证用户的HTTP身份验证和$_SESSION之间有什么区别?

HTTP,

<?php
if(!isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW']) || $_SERVER['PHP_AUTH_USER'] !== 'demo' || $_SERVER['PHP_AUTH_PW'] !== 'demo') {
    header("WWW-Authenticate: Basic realm='"Secure Page'"");
    header("HTTP' 1.0 401 Unauthorized");
    echo 'No soup for you';
    exit;
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Basic HTTP Authentication</title>
</head>
<body>
<h1>Secure Page</h1>
<p>This is a page with secure content...</p>
</body>
</html>

,

ession_start();
if( isset($_POST['username']) && isset($_POST['password']) )
{
    if( auth($_POST['username'], $_POST['password']) )
    {
        // auth okay, setup session
        $_SESSION['user'] = $_POST['username'];
        // redirect to required page
        header( "Location: index.php" );
     } else {
        // didn't auth go back to loginform
        header( "Location: loginform.html" );
     }
 } else {
     // username and password not given so go back to login
     header( "Location: loginform.html" );
 }

哪个更安全?

我会选择PHP版本,因为它更安全,更容易使用。