Proc_open php
Nếu lệnh được thực thi không thể tìm thấy, một hợp lệ tài nguyên sẽ được trả lại. Điều này có vẻ kỳ lạ, nhưng có ý nghĩa; Bài viết hướng dẫn cách thực thi dòng lệnh trên máy chủ bằng PHP nhưng hiển thị đầu ra thời gian thực trên trình duyệt Có nhiều hàm thực thi dòng lệnh, tôi thường sử dụng hàm 1000, // 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 "1 để có thể kiểm tra giám sát được cả"; 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'); } } ?> 1000, // 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 "2,"; 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'); } } ?> 1000, // 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 "3 và"; 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'); } } ?> 1000, // 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 "4"; 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'); } } ?> Hiển thị thời gian thực không cần xử lý gì ở máy kháchĐể hiển thị thời gian thực trên trình duyệt mà không cần xử lý gì ở máy khách thì phía máy chủ phải đảm bảo 2 vấn đề
Ví dụ sau hiển thị đầu ra của lệnh 1000, // 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'); } } ?>0 trên màn hình duyệt kiểm tra ping. 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 1000, // 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'); } } ?>1, 1000, // 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'); } } ?>2 và chứa thẻ đầu ra của lệnh ở đây là 1000, // 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'); } } ?>3) sẽ không được ghi ra cho đến khi thực hiện xong lệnh Đặ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 chưa sẵn sàng Vì vậy, giải pháp tốt hơn là hãy sử dụng Sự kiện do máy chủ gửi cho phía khách hàng đầu ra lệnh của một cách chủ động khi mọi thứ đã sẵn sàng Hiển thị thời gian thực bằng Server-sent EventsTa edit 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 sau máy khách chuẩn bị HTML nội dung như sau ________số 8Đoạn mã JavaScript sử dụng EventSource để đọc đầu ra của lệnh, thêm vào giữa 1000, // 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'); } } ?>5 mỗi khi có dữ liệu mới sse. js1000, // 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 "0"; 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'); } } ?> Như vậy với cách làm này, thì phần xử lý CSS và JavaScript đã được tải đầy đủ khi thực hiện lệnh, giao diện hiển thị kết quả sẽ được đảm bảo |