Ad:
 
  • PHP Code Snippet
  • Views: 10977
  • PHP RSS
0

Ping

by corewizard on Apr 16th, 2005
Ping a server
PHP Code Snippet (Toggle Plain Text)
  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.  
Comments on this Code Snippet
Oct 16th, 2009
0

Re: Ping

this is the most insecure way of performing a ping, if your webhost allowes you the system / exec functions, you are hosting your site on a highly possible hack target, with the biggest loophole of php.
Newbie Poster
JLChafardet is offline Offline
15 posts
since Jun 2007
Message:
Previous Thread in PHP Forum Timeline: creating name servers for localhost on window xxp
Next Thread in PHP Forum Timeline: going mad now mysql db password change fails





About Us | Contact Us | Advertise | Acceptable Use Policy
Build Custom RSS Feed


Follow us on Twitter


© 2010 DaniWeb® LLC