You're looking at the problem from the wrong perspective. The problem is not that the user can submit incorrect values. The problem is that your server-side code assumes that the values are correct.
the hidden fields can edit in Inspect Source in Web browsers by every body
Yes, this is true. Users can also craft any request they want to your server entirely outside of a web browser. It's fundamentally true that any client can send any request to your server at any time.
Rather than try to fight this, simply account for it. Ask yourself...
Why shouldn't User X be allowed to submit Value Y in this field?
As an example, perhaps that value/record belongs to a different user and this user shouldn't be able to edit other users' records. Then that's exactly what you should be validating server-side.
Expressed as pseudo-code:
if (!CurrentUser->canEdit($_POST[id_Hidden])) {
die "You are not allowed to edit this record.";
}
// continue editing the record here
So then what is CurrentUser
? That depends on how you track who the current user is. (Not included in the question.) However you track your logged-in users, refer to that information. And what is canEdit()
? That depends on how you determine whether any given user is permitted to edit any given record. You'll have to write that logic.
Please note that the above is pseudo-code and you won't be able to just copy/paste it as-is and expect it to work. But hopefully it's illustrating the overall point. To put it simply:
When a user attempts to perform an action, first determine if the user is allowed to perform that action. If they are not, return an error. If they are, perform the action.
That's really all there is to it. Never implicitly trust information from the client. Always validate that the user is authorized to do what they're trying to do.
Side note, following all of the other advice posted in this question/answers so far...
Sessions are not needed for this. And relying on them for this will be extremely limiting and overly complex as your application needs grow and as application complexity grows.
There's absolutely nothing wrong with the client providing "hidden" values that the server uses. Indeed, this allows your services to be much more RESTful and much simpler to use and maintain. All you need to do is validate those values server-side before using them.