Visualizzazione dei risultati da 1 a 3 su 3
  1. #1

    Semplice contatore multisito

    Vorrei scrivere un semplice programmino PHP per mantenere in un unico file tutti i miei counter. In pratica il file dovrebbe contenere record del tipo

    rosso:12983
    verde:65433
    bianco:99

    ed essere usato come

    <?php aggiorna("rosso"); ?>

    Il risultato sarebbe, in questo caso, 12984

    se invece scrivessi

    <?php aggiorna("blu"); ?>

    avrei 1 e il nuovo file sarebbe

    rosso:12983
    verde:65433
    bianco:99
    blu:1

    Posso usare list() e split() per leggere le coppie nome:valore, ma come aggiorno il singolo record? Non ho familiarità con le funzioni di scrittura del PHP. In genere non lavoro con i file.
    Dario de Judicibus - Roma, Italia
    Sito: http://www.dejudicibus.it
    Blog: http://lindipendente.splinder.com

  2. #2
    Che ne pensate di questo? Può funzionare oppure ho commesso qualche errore?

    codice:
    // Load any text file (Unix, Win, Mac, ...) and strip end of line chars
    // Better and faster than file()
    function loadFile($filename) {  $fp = fopen($filename, "rb");
      $buffer = fread($fp, filesize($filename));
      fclose($fp);
      $lines = preg_split("/\r?\n|\r/", $buffer);
      return $lines;
    } 
    
    // Save an array of strings to a text file
    function saveFile($filename, $array) {
      $fp = fopen($filename, "w+");
      foreach ($array as $line) {
        fputs($fp,$line); 
      }
      fclose($fp);
    }
    
    // Read counter file, search for name
    // If found update, otherwise create (initial value = 1)
    function updateCounter($counterName) {
      $found = false ;
      $counterFile = "counters.txt"; 
    
      if (file_exists($counterFile)) {
        $fileRecords = loadFile($counterFile);
        for ($i=0; $i<sizeof($fileRecords); $i++) {  
          list($aName,$counterValue) = split(":",$fileRecords[$i]);
          if ($aName == $counterName) {
            if (is_int($counterValue)) {
              $counterValue++ ;
            } 
            else {
              $counterValue = 1 ;
            }
            $fileRecords[$i] = $counterName . ":" . $counterValue ;
            $found = true ;
            break ;
          }
        }
      }
      if (!$found) {
        $fileRecords[1] = $counterName . ":1" ;
      }
      saveFile($counterFile, $fileRecords) ;
      return $counterValue ;
    }
    Dario de Judicibus - Roma, Italia
    Sito: http://www.dejudicibus.it
    Blog: http://lindipendente.splinder.com

  3. #3
    Ho provato questo codice, ma anche se funziona, mi produce una serie di linee aggiuntive fra i record. Ho provato a togliere \n nella save, ma mi vengono i record tutti di seguito. Chi è che mi aggiunge le linee?

    codice:
    // Check if a string is a whole number
    function is_whole_number($var){
    	return (is_numeric($var)&&(intval($var)==floatval($var)));
    }
    // Load any text file (Unix, Win, Mac, ...) and strip end of line chars
    // Better and faster than file()
    function loadFile($filename) {
    	$fp = fopen($filename, "rb");
    	$buffer = fread($fp, filesize($filename));
    	fclose($fp);
    	$lines = preg_split("/\r?\n|\r/", $buffer);
    	return $lines;
    } 
    // Save an array of strings to a text file
    function saveFile($filename, $array) {
    	$fp = fopen($filename, "w+");
    	for ($i=0; $i<sizeof($array); $i++) {
    		fputs($fp,$array[$i]."\n"); 
    	}
    	fclose($fp);
    }
    // Read counter file, search for name
    // If found update, otherwise create (initial value = 1)
    function updateCounter($counterName) {
    	$found = 0 ;
    	$append = 0 ;
    	$counterFile = "counters.txt"; 
    	if (file_exists($counterFile)) {
    		$fileRecords = loadFile($counterFile);ECHO "[".sizeof($fileRecords)."]";
      		for ($i=0; $i<sizeof($fileRecords); $i++) {
        		list($aName,$counterValue) = explode(":",trim($fileRecords[$i]));
        		if (strcmp($aName,$counterName) == 0) {
    				if (is_whole_number($counterValue)) {
    					$counterValue++ ;
    				} 
    				else {
    					$counterValue = 1 ;
    				}
    				$fileRecords[$i] = $counterName . ":" . $counterValue ;
    				$found = $counterValue ;
    				break $i ;
        		}
    		}
    		$append = sizeof($fileRecords) ;
    	}
    	if ($found == 0) {ECHO $last . "!";
    		$counterValue = 1 ;
    		$fileRecords[$append] = $counterName . ":" . $counterValue ;
    	}
    	saveFile($counterFile, $fileRecords) ;
    	return $counterValue ;
    }
    Dario de Judicibus - Roma, Italia
    Sito: http://www.dejudicibus.it
    Blog: http://lindipendente.splinder.com

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.