ฉันยังไม่ได้เขียนโค้ดนี้ด้วยตัวเอง แต่ฉันจะแก้ไขปัญหาดังนี้:
1. สร้างตารางที่สามารถใช้เพื่อบังคับการตรวจสอบความถูกต้องเมื่อผู้ใช้แสดงคุกกี้เพื่อความคงอยู่:
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 );
}
สิ่งที่โค้ดนี้ทำคือตรวจสอบว่าผู้ใช้ได้ตรวจสอบการจำฉันแล้วหรือไม่ และมันเติมตารางฐานข้อมูลด้วยคีย์ โทเค็น และเกลือสำหรับผู้ใช้ตลอดจนเวลา (เพื่อให้คุณสามารถบังคับใช้ข้อจำกัดเวลาในการจดจำฉัน คุณสมบัติ).
จากที่นี่ คุณสามารถเพิ่มโค้ดลงในเว็บไซต์ของคุณเพื่อตรวจสอบว่าคุกกี้ CookieMonster ได้รับการตั้งค่าไว้หรือไม่ และ หากเป็นเช่นนั้น คุณสามารถทำตามขั้นตอนเหล่านี้เพื่อบังคับใช้ความถูกต้องได้:
แยกรหัสผู้ใช้และรหัสออกจากคุกกี้ที่แสดง
ค้นหาฐานข้อมูลด้วย userID เพื่อดูว่า
--> 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
เรียกใช้โทเค็นและเกลือผ่านการเรียกใช้ฟังก์ชัน encrypt() และจับคู่กับคีย์ที่นำเสนอ
หากทุกอย่างเรียบร้อยดี ให้สร้างเซสชันใหม่และเข้าสู่ระบบผู้ใช้
ตอนนี้ทุกครั้งที่ผู้ใช้มาที่ไซต์ของคุณ พวกเขาจะต้องเข้าสู่ระบบ และ ในกรณีที่คอมพิวเตอร์ของพวกเขาถูกบุกรุก ผู้โจมตีจะไม่สามารถเข้าถึงรหัสผ่านของพวกเขาได้
หมายเหตุด้านข้าง: คุณควรกำหนดให้ผู้ใช้ของคุณแสดงรหัสผ่านเสมอเมื่อเปลี่ยนรหัสผ่านหรืออีเมล วิธีนี้หากคุกกี้ของผู้ใช้ตกไปอยู่ในมือของผู้ไม่ประสงค์ดี ผู้โจมตีของคุณจะไม่สามารถขโมยบัญชีได้
person
Charles Addis
schedule
16.03.2013