php: prevent other users from saving data

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cjkeane
Forum Contributor
Posts: 217
Joined: Fri Jun 11, 2010 1:17 pm

php: prevent other users from saving data

Post by cjkeane »

hi everyone:

i have created a login script using sessions which works great. it displays the users sign on name on each page and i record their name when they save data into a mysql database.

i have an issue when two users (or more) save data at the same time. i have a page which allows users to transfer data from one table to another e.g. perform an insert from data from one table into another table, then delete the data from the original table. A problem occurs when two users have the same page open at the same time. If the first user transfers the data (the content then doesn't exist in the original table), then another user attempts to transfer the data, their name is inserted into the destination table with no other data as it doesn't exit in the original table. i need to prevent the second user from transferring data if it no longer exists. what is the best way to prevent this?

thanks.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: php: prevent other users from saving data

Post by AbraCadaver »

You should probably block anyone from opening that page if someone else has it open. When someone opens the page set a value in another DB table indicating that it is opened already and put a timestamp with it. Then if someone else tries to open it before a specified time after the timestamp (1 minute, 10 minutes, whatever) then don't let them. You should also check this again when performing the actual operation.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
cjkeane
Forum Contributor
Posts: 217
Joined: Fri Jun 11, 2010 1:17 pm

Re: php: prevent other users from saving data

Post by cjkeane »

I was thinking about a timestamp or even inserting the logged on username into a new field called 'InUseBy' but then the issue occurs where i'd need to remove their name once they move to another record.

1. i'm not sure how to insert either a timestamp or their username into the table when the link is clicked
2. i'm not sure how to remove the timestamp/username from the table when they move to another record so another user can edit/save the record.

any helpful ideas?
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: php: prevent other users from saving data

Post by McInfo »

Use transactions. See mysqli::commit().

To make a script wait for another script's transaction to complete, you can use SELECT ... LOCK IN SHARE MODE.
cjkeane
Forum Contributor
Posts: 217
Joined: Fri Jun 11, 2010 1:17 pm

Re: php: prevent other users from saving data

Post by cjkeane »

yes i did read about lock in share mode. what's your opinion on SELECT ... FOR UPDATE?
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: php: prevent other users from saving data

Post by McInfo »

I don't have a good answer at this time.
Post Reply