How do you ping an ip addresses in php. and give the the results as if you are on cmd program in windows

<?php 


  system(‘ping -c 192.168.0.104’); // Ping IP address.

    echo “pinged”;

?>

Dani AI

Generated

A few practical points to make a PHP script run Windows ping and show output like the cmd prompt. This builds on 's question and extends 's suggestion by covering Windows flags, security, real‑time output, and common server pitfalls.

A minimal, safe capture (returns whole output) uses an absolute ping path, input validation, and escaping to avoid injection:

$ip = '192.168.0.104';
if (!filter_var($ip, FILTER_VALIDATE_IP)) { exit('invalid IP'); }

$cmd = 'C:\\Windows\\System32\\ping.exe -n 4 ' . escapeshellarg($ip);
$output = shell_exec($cmd);
echo nl2br(htmlspecialchars($output));

To mimic the live stream you see in cmd (line-by-line), read from the process and flush buffers so the browser receives output as it comes:

$cmd = 'C:\\Windows\\System32\\ping.exe -n 4 ' . escapeshellarg($ip);
$h = popen($cmd, 'r');
header('Content-Type: text/plain');
while (!feof($h)) {
    $line = fgets($h);
    echo htmlspecialchars($line);
    @ob_flush(); flush();
}
pclose($h);

Troubleshooting and cautions: confirm exec/shell_exec/popen/proc_open are not disabled in php.ini (disable_functions). On 64-bit Windows a 32-bit PHP under IIS may be redirected away from System32 — use C:\Windows\Sysnative\ping.exe when that happens. Ensure the webserver user has permission to run external programs and remember some networks or hosts block ICMP so ping can fail even when code is correct. Always validate and escape any user-supplied IP before passing it to the shell.

try this:

<?php
    echo exec("ping www.google.com");
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.