Jump to content

website being cached


php_novice2007

Recommended Posts

Hi,

 

I'm writing a page where each time it is loaded, PHP script is used to generate an xml file as well as a text file. Then javascript is used to read the xml file and generate some plots on the page. Each time the page is loaded the xml file should be changing, however the Javascript seems to be remembering the very first file generated and plot the same thing even though the file has changed. I checked the file manually in the directory and indeed the xml file is different yet the javascript is still printing old stuff. I tried putting just a <a href..> link to the file and it is displaying the old file no the newest one.. So far I found that if I close the browser and open it again then it works.

 

How do I make javascript read the "latest" file...

 

Thanks~!

 

 

 

Link to comment
Share on other sites

I had a problem similar to this, I just edited my .htaccess to read a certain page like FAFDAF-thispage.whatever would actually be a regular page

I would randomize the name and add the -thispage.whatever at the end to certify that it was the page and it would cache. hope this helped

Link to comment
Share on other sites

Hi,

 

I'm a bit confused.. what do I have to do?

 

This is my code originally:

 

<?php
    //start the session 
    session_start(); 
    //check to make sure the session variable is registered 
    if(session_is_registered('userid')){
      if ($_SESSION['group']=="admin") {
        $doc = new DomDocument('1.0');
        $node = $doc->createElement("positions");
        $parnode = $doc->appendChild($node);
        $all = 0;
        $num = 0;
        if (isset($_POST['a']) || isset($_POST['b'])) {
          if(isset($_POST['a']) && isset($_POST['unitNum']) && !isset($_POST['b'])){
            $all = 1;
            $unitNUM = $_POST['unitNum'];
            $unitNum = "u" . str_pad($unitNUM, 4, "0", STR_PAD_LEFT); // pad the number with 0 to make it a 4 digit number 

            require("databaseInfo.php");
          	$dbtable = $unitNum;
          	$link=mysql_connect("localhost", $username, $password) or die("Cannot connect to database");
          
          	//select database
          	@mysql_select_db($database) or die("Unable to select database");
          	
          	//The whole contents of $dbtable from the red1 database is now contained in $result.
          	$query="SELECT * FROM ".$dbtable;
          	$result=mysql_query($query, $link) or die("Unable to load selected table");
          
          	//find out how many rows there are in the database
          	$num=mysql_num_rows($result) or die("There are no results");
            
            //close link
          	mysql_close($link) or die("Unable to close database");
          } else {
            if(!isset($_POST['a']) && isset($_POST['b']) && isset($_POST['sday']) && isset($_POST['smonth']) && isset($_POST['syear']) && isset($_POST['eday']) && isset($_POST['emonth']) && isset($_POST['eyear']) && isset($_POST['unitNum'])){
              $num = 0;
              $start = $_POST['syear']."-".$_POST['smonth']."-".$_POST['sday']." 00:00:00";
              $end = $_POST['eyear']."-".$_POST['emonth']."-".$_POST['eday']." 23:59:59";
              $unitNUM = $_POST['unitNum'];
              $unitNum = "u" . str_pad($unitNUM, 4, "0", STR_PAD_LEFT); // pad the number with 0 to make it a 4 digit number 
  
              require("databaseInfo.php");
              $dbtable = $unitNum;
              $link=mysql_connect("localhost", $username, $password) or die("Cannot connect to database");
            
              //select database
              @mysql_select_db($database) or die("Unable to select database");
    
              //The whole contents of $dbtable from the red1 database is now contained in $result.
              $query="SELECT * FROM `$dbtable` WHERE fixdatetime >= '$start' AND fixdatetime <= '$end'";
              $result=mysql_query($query, $link) or die("Unable to load selected table");
     
              //find out how many rows there are in the database
              $num=mysql_num_rows($result);
        
              //close link
              mysql_close($link) or die("Unable to close database");
            }
          }
          
          if ($num > 0) {
             $myFile = $unitNum."AdminData".".txt";
             $fh = fopen($myFile, 'w') or die("can't open file");
             fwrite($fh, "$num \n");
             $i=0;    //to iterate through each row
  	         while ($i < $num) {
   		         $lat=mysql_result($result,$i,"lat");
   		         $long=mysql_result($result,$i,"lng");
   		         $timestamp = mysql_result($result,$i,"fixdatetime");
               $node = $doc->createElement("position");
               $newnode = $parnode->appendChild($node);
               $newnode->setAttribute("timestamp", $timestamp);
               $newnode->setAttribute("latitude", $lat);
               $newnode->setAttribute("longitude", $long);
               
               $stringData =$timestamp.",".$lat.",".$long."\n";
               fwrite($fh, $stringData);
               $i++;
             }
             fclose($fh);
             //generate xml 
             $doc->formatOutput = true; // set the formatOutput attribute of domDocument to true 
             // save XML as string or file 
             //$test1 = $doc->saveXML(); // put string in test1 
             $xmlName = $unitNum.".xml";
             $doc->save($xmlName); // save as file 
?>
            <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"    
            "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
            <html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml">
            <head>
            <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
            <title>Query Results</title>
            <script src="http://maps.google.com/maps?file=api&v=2&key=ABQIAAAA_ZKXKLNCYSzHFMNyG-Bj2xQXwYfUv9Msb5tvjKK5I8eE-jDdexQRB0gnc-9SRTy-FhW0Wii5KFTosg"
            type="text/javascript"></script>    
            <script src="http://www.acme.com/javascript/OverlayMessage.js" type="text/javascript"></script>
            <script src="http://www.acme.com/javascript/Clusterer2.jsm" type="text/javascript"></script>
            
            <script type="text/javascript">
              //<![CDATA[
              function createMarker(point,iCon) {
                var marker = new GMarker(point,iCon);
                return marker;
              }
              var om;
              var toggleState=1;
              var markerArray = [];
              var pointsArray = [];
              var clusterer; 
              var map;
              function loadMap() {
                
                if (GBrowserIsCompatible()) {
                  //display the loading message
                om = new OverlayMessage(document.getElementById('map'));
                om.Set('<b>Map is loading...</b>');
      
                var icon = new GIcon();
                icon.iconSize = new GSize(12,20);
                icon.iconAnchor = new GPoint(6,20);
                iconblue = new GIcon(icon, "http://labs.google.com/ridefinder/images/mm_20_blue.png");

                  //create the map
                map = new GMap2(document.getElementById("map"));
                  map.addControl(new GLargeMapControl());
                  map.addControl(new GMapTypeControl());
                  map.setCenter(new GLatLng(0,0),0);

     	            //create bounds object to set zoom level to fit all datapoints
                  var bounds = new GLatLngBounds();
        
                  // create the clusterer
                  clusterer = new Clusterer(map);
        
                  // set the clusterer parameters if you dont like the defaults     
                  //clusterer.maxVisibleMarkers = 50;
                  //clusterer.gridSize = 5;
                  clusterer.minMarkersPerClusterer = 3;
      
                  GDownloadUrl("<? echo $xmlName;?>", function(data) {  
                    var xml = GXml.parse(data);  
                    var positions = xml.documentElement.getElementsByTagName("position");  
                   
                    
                    for (var i = 0; i < positions.length; i++) {        
                      var point = new GLatLng(parseFloat(positions[i].getAttribute("latitude")), 
                                    parseFloat(positions[i].getAttribute("longitude")));    
                      pointsArray[i] = point;   
                      var marker = createMarker(point,iconblue);
                      
                      clusterer.AddMarker(marker);
                      markerArray[i] = marker;
                      //map.addOverlay(marker);
                      bounds.extend(point);
                  }
                    map.setZoom(map.getBoundsZoomLevel(bounds));
                  map.setCenter(bounds.getCenter());
                    map.addOverlay(new GPolyline(pointsArray,"#0000FF",2));
                  });
               } else {
                  // display a warning if the browser was not compatible
                    alert("Sorry, the Google Maps API is not compatible with this browser");
                 }

               }
               
               //remove the loading message when the page has finished loading
               function clearmessage() {
                 om.Clear();
               }
               
               function toggleMarkers() {
               //clusterer = new Clusterer(map);
                 if (toggleState == 1) {
                  map.getPane(G_MAP_MARKER_PANE).style.visibility="hidden";
                    toggleState = 0;
               } else {
	             		map.getPane(G_MAP_MARKER_PANE).style.visibility="visible";
                    toggleState = 1;
               }
               }
               //]]>              
            </script>
            
            
                       
            </head>
            <body bgcolor="#CCFFCC" onload="loadMap(); clearmessage();" onunload="GUnload()">
<?
            if ($all == 1) {
               echo "<center><h1>Sea Grass Tracker Data for Unit $unitNUM </h1></center><br>";
            } else {
               echo "<h1><center>Results from $start to $end for unit $unitNUM</center></h1><br>"; 
            }
?>            
            <center><div id="map" style="width: 650px; height: 550px"></div></center>
            <input id="markerToggler" type="button" onclick="toggleMarkers();" value="Toggle Markers">
            <br><a href=<? echo $myFile;?>>Raw Unit Data</a>
            <br><a href=displayAdmin.php>Go Back</a>
         
            
            </body></html>            
<?    
          } else {
            echo "There are no results. <br><br><a href=displayAdmin.php>Go Back</a>";
          }
        } else {
           echo "This page require input from somewhere else.";
           echo "<br><a href=secureMenuAdmin.php>Back to Menu</a>";
        }
      } else {
          // someone has logged on but is not admin
          echo "You do not have permission to access this page.<br><br><a href=secureMenu.php>Back to Menu</a>";
      }
   } else {
        echo "You are not logged in, please go <a href=loginForm.html>here</a> to login";
   }
?>        

 

Thanks

 

p.s. Sorry I'm still waiting for things to be set up so I can't put a link to the page and show what I'm talking about...

 

 

Link to comment
Share on other sites

try using an ajax trick to stop caching by adding a random url passed # at the end of the url that calls the xml file. Something like this:

MYxmlURL = 'someurl.xml?x=' + Math.random();

--it shouldn't matter that the url parameter is not actually processed by the xml file.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.