User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 363,779 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,496 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Apr 16th, 2005
Views: 4,284
Ping a server
php Syntax | 5 stars
  1. <?php
  2.  
  3. // Address error handling.
  4.  
  5. ini_set('display_errors', 1);
  6. error_reporting(E_ALL ^ E_NOTICE);
  7.  
  8. // Obtain POST data.
  9.  
  10. $ping_ip_addr = $_POST['ping_ip_addr']; // input
  11. $ping_count = $_POST['ping_count']; // select
  12.  
  13. // Remove any slashes if Magic Quotes GPC is enabled.
  14.  
  15. if (get_magic_quotes_gpc())
  16. {
  17. $ping_ip_addr = stripslashes($ping_ip_addr);
  18. }
  19.  
  20. // Create arrays.
  21.  
  22. $ping_count_array = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 25); // Ping count
  23.  
  24. ?>
  25.  
  26. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  27.  
  28. <head>
  29. <title>Ping</title>
  30. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  31. <meta name="author" content="firstbase" />
  32. <style type="text/css">
  33.  
  34. body
  35. {
  36. margin: 0;
  37. padding: 10px;
  38. background-color: #ffffff;
  39. }
  40.  
  41. div.output
  42. {
  43. margin: 0;
  44. padding: 10px;
  45. background-color: #eeeeee;
  46. border-style: solid;
  47. border-width: 1px;
  48. border-color: #000000;
  49. }
  50.  
  51. </style>
  52. </head>
  53.  
  54. <body>
  55. <h1>Ping</h1>
  56. <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
  57. <p><label for="ping_ip_addr">IP address:</label><br />
  58. <input name="ping_ip_addr" id="ping_ip_addr" type="text" value="<?php echo $_POST['submit'] == 'Ping' ? htmlentities($ping_ip_addr, ENT_QUOTES) : $_SERVER['REMOTE_ADDR'];; ?>" size="40" maxlength="15" /></p>
  59. <p><label for="ping_count">Ping count:</label><br />
  60. <select name="ping_count" id="ping_count">
  61. <?php
  62.  
  63. foreach ($ping_count_array as $ping_count_item)
  64. {
  65. echo '<option' . ($ping_count == $ping_count_item ? ' selected="selected"' : '') . '>' . $ping_count_item . '</option>' . "\n";
  66. }
  67.  
  68. ?>
  69. </select></p>
  70. <p><input type="submit" name="submit" value="Ping" /></p>
  71. </form>
  72. <p>Ping may take a while, please be patient.</p>
  73. <?php
  74.  
  75. if ($_POST['submit'] == 'Ping') // Form has been submitted.
  76. {
  77. echo '<div class="output">' . "\n";
  78.  
  79. /**************************************************************************/
  80.  
  81. // Check for spoofed form submission.
  82.  
  83. $illegal = FALSE;
  84.  
  85. if (strlen($ping_ip_addr) > 15)
  86. {
  87. $illegal = TRUE;
  88. }
  89.  
  90. if (!in_array($ping_count, $ping_count_array))
  91. {
  92. $illegal = TRUE;
  93. }
  94.  
  95. /**************************************************************************/
  96.  
  97. if (!$illegal) // Form submission was not spoofed.
  98. {
  99. if (ereg('^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$', $ping_ip_addr)) // Acquired data contains no problems.
  100. {
  101. // Display result.
  102.  
  103. echo '<pre>' . "\n" .
  104. 'ping -c ' . $ping_count . ' ' . $ping_ip_addr . "\n\n";
  105.  
  106. system('ping -c ' . $ping_count . ' ' . $ping_ip_addr); // Ping IP address.
  107.  
  108. echo '</pre>' . "\n" .
  109. '<p>Ping complete.</p>' . "\n";
  110. }
  111. else // Acquired data contains problems!
  112. {
  113. echo '<p>Please enter a valid IP address.</p>' . "\n";
  114. }
  115. }
  116. else // Form submission was spoofed!
  117. {
  118. echo '<p>An illegal operation was encountered.</p>' . "\n";
  119. }
  120.  
  121. echo '</div>' . "\n";
  122. }
  123.  
  124. ?>
  125. </body>
  126.  
  127. </html>
  128.  
Post Comment

Only community members can submit or comment on code snippets. You must register or log in to contribute.

DaniWeb Marketplace (Sponsored Links)
All times are GMT -4. The time now is 10:39 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC