2

私は、両方のセッションを使用するログイン システムを作成中です (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_hmacに頼ってから、ソルト + キーを sha512 して、それを Cookie として保存することができます.. .

私の全体的な質問は、私が提供したチャンクは、Cookie を介して自動ログインを処理するための半安全な方法であり、悪者がキーを操作して必要なデータを取得する可能性を最小限に抑えることができるということですか?

4

1 に答える 1