我正在创建一个使用两个会话的登录系统(对于那些不允许使用 cookie 的人(同意cookie 法..我正在使用网站http://www.cookielaw.org/the-cookie -law.aspx作为参考)
现在,我有这个系统用于我的 cookie 身份验证
function GenerateString(){
        $length = mt_rand(0,25);
        $characters = '0123456789abcdefghijklmnopqrstuvwxyz';
        $string = '';
        for ($p = 0; $p < $length; $p++) {
            $string .= $characters[mt_rand(5, strlen($characters) -1)];
        }
        return $string;
}
$RandomString = GenerateString();
$CookieAuth = $DB->prepare("INSERT INTO cookieauth (Username,RandomString) VALUES (?,?)");
$CookieAuth->bind_param('ss',$_POST['Username'],$RandomString); 
$CookieAuth->execute(); // Insert the Authentication Methods into the database 
$CookieAuth->close(); // Allow another query/statement
$GetInsertID = $DB->prepare("SELECT ID FROM CookieAuth WHERE RandomString=?");
$GetInsertID->bind_param('s',$Randomstring);
$GetInsertID->execute();
$GetInsertID->bind_result($RowID);
$GetInsertID->fetch();
$GetInsertID->close(); 
setcookie("Auth[ID]",$RowID);
setcookie("Auth[UName],$_POST['Username']);
setcookie("Auth[RandomString]",$RandomString);
然后处理cookie:
if(isset($_COOKIE['Auth'])){
   $Authenticate = $DB->prepare("SELECT Username,RandomString FROM cookieauth WHERE ID=?");
   $Authenticate->bind_param('i',$_COOKIE['Auth']['ID']);
   $Authenticate->execute();
   $Authenticate->bind_result($RowUsername,$RowString);
   $Authenticate->fetch();
   $Authenticate->close();
if ($_Cookie['Auth']['UName'] == $RowUsername){
    if ($_COOKIE['Auth']['RandomString'] == $RowString){
        header("Location: LoggedIn.php");
    }else{
        die("Possible Cookie Manipulation, Autologin Cannot Continue");
    }
}else{
    die("Possible Cookie Manupulation, Autologin Cannot Continue!");
}
我的总体目标是通过使用 cookie 提供自动登录功能。正如人们应该知道的那样,它们基本上以纯文本形式存储在硬盘驱动器上。所以如果我包含一个随机生成的字符串,每次进一步处理都会更改(然后更新 cookie 以匹配数据库)这是一种相当安全的方式完成任务?我的意思是,我知道这不是 100% 安全的,因为某些用户可能会尝试操纵随机字符串,所以我可以使用盐随机密钥,然后使用hash_hmacsha512 盐+密钥并将其保存为 cookie。 .
我的总体问题是,我提供的块是否提供了一种半安全的方法来通过 cookie 处理自动登录,并且可以最大限度地减少一些坏人操纵密钥以获取所需数据的可能性?
