php - 使用 session 的高级用户身份验证

标签 php session authentication

我正在建立一个拥有 3 种类型用户的网站:

-those who are registered and are subscribed for current month
-those that are registered, but are not subscribed for current month
-users that are not registered (you cant be subscribed if you are not regitered)

我已经创建了识别这 3 类用户并采取适当行动的代码。我的问题是,这是要走的路吗?我以前从未做过类似的事情。或者我应该重新规划我的方法?

//登录.php

//connect to database and see if a user and password combination exists. Store $exists=0 if not, and $exists=1 if it exists.
session_start();

$conn = new mysqli($hn,$un,$pw,$db);
if ($conn->connect_error){
    die($conn->connect_error);
}
$query = "SELECT COUNT(1) as 'exists',expiration_date FROM table WHERE email = ? AND password = ?;";
$stmt = $conn->prepare($query);
$stmt->bind_param("ss", $email, $password);

$email = $_POST["email"];
$password = hash("hashingalgorithm", "salt".$_POST["password"]."salthere");

$stmt->execute();
   /* Get the result */
$result = $stmt->get_result();
$num_of_rows = $result->num_rows;
$row = $result->fetch_assoc();
$exists = $row["exists"];
$expiration_date = $row["expiration_date"];

/* free results */
$stmt->free_result();

/* close statement */
$stmt->close();
$conn->close();

date_default_timezone_set('Europe/Berlin');


if ($exists==0){
    echo "Wrong email or password";
    $_SESSION['loginerror'] = 2;
    header('Location: https://www.homepage.com/login'); 
}else if ($exists){
    if (strtotime($expiration_date) < (strtotime("now"))){//logged in, but not subscribed
        session_destroy();
        session_start();
        $_SESSION["authenticated"] = true;
        header('Location: https://www.homepage.com');
    }else{//logged in and ready to go
        $_SESSION["authenticated"] = true;
        $_SESSION["email"] = $email;
        header('Location: https://www.homepage.com');
    }
}else{
    echo "An error with has occured.";
} 

然后在我网站的每个页面上我都使用此代码,以查看访问过我的用户类型

session_start();
if(isset($_SESSION["authenticated"]) && isset($_SESSION["email"])){ 
    $email = $_SESSION["email"];

    //connect to database and fetch expiration_date for a user with $email. Store it in $expiration_date

$conn = new mysqli($hn,$un,$pw,$db);
    if ($conn->connect_error){
        die($conn->connect_error);
    }
    $query = "SELECT expiration_date FROM table WHERE email = ?;";
    $stmt = $conn->prepare($query);
    $stmt->bind_param("s", $email);

    $email = $_SESSION["email"];
    $stmt->execute();
    /* Get the result */
    $result = $stmt->get_result();
    $num_of_rows = $result->num_rows;
    $row = $result->fetch_assoc();
    $expiration_date = $row["expiration_date"];
    /* free results */
    $stmt->free_result();
    /* close statement */
    $stmt->close();
    $conn->close();
    date_default_timezone_set('Europe/Berlin');

    if (strtotime($expiration_date) < (strtotime("now"))){//logged in, but not subscribed
        session_destroy();
        session_start();
        $_SESSION["authenticated"] = true;
        header('Location: https://www.homepage.com');
    }else{  //html for subsribed and registered user
    echo <<<_END
    //html here
    _END;
    }
}else if(isset($_SESSION["authenticated"]) && !isset($_SESSION["email"])){
        // user is logged in, but not subscribed;
        echo <<<_END
        //htmlhere
        _END;
}else{// user is not registered nor is subscribed
        echo <<<_END
        //htmlhere
        _END;
}

该代码有效,但我担心一旦用户注册并订阅,就会在每个页面上访问数据库。我实际上是在惩罚注册和订阅的用户。 是否有更好的性能明智的方法来处理此类问题?

最佳答案

这里的解决方案是检查订阅用户(仅在他们登录您网站的第一页)即在 login.php 中,您可以使用,

// ...your previous code
session_start();
// initialize session variables
$_SESSION['subscribe_date'] = $_SESSION['authenticated'] = false;
if ($exists == 0){
    echo "Wrong email or password";
    $_SESSION['loginerror'] = 2;
    header('Location: https://www.homepage.com/login'); 
} else if ($exists){
    $_SESSION["authenticated"] = true;
    if (strtotime($expiration_date) > (strtotime("now"))){ //logged in,& subscribed
        $_SESSION["email"] = $email;
        $_SESSION['subscribe_date'] = $expiration_date;
    } else {  //logged in and not subscribed, do nothin!
    }
    header('Location: https://www.homepage.com');
} else {
    echo "An error has occured.";
} 

然后,在其他所有页面上,您只需检查 $_SESSION['subscribe_date'] 而不是每次都触发查询

if(!empty($_SESSION["subscribe_date"]) && strtotime($_SESSION["subscribe_date"]) > (strtotime("now"))) {
 // html for subscribed and registered user
} else if (!empty($_SESSION["authenticated"])) {
 // html for registered user
} else {
 // html for unregistered user
}

另外,请注意我已经删除了 session_destroy(); 在每个页面上调用它是一个非常糟糕的主意。如果需要,您可以取消设置 session 变量。即 unset($_SESSION["email"]); 您应该仅在用户注销结束时调用 session_destroy();。查看 session_destroy() 的警告部分

关于php - 使用 session 的高级用户身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42378537/

相关文章:

php - native 准备语句 : are they so limited?

javascript - 在 javascript 中安全地存储数据(自动建议)

javascript - 兹科斯 : How to notify user of session timeout or lost connection to server?

node.js - 如何访问 socket.io 事件中的快速 session ?

linux - linux上的登录时间问题

php - 在 Python 中进行多个字符串替换的最快实现

php - 如何通过包含 & 符号和/或换行符的 ajax 传递 textarea 数据?

php - 无法让 php 设置 session

sql-server-2005 - 在 MS-SQL Server 中,如何将数据库用户从 WithoutLogin 更改为与登录名关联

azure - 尝试使用 Azure AD 登录时出现错误 AADSTS65001