RISOLTO Reload code php

Stato
Discussione chiusa ad ulteriori risposte.

Matt0550

Nuovo Utente
99
12
CPU
i7 9700k
Dissipatore
Cooler Master 212 evo
Scheda Madre
Asus prime z390
HDD
Seagate 1tb
RAM
16gb corsair
GPU
Asus dual rtx2060
Monitor
Omen full hd 144hz
PSU
Seasonic focus + 750w
Case
Cooler master mb511
OS
Windows 10 pro
Salve a tutti! Io ho questo script:
PHP:
function ping($Host2, $port2, $timeout2)
{
  $tB2 = microtime(true);
  $fP2 = fSockOpen($Host2, $port2, $errno2, $errstr2, $timeout2);
  if (!$fP2) { return "down"; }
  $tA2 = microtime(true);
  return round((($tA2 - $tB2) * 1000), 0)." ms";
}
$statoms1 = ping("www.google.com", 80, 10);


if ($statoms1 > 100) {
  echo "Ping troppo alto";
} else {
  echo "Ping ok";
}
echo "\n" . $statoms1;

Volevo fare uno script che ogni secondo il ping si aggiorna. Ho provato un codice ma mi aggiorna la pagina invece voglio un script che aggiorna solo quella parte di codice. E' possibile? grazie.

Inoltre è possibile togliere gli errori di php perché quando per esempio il sito è in down o l'url è sconosciuto mi da anche un'errore php con scritto che non si può connettere o che non trova l'host. è possibile toglierlo?
 

icox

Utente Attivo
497
246
Non sapendo che struttura ha la tua pagina il modo piu' semplice che mi viene in mente e' quello di creare una funzione javascript che periodicamente interroga il tuo server. L'output della tua funzione PHP viene quindi inserito nella pagina aggiornando solo gli elementi del DOM che ti interessano.
Guarda XMLHttpRequest per utilizzare solamente javascript. Se invece usi qualche framework o libreria e' molto probabile che ti offrano dei metodi per chiamate asincrone.

In alternativa puoi usare un iframe, puntando l'url al server/funzione che genera il risultato (personalmente non mi piace molto).

Infine, per tenerlo "aggiornato" puoi banalmente ripetere la richiesta ogni n secondi oppure usare un websocket (piu' avanzato ma piu' efficiente).
 

Matt0550

Nuovo Utente
99
12
CPU
i7 9700k
Dissipatore
Cooler Master 212 evo
Scheda Madre
Asus prime z390
HDD
Seagate 1tb
RAM
16gb corsair
GPU
Asus dual rtx2060
Monitor
Omen full hd 144hz
PSU
Seasonic focus + 750w
Case
Cooler master mb511
OS
Windows 10 pro
Non sapendo che struttura ha la tua pagina il modo piu' semplice che mi viene in mente e' creare una semplice funzione javascript che periodicamente interroga il tuo server. L'output della tua funzione PHP viene quindi inserito nella pagina aggiornando solo gli elementi DOM che ti interessano.
Guarda XMLHttpRequest per utilizzare solamente javascript. Se invece usi qualche framework o libreria e' molto probabile che ti offrano dei metodi per chiamate asincrone.

In alternativa puoi usare un iframe, usando l'url che genera il risultato che desideri inserire nella pagina (personalmente non mi piace molto).

Infine, per tenerlo "aggiornato" puoi banalmente ripetere la richiesta ogni n secondi oppure usare un websocket (piu' avanzato ma piu' efficiente).
Grazie, sul gruppo telegram già mi stanno aiutando infatti mi hanno detto di usare le chiamate asincrone. In caso che non funziona proverò anche la tua soluzione. Grazie!
 

icox

Utente Attivo
497
246
Ah be se hai il gruppo Telegram...

Ti invito a condividere la soluzione qualora la trovassi, potrebbe essere utile ad altri.
 
  • Mi piace
Reazioni: Mursey

Matt0550

Nuovo Utente
99
12
CPU
i7 9700k
Dissipatore
Cooler Master 212 evo
Scheda Madre
Asus prime z390
HDD
Seagate 1tb
RAM
16gb corsair
GPU
Asus dual rtx2060
Monitor
Omen full hd 144hz
PSU
Seasonic focus + 750w
Case
Cooler master mb511
OS
Windows 10 pro
Ah be se hai il gruppo Telegram...

Ti invito a condividere la soluzione qualora la trovassi, potrebbe essere utile ad altri.
Sisi, lo so. Grazie
Post unito automaticamente:

Soluzione:
index.html:
HTML:
<html>
<head>
    <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
    <script src="index.js"></script>
    </head>
    
    <body>
    <div>
        <p>ping 1</p>
        <p id="ms1"></p>
        <p id="status1"></p>
    </div>
    <div>
        <p>ping 2</p>
        <p id="ms2"></p>
        <p id="status2"></p>
    </div>
    <div>
        <p>ping 3</p>
        <p id="ms3"></p>
        <p id="status3"></p>
    </div>

    </body>
</html>

ping.php:
PHP:
<?php


function ping($Host2, $port2, $timeout2)
{
  $tB2 = microtime(true);
  $fP2 = fSockOpen($Host2, $port2, $errno2, $errstr2, $timeout2);
  if (!$fP2) { return "down"; }
  $tA2 = microtime(true);
  return round((($tA2 - $tB2) * 1000), 0)." ms";
}

$statoms1 = ping("www.google.com", 80, 10);
$statoms2 = ping("socialclub.rockstargames.com", 80, 10);
$statoms3 = ping("www.rockstargames.com", 80, 10);
$status = '';
$data = array();

if ($statoms1 >= 100 && $statoms1 < 999) {
    $status = "Ping troppo alto";
    array_push($data, ['status' =>  $status, 'ms' => $statoms1]);
} else if($statoms1 < 100){
    $status = "Ping ok";
    array_push($data, ['status' =>  $status, 'ms' => $statoms1]);
}
else if($statoms1 >1000){
    $status = "Ping troppo alto";
    array_push($data, ['status' =>  $status, 'ms' => $statoms1]);
}

if ($statoms2 >= 100 && $statoms2 < 999) {
    $status = "Ping troppo alto";
    array_push($data, ['status' =>  $status, 'ms' => $statoms2]);
} else if($statoms2 < 100){
    $status = "Ping ok";
    array_push($data, ['status' =>  $status, 'ms' => $statoms2]);
}
else if($statoms2 >1000){
    $status = "Ping troppo alto";
    array_push($data, ['status' =>  $status, 'ms' => $statoms2]);
}

if ($statoms3 >= 100 && $statoms3 < 999) {
    $status = "Ping troppo alto";
    array_push($data, ['status' =>  $status, 'ms' => $statoms3]);
} else if($statoms3 < 100){
    $status = "Ping ok";
    array_push($data, ['status' =>  $status, 'ms' => $statoms3]);
}
else if($statoms3 >1000){
    $status = "Ping troppo alto";
    array_push($data, ['status' =>  $status, 'ms' => $statoms3]);
}


echo json_encode($data); //output in formato json

 
?>


index.js:
JavaScript:
$(document).ready(function(){ //jquery attende il caricamento della pagina

setInterval(function() { //esegue ogni secondo la funzione al suo interno
    $.getJSON( "ping.php", function( data ) { //chiamata asincrona con get allo script
        $.each(data, function(key, value){ // esegue un foreach
            if(key == 0){
                $('#ms1').html(value.ms); //scrive i ms
                $('#status1').html(value.status); //scrive lo stato
                if (value.status == "Ping troppo alto"){
                    $('#status1').css("color", "red");
                }
                else if (value.status == "Ping ok"){
                    $('#status1').css("color", "green")
                }
            }
            else if(key == 1){
                $('#ms2').html(value.ms); //scrive i ms
                $('#status2').html(value.status); //scrive lo stato
            if (value.status == "Ping troppo alto"){
                $('#status2').css("color", "red");
            }
                else if (value.status == "Ping ok"){
                    $('#status2').css("color", "green")
                }
            }
            
            else if(key == 2){
                $('#ms3').html(value.ms); //scrive i ms
                $('#status3').html(value.status); //scrive lo stato
            }
            if (value.status == "Ping troppo alto"){
                $('#status3').css("color", "red");
            }
                else if (value.status == "Ping ok"){
                    $('#status3').css("color", "green")
                }
        });
    }).fail(function(){alert ('url sbagliato');});
}, 1000);// 1000 sono i millisecondi
  
});

GRAZIE A mt512 CHE MI HA AIUTATO A RISOLVERE IL PROBLEMA.
 

Allegati

  • ping.zip
    1.4 KB · Visualizzazioni: 34
Ultima modifica:

sp3ctrum

Amministratore
Staff Forum
15,961
7,809
CPU
AMD Ryzen 5 3900X
Dissipatore
Scythe Mugen 5 rev.b Push&Pull
Scheda Madre
ASUS TUF Gaming B550M-Plus WIFI
HDD
Nvme Samsung EVO 1Tb + SSD Samsung evo 850 250gb + Toshiba P300 3Tb
RAM
HyperX FURY 16gb 3200Mhz
GPU
ASUS Dual GeForce RTX 4070 OC White Edition 12GB GDDR6X
Audio
Topping DX3 Pro + Focusrite Scarlett 2i2 + Mackie MR524 + Beyer DT 770 Pro + Presonus SubWoofer
Monitor
LG Ultragear 27gp850
PSU
Corsair RM850x 80 PLUS Gold
Case
Thermaltake V200 RGB
Net
Vodafone 1000Mb
OS
Windows 11 Pro
Chiudo come da richiesta
 
Stato
Discussione chiusa ad ulteriori risposte.

Entra

oppure Accedi utilizzando
Discord Ufficiale Entra ora!