什么是$_SESSION以及如何在PHP中保护SESSION名称


What is $_SESSION and how do i secure SESSION name in PHP

我的SESSION在成功登录后启动,我真的不知道我的SESSESSION值有多安全

我的SESSION值是user name,这是我的代码

<?php
require_once('includes/config.php');
if ($user->is_logged_in()) {
    header('Location: signup.php');
}
if (isset($_POST['submit'])) {
    $username = filter_input(INPUT_POST, 'username');
    $password = filter_input(INPUT_POST, 'password');
    if ($user->login($username, $password)) {
        $_SESSION['username'] = $username;
        header('Location: index.php');
        exit;
    } else {
        $error[] = 'Wrong username or password or your account has not been activated.';
    }
}
?>

这是我的功能

private function get_user_hash($username){  
        try {
            $stmt = $this->_db->prepare('SELECT password FROM members WHERE username = :username AND active="Yes" ');
            $stmt->execute(array('username' => $username));
            $row = $stmt->fetch();
            return $row['password'];
        } catch(PDOException $e) {
            echo '<p class="bg-danger">'.$e->getMessage().'</p>';
        }
    }
    public function login($username,$password){
        $hashed = $this->get_user_hash($username);
        if($this->password_verify($password,$hashed) == 1){
            $_SESSION['loggedin'] = true;
            return true;
        }   
    }

现在我需要知道我的$_SESSION 有多安全

以及如何确保我的$_SESSION值

首先,深入了解会话在PHP中的工作方式是很有价值的。当您运行session_start()时,PHP为当前访问者注册一个ID,并允许您开始存储/接收链接到会话ID的信息,例如:

session_start(); // Registered visitor ID on server "B3D4IUC86"
$_SESSION['password'] = "test1234"; // Stores password for visitor "B3D4IUC86"
echo $_SESSION['password']; // Retrieves 'password' index for visitor "B3D4IUC86"

最好不要将安全/未加密的数据直接存储在用户的机器上,因为这些数据可以在传输中被拦截或直接访问。相反,存储一个ID,您可以在机器上查找该ID并在内部查找信息。这也将(略微)提高网络传输速度,因为您不是在传输所有数据,而是只传输一个可以查找的简单ID。