Locking a detail view if a user is editing the item
I am developing a user manager which must control access to the detail view of editable items. At present, when a user clicks 'edit', the application queries the link table to check if a user is currently editing that page, if not, it allows access to the page and then inserts a record into 开发者_开发知识库the link table preventing another user from editing the same page at the same time.
My question is what would the best way to handle the removal of records if say a user exists the browser without saving etc, therefore no action to remove the record.
I have a couple of ideas but would like other input before I decide.
BenTheDesigner
A simple timeout. Have a field last_seen
in the table and update it with the current time when the user performs an action. Then, you can check if the last_seen
field is over 15 minutes or so old, delete the record.
Some pseudo-ish code:
edit_something() {
// Assume there's a lock
$is_editable = false;
// Check if lock exists
$q = mysql_query("SELECT * FROM linktable WHERE item_id = 2");
// If a lock exists
if(mysql_num_rows($q) > 0) {
// Check if it's timed out
$r = mysql_fetch_assoc($q);
// Delete lock if it's over 15 minutes old
if(time() - $r > 15 * 60) {
mysql_query("DELETE FROM linktable WHERE item_id = 2");
$is_editable = true;
}
} else {
$is_editable = true;
}
if($is_editable) {
// Lock for current user
mysql_query("INSERT INTO linktable SET item_id = 2, user_id = 5, last_seen = NOW()");
} else {
echo "Sorry, it's locked.";
}
}
精彩评论