gpt4 book ai didi

php - 为 "remember me"功能设置 cookie 的最安全方法

转载 作者:行者123 更新时间:2023-12-04 21:05:51 31 4
gpt4 key购买 nike

我有兴趣在登录页面上编写一个记住我功能,该功能将在个人计算机上记录一个cookie多久。我知道这样做有危险,所以我想确保我以正确的方式做这件事。

我听说有人在数据库表和 cookie 之间创建链接以进行验证;但是,不确定如何执行此操作或是否建议这样做。我是 cookie 的新手,所以请解释一下最好的方法。我没有任何示例,因为就像我说的那样,我以前从未编写过此代码,并且希望确保我以正确的方式编写代码并从编写代码的人那里获得意见。

我不确定 facebook 的 cookie 是否永远存在,我只知道在我的计算机上我永远不需要重新登录,除非我删除我的历史记录。所以我可能想像他们那样做。

提前致谢!

最佳答案

我还没有为自己编写代码,但我会这样处理这个问题:

1. 创建一个表,当用户提供 cookie 以进行持久性时,该表可用于强制进行有效性检查:

create table RememberMe
(
user_id int(10) NOT NULL,
user_token char(10) NOT NULL,
token_salt int(6) NOT NULL,
time int(10) NOT NULL,
PRIMARY KEY (user_id),
CONSTRAINT nameYourConstraint
FOREIGN KEY (user_id)
REFERENCES userTableName (whatever_user_id_equals)
)

要填充此表,我将在登录名中添加一些代码行,在此示例中,我将使用伪代码

// userID variable has been sanitized already so
// check if user clicked remember me
// and if the user logged in successfully:
if ( rememberMe == checked && login() == true )
{
// random number to serve as our key:
randomNumber = random( 99, 999999 );

// convert number to hexadecimal form:
token = toHex( ( randomNumber**randomNumber ) );

// encrypt our token using SHA1 and the randomNumber as salt
key = encrypt( token, randomNumber, SHA1 );

// get the number of seconds since unix epoch:
// (this will be 10 digits long until approx 2030)
timeNow = unix_time()

// check to see if user is in table already:
sql = "SELECT user_id FROM RememberMe
WHERE user_id = 'userID'";

// connect to database:
db = new DBCon();

result = db->query( sql );

// number of rows will always be 1 if user is in table:
if ( result->rows != 1 )
exists = true;
else
exists = false;

result->free_memory();

if ( exists == true )
{
sql = "UPDATE RememberMe SET
user_id = 'userID'
user_token = 'token'
token_salt = 'randomNumber'
time = 'timeNow'";
}
else
{
sql = "INSERT INTO RememberMe
VALUES( 'userID', 'token', 'randomNumber', 'timeNow' )";
}

result = db->query( sql );

// the affected rows will always be 1 on success
if ( result->affected_rows != 1 )
{
print( "A problem occurred.\nPlease log in again." );
quit();
}

result->free_memory();

// create a new cookie named cookiemonster and store the key in it:
// (we're not actually storing a score or birthday, its a false flag)
set_cookie( "CookieMonster", escape("score="+ userID +"birthday="+ key );
}

此代码的作用是检查用户是否检查了记住我,并使用用户的键、 token 和盐以及时间填充数据库表(以便您可以对记住我功能)。

从这里您可以向您的网站添加代码,检查是否设置了 CookieMonster cookie,如果设置了,您可以按照以下步骤强制执行其有效性:

  1. extract the userID and the key from the cookie presented

  2. Query the database with userID to see if

       --> a) user has requested to be remembered

    --> b) check the time to see if they cookie is still valid

    --> c) extract the token and salt from database table record
  3. Run the token and salt through an encrypt() function call and match againstthe presented key.

  4. If everything checks out, create a new session and log the user in.

现在,每次用户访问您的网站时,他们都会登录,并且如果他们的计算机遭到入侵,攻击者将无法访问他们的密码

附注:您应该始终要求您的用户在更改密码或电子邮件时提供密码,这样如果用户的 cookie 落入坏人之手,您的攻击者将无法窃取该帐户。

关于php - 为 "remember me"功能设置 cookie 的最安全方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15454634/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com