I have a strange situation and I'm hoping someone might have experienced this before and know what possible scenarios might cause this to occur.
I have a PHP website which accesses data from an SQL database. Users fill out a form to add or edit data which is POSTED and the data is written to the database. If the user is editing existing data, the php loads the data needed for the form, and I use JavaScript to fill the data into the appropriate fields once the page has loaded.
All of this works correctly for me on all the machines I've tested, and most users.
HOWEVER... SOME users are reporting that they are not seeing the correct data in their forms, but rather outdated information. Thus, when these users "edit" a form, rather than seeing the information that is currently contained within the database, they see old information.
My understanding is that (normally) php files aren't cached since they are executed on the server and then the final product is being handed to the user. Is there any situations which might cause a certain user to somehow cache these "assembled" files once they are received from the server and then reloaded the next time the page loads?
Example:
Today a client was trying to edit the information using the form. They called me because a field was showing something incorrect.
I logged into my site and loaded the same form. My form was showing the correct data (as contained within the SQL database), while theirs was showing different data.
Two different users looking at the same php webpage, and same form, using the same browser (chrome), which should be displaying the same data, but is not.
I'm baffled as to how to trace this issue and fix it.
CodePudding user response:
Perhaps the browser is caching the page?
To ensure no cache for the browser, please try it in PHP
<?php
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
?>
CodePudding user response:
Just like you mentioned that PHP executed on the server so they will not be cached. However, browsers basically cache the frequent or recent form data for later use. Lets discuss this in details.
By default, browser watch and remember all the information filled through <input>
fields on websites. This enables the browser to offer autocompletion (that is, suggest possible completions for fields that the user has started typing in) or autofill.
These features are enabled by default but user can disable this feature on demand. However, browser doesn't cache some sensitive information like pin code etc. To disable this browser default cache behavior you can set the autocomplete
attribute to "off"
:
autocomplete="off"
You can do this either for an entire form, or for specific input elements in a form:
<form method="post" action="/form" autocomplete="off">
[…]
</form>
Setting autocomplete on form has two effects.
- It notify the browser not to save data entered by user in form fields for later auto-completion
- it prevents the browser from caching form data in the session history. When data is cached one time, it is used later on when user uses the same form and relevant input fields. In short, the cached data is mapped to the related input fields.
However, a important point to note that the autocomplete behavior doesn't work for the login forms. i.e the forms includes the username and password fields. Additionally, the browser enables the user to choose a master password that the browser will use to encrypt stored login details. For this reason, many modern browsers do not support autocomplete="off" for login fields:
- If a site sets autocomplete="off" for a , and the form includes username and password input fields, then the browser still offers to remember this login, and if the user agrees, the browser will autofill those fields the next time the user visits the page.
- If a site sets autocomplete="off" for username and password fields, then the browser still offers to remember this login, and if the user agrees, the browser will autofill those fields the next time the user visits the page.
However, last but not least, there is genuine solution to eliminate this issue and this is filling the user edit form server side. i.e through PHP. In this case there will be no concern of Caching problem and everything will work smoothly. i.e
<input type="text" value="<?php echo $username; ?>" name="firstname"/>
I hope this discussion will help you!