Hướng dẫn dùng ping -f trong PHP
Làm thế nào để bạn ping một địa chỉ ip trong php. và đưa ra kết quả như thể bạn đang sử dụng chương trình cmd trong windows
17 hữu ích 1 bình luận 90k xem chia sẻ answer 20
đầu ra trông giống như bên dưới
20 hữu ích 5 bình luận chia sẻ answer 21 Thử cái này
Lưu ý: Điều này phụ thuộc vào hệ điều hành bạn đang chạy. Windows sẽ mặc định chỉ có 4 ping trong khi Linux sẽ ping mãi mãi. Để ping hai lần trong Windows, hãy sử dụng "ping -n 2 host" Để ping hai lần trong Linux, hãy sử dụng "ping -c 2 host" 21 hữu ích 0 bình luận chia sẻ answer 2 Tôi vừa ping google với giám đốc điều hành đó
đầu ra là:
2 hữu ích 0 bình luận chia sẻ Đăng nhập để trả lời câu hỏiCó thể bạn quan tâmBài viết hướng dẫn cách thực thi dòng lệnh trên server bằng PHP nhưng hiển thị realtime đầu ra trên trình duyệt. Có nhiều hàm thực thi dòng lệnh, tôi thường dùng hàm Hiển thị realtime không cần xử lý gì ở clientĐể hiển thị realtime trên trình duyệt mà không cần xử lý gì ở client thì phía server phải đảm bảo 2 vấn đề:
Ví dụ sau hiển thị đầu ra của lệnh ping-test.php1000, // 1000 packets '-a', // resolve IP address to hostname '8.8.8.8', // Target IP address ); /// Form the command line $cmd = escapeshellarg($cmd); foreach ($args as $arg => $value) : if (is_string($arg)) : $cmd .= ' ' . escapeshellarg($arg) . ' ' . escapeshellarg($value); else : $cmd .= ' ' . escapeshellarg($value); endif; endforeach; $descriptorspec = array( 0 => array("pipe", "r"), // stdin 1 => array("pipe", "w"), // stdout 2 => array("pipe", "w") // stderr ); $cwd = NULL; $env = NULL; $options = array('bypass_shell' => true); disable_ob(); $process = proc_open($cmd, $descriptorspec, $pipes, $cwd, $env, $options); if ( is_resource($process) ) : header("Content-type: text/html; charset=utf-8"); echo ""; while ($s = fgets($pipes[1])) { print htmlspecialchars( $s ); flush(); // dump the line immediately } $retval = proc_close($process); echo "\nCommand returned $retval\n"; echo ""; endif; /** * Turn off Output Buffer. */ function disable_ob() { // Turn off output buffering ini_set('output_buffering', 'off'); // Turn off PHP output compression ini_set('zlib.output_compression', false); // Implicitly flush the buffer(s) ini_set('implicit_flush', true); ob_implicit_flush(true); // Clear, and turn off output buffering while (ob_get_level() > 0) { // Get the curent level $level = ob_get_level(); // End the buffering ob_end_clean(); // If the current level has not changed, abort if (ob_get_level() == $level) break; } // Disable apache output buffering/compression if (function_exists('apache_setenv')) { apache_setenv('no-gzip', '1'); apache_setenv('dont-vary', '1'); } } ?> Nếu chú ý bạn sẽ thấy các thẻ đóng (điển hình là của Đặc biệt nếu HTML có chứa CSS và JavaScript sẽ khó có thể thực thi chính xác khi DOM còn chưa sẵn sàng. Vì vậy giải pháp tốt hơn là hãy sử dụng Server-sent Events để phía client xử lý đầu ra của lệnh một cách chủ động khi mọi thứ đã sẵn sàng. Hiển thị realtime dùng Server-sent EventsTa chỉnh lại đoạn mã trên một chút:
ping-test-sse.php1000, // 1000 packets '-a', // resolve IP address to hostname '8.8.8.8', // Target IP address ); /// Form the command line $cmd = escapeshellarg($cmd); foreach ($args as $arg => $value) : if (is_string($arg)) : $cmd .= ' ' . escapeshellarg($arg) . ' ' . escapeshellarg($value); else : $cmd .= ' ' . escapeshellarg($value); endif; endforeach; $descriptorspec = array( 0 => array("pipe", "r"), // stdin 1 => array("pipe", "w"), // stdout 2 => array("pipe", "w") // stderr ); $cwd = NULL; $env = NULL; $options = array('bypass_shell' => true); disable_ob(); $process = proc_open($cmd, $descriptorspec, $pipes, $cwd, $env, $options); if ( is_resource($process) ) : header("Content-type: text/event-stream; charset=utf-8"); while ($s = fgets($pipes[1])) : print "data: " . htmlspecialchars( $s ) . PHP_EOL; print PHP_EOL; flush(); // dump the line immediately endwhile; $retval = proc_close($process); print "id: close" . PHP_EOL; // notify end of script print "data: Command returned $retval" . PHP_EOL; print PHP_EOL; endif; /** * Turn off Output Buffer. */ function disable_ob() { // Turn off output buffering ini_set('output_buffering', 'off'); // Turn off PHP output compression ini_set('zlib.output_compression', false); // Implicitly flush the buffer(s) ini_set('implicit_flush', true); ob_implicit_flush(true); // Clear, and turn off output buffering while (ob_get_level() > 0) { // Get the curent level $level = ob_get_level(); // End the buffering ob_end_clean(); // If the current level has not changed, abort if (ob_get_level() == $level) break; } // Disable apache output buffering/compression if (function_exists('apache_setenv')) { apache_setenv('no-gzip', '1'); apache_setenv('dont-vary', '1'); } } ?> Trong đó phần được thay đổi là header("Content-type: text/event-stream; charset=utf-8"); while ($s = fgets($pipes[1])) { print "data: " . $s . PHP_EOL; print PHP_EOL; flush(); // dump the line immediately } $retval = proc_close($process); print "id: close" . PHP_EOL; // notify end of script print "data: Command returned $retval" . PHP_EOL; print PHP_EOL; Phía client chuẩn bị một nội dung HTML như sau:
Đoạn mã JavaScript dùng EventSource để đọc đầu ra của lệnh, chèn vào giữa sse.jsif ( !!window.EventSource ) { var output = document.getElementById("output"); var source = new EventSource("ping-test-sse.php"); source.onopen = function(e) { console.log("SSE connection was established"); }; source.onerror = function(e) { alert("SSE error!"); console.error(e); } source.onmessage = function(e) { output.innerHTML += e.data + "\r\n"; if (e.lastEventId === 'close') // id 'close' was found source.close(); }; } else { alert("Your web browser does not support SSE"); } Như vậy với cách làm này, thì phần CSS và JavaScript xử lý đã được load đầy đủ khi thực thi lệnh, giao diện hiển thị kết quả sẽ được đảm bảo. |