[英]what is the best way to design mysql tables which has relation to each other?
[英]Activator link: Which the best way to design?
我正在使用PHP,我知道這個網站上對此存在許多類似的問題。 但是我找不到關於以下幾點的很好的解釋:
現在,以為我只有以下選項來設計良好的激活器鏈接:
在用戶表中存儲一個唯一令牌,並使用兩個參數來驗證帳戶激活:用戶標識符和您在數據庫中的相應令牌。 可能是這樣的:
$user->token = bin2hex(openssl_random_pseudo_bytes(16));
hostname/accountConfirm?user=username&token=userUniqueToken
僅使用一個具有散列的秘密值+用戶電子郵件字符串的參數作為令牌。 此選項不需要任何數據庫列來存儲令牌。 像這樣:
$secret = 'some_secret_string';
$hashedToken = password_hash( $secret . $user->email, PASSWORD_DEFAULT );
hostname/accountConfirm?token=($hashedToken)
我的問題:
這些有哪些優缺點?
如果有,還有什么其他好的解決方案?
與未經身份驗證的用戶相比,訪問數據庫比不訪問數據庫的風險更大。 通過使用基於散列的消息身份驗證代碼(HMAC),可以避免在此步驟中訪問數據庫。 這也減慢了腳本類嘗試填充數據庫的速度。
因此,我喜歡您的第二個選擇,但是您仍然需要將電子郵件地址/用戶名保留在某個地方。
這是我用過的(偽代碼):
$email = 'user@example.com';
$expires = current time + 24 hours;
$userIP = getUserIP();
$hmac = hmac_function( $serverSecret, $email + $expires + $userIP );
//correctly escaping query params of course...
$confirmationURL = https://hostname/accountConfirm?email=$email&expires=$expires&ip=$userIP&hmac=$hmac
將網址發送到$ email
在accountConfirm處理程序中:
$email = $_GET['email'];
$expires = $_GET['expires'];
$userIP = $_GET['ip'];
$hmac = $_GET['hmac'];
$expectedHmac = hmac_function( $serverSecret, $email + $expires + $userIP);
if($expectedHmac != $hmac) {
log 'evil request';
throw 400 error;
}
//check user ip
//check expiry time
//proceed with registration
如果有人弄亂了IP,電子郵件地址或有效時間,則HMAC將不匹配。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.