4

I made multi user application in which if two user open the same edit page and at the same time both user enter data in the form there would be conflict data so I need a solution like if one user open particular edit page then all other user block for some time and prompt a message to them like" other user edit the same page...."

Roshan Dudhat
  • 41
  • 1
  • 2
  • They edit something PHP side (a file?), or the content of the page itself ? – Clément Malet Sep 12 '14 at 09:44
  • For example: If a users clicks edit a column like `page_edit` should get 1 instead of 0. when the user is done editing return the 1 abck to zero. – SuperDJ Sep 12 '14 at 09:45
  • 1
    When you edit a page, store the data of that page in a variable. When you're about to save, just query the database, if the data has been changed already. If it did, it should show a warning like: This page has already been edited by someone else. – Tibor B. Sep 12 '14 at 09:45

5 Answers5

4

Maybe,

you could add two columns in your database 'open' and 'time'

Set 'open' to 'true' or 'false' if someone is editing it. Also set a timestamp to the 'time' column. Replace the timestamp every so many seconds while editing takes place.

If someone else opens it, check the 'open' column and of it's 'true', calculate the time passed from the 'time' column. If it's over a certain time (say 2 or 3 minutes), assume the other user isn't editing anymore and allow this user to edit.

To make things clear,

Option 1:

  • a user clicks on edit
  • check the 'open' column, if it's 'false', set it to 'true' and add the timestamp to 'time'
  • As long as the user is typing in the inputfield, every 10 seconds or so, an AJAX call is made to update the 'time' column with the present timestamp
  • user clicks 'save', the 'open' column is set to 'false'. -

Option 2:

  • a user clicks on edit
  • check the 'open' column, if it's 'true'n check the 'time' column.
  • if the difference with the current timestamp is more than, lets say, 2 - 3 minutes, allow editing for this user and update the timestamp.
Wezy
  • 665
  • 1
  • 5
  • 14
  • 1
    With your edit, you're still forcing some arbitrary time to pass. let's say i open the edit and i'm writing a very long string, 5 minutes passed and i'm still typing, why should the file change to "closed"? – Patrick Sep 12 '14 at 09:54
  • @Patrick It would keep updating the 'time' column with the current timestamp every, let's say 10 seconds or so, through an AJAX call while there's action in the textbox. – Wezy Sep 12 '14 at 09:56
  • Ajax is the real answer to this question(or websockets), but you did not state it in your answer. At least my in opinion. – Patrick Sep 12 '14 at 09:57
  • Which is basically the answer I've posted, I'd remove option 2, it's not really an option. but the ajax/websocket way is the only way to go – Patrick Sep 12 '14 at 10:10
1

Best and easiest way to do it is to keep a live connection between the website and the editing user after he clicks edit.

So the basic steps :

1)user clicks edit.
2)initiate either ajax or websocket connection with that user to tell the server the file is still being edited
3)if user closes manually or just leaves the website, have some logic to deal with it server side
4)profit.

The continous ajax calls to the server(like a chat applciation) can update a "time_last_edited" column, if that column is, say, more than 10 seconds old, file is safe to edit, else it means someone is still editing it.

Patrick
  • 3,289
  • 2
  • 18
  • 31
0

Give the content a modified time. Before the new content is submitted, check the time is the same as it was before. If it is different, then the content has changed and the user should be informed.

Gets around most edge cases.

Flosculus
  • 6,880
  • 3
  • 18
  • 42
  • Problem is that if you open a file for edit, and leave your computer for 5 minutes, the file is still open for you and I can open it aswell, resulting in two people editing the same file – Patrick Sep 12 '14 at 09:52
  • That all depends on how much you care about your users. What remains intact is your systems integrity. – Flosculus Sep 12 '14 at 09:56
  • It's also a bit of UX, if I wish to prevent users from wasting their time editing a file that's already opened somewhere else:) – Patrick Sep 12 '14 at 09:56
  • You could use a diff tool afterwards allowing users to merge their changes. There are a few PHP implementations out there. – Flosculus Sep 12 '14 at 09:58
  • Assuming that's what you want in the first place. Not exactly what the OP requested. Ofcourse you could run a git-like version tracker, But that doesn't solve the OP question. – Patrick Sep 12 '14 at 09:59
  • We had a simpler question before, http://stackoverflow.com/questions/21046733/how-can-check-if-page-is-being-already-accessed/21046961#21046961 our answers revolved around determining idle time. Didn't want to give the same solution twice :P – Flosculus Sep 12 '14 at 10:11
  • @Flosculus Flag as duplicate then :) – Clément Malet Sep 12 '14 at 10:14
0

Have a field in the table such as Page_status and set it as locked if a user opens it for editing. and set it to unlocked if editing is completed. And each time check the status before allowing each users to edit.

If the user closes the page without editing. then have session for each users and set time limit and if the session expires then set the status to unlocked.

  • So you also need to keep track of which user edited the file, so you can then check if that specific user's session expired, you're also forced to store sessions in a DB to check for expiry. – Patrick Sep 12 '14 at 09:56
0

For this you have to keep flag in table, if suppose you have table name "userdetails" with columns such as name, mobile, email, address so now you add two more column name

inused with datatype TINYINT with default value 0 inuseddatetime with datatype datetime with default value NULL

So as soon as particular user open details for edit update inused column to 1 and inuseddatetime column with that currendt date and time

So wen other user open same detail for edit and if the column has value 1 show them prompt.

In order to again change 1 to 0, when first user click submit button again update inused column to 0 and inuseddatetime column to null

In some case if user do not press submit button and leave page so to avoid such condition you can write cron job on server which will check inuseddatetime column and will update inused column to 0 for column which are ideal from last 10 min or 5 min