interessante mzanella...la cosa interessante da fare sarebbe quella di leggere dal file la variabile che viene incrementata cosi ogni volta che si ricarica la pagina il contatore non si azzera...
Come posso modificare?
Codice PHP:
// Legge lo stato condiviso e lo restituisce come vettore associativo
function read_status() {
$filename = "pos.txt";
if (!file_exists($filename) || !is_readable($filename)) {
touch($filename);
}
$status = file_get_contents($filename);
if (stripos($status, "on") === 0) {
return array(
'pulsante1' => "on",
'pulsante2' => "off"
);
} else {
return array(
'pulsante1' => "off",
'pulsante2' => "on"
);
}
}
// Scrive lo stato condiviso
function write_status($status) {
$filename = "pos.txt";
file_put_contents($filename, $status['pulsante1']);
}
// Inverte lo stato, restituendone una copia
function switch_status($status) {
return array(
'pulsante1' => $status['pulsante2'],
'pulsante2' => $status['pulsante1']
);
}
// Se è stato premuto un pulsante/richiesto un cambiamento dello stato condiviso...
if (isset($_POST['pulsante'])) {
$pulsante = $_POST['pulsante'];
// Legge lo stato condiviso
$status = read_status();
// Se il pulsante premuto era abilitato, inverte lo stato
if ($status[$pulsante] === "on") {
$status = switch_status($status);
write_status($status);
}
// Restituisce lo stato condiviso
echo json_encode($status);
}
// Altrimenti, mostra semplicemente lo stato attuale senza intervenire attivamente
else {
echo json_encode(read_status());
}