0

So I have a project where I am using an XML document to store data that will be used by several users. User will need to read the contents of the XML, make changes by adding or removing elements, and then save the XML for others to see.

The problem that I am having has to do with browsers caching the XML file. Because of this, a user can successfully read, change, and save a document without other users seeing the changes. Even the user that makes the changes cannot access the new file by default. The server file has changed successfully, but users are viewing the cached file rather than the most recent version.

As I understand it, if the version of the file changes that should force a browser to use the higher version rather than the cached version. However, I can't figure out how to change the version number of the XML file I am saving. Even if I manually change the version of my XML document, my code changes it back to "1.0" and I don't know why or how.

Here is my javascript that alters the XML:

/*-------
This function modifies the XMLDoc which stores the xml information from         
users/logins.xml
It then calls SaveData() to write the modified information to file
-------*/
function RemoveFromXML(attribute){

    /*-------
    This is the tag we will be removing
    -------*/   
    var removeElement = XMLDoc.getElementsByTagName(attribute)[0];

    /*-------
    This removes the element from the document
    -------*/
    removeElement.parentNode.removeChild(removeElement);

    /*-------
    Now we save the document with the new changes
    -------*/   
    SaveData();
}

/*-------
Updates the users/logins.xml document
-------*/
function SaveData(){

    /*-------
    Data to Save and file at which to save
    -------*/
    var sendData = "This is new data";
    var path = "saveUserXML.php";

    /*-------
    Create the data based upon the contents of XMLDoc
    -------*/
    sendData = new XMLSerializer().serializeToString(XMLDoc);


    /*-------
    Send the "sendData" to the php file
    -------*/

    xmlhttp.open("POST",path,false);
    xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    xmlhttp.send("a="+sendData);

}

And my PHP file that actually writes the XML:

<?php

$myfile = fopen("users/logins.xml", "w+");
fwrite($myfile, $_POST['a']);
fclose($myfile);

echo "Finished.";

?>

Again, my goal is to prevent users from viewing the cached version of the XML file when the file has been changed. If there is a better way than changing the version after every alteration I would be open to that.

user1544522
  • 43
  • 1
  • 5
  • 1
    Your entire premise is wrong; the version number in an XML file has nothing to do with caching, it's the version of XML as a language. There are only two valid versions of XML, 1.0 and 1.1 (and the differences are very slight). And the browser won't even *know* if you change something inside the file, because the whole point of caching is to avoid downloading the file contents again. Look up "HTTP cache headers" to find out what you actually need to change. – IMSoP Sep 26 '15 at 17:20
  • Your code seems to have a data race. When two requests change the file at the same time, changes from the first request could be destroyed by the second request. – Finn Sep 26 '15 at 21:35
  • Possible duplicate of [What do I put in my HTML to ensure users get latest version of my page, not old version?](https://stackoverflow.com/questions/34445358/what-do-i-put-in-my-html-to-ensure-users-get-latest-version-of-my-page-not-old) – Quentin Jan 26 '18 at 17:12

1 Answers1

-1

Adding a parameter to the link that changes every time to the file is requested seems to work for me. This one adds 3 digits:

$anti_cache = substr(time(), 7);
<a href='file.xml?$anti_cache'>link</a>
Nick Iredale
  • 79
  • 2
  • 11