admin.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. <?php
  2. namespace Statistics\Modules;
  3. function admin()
  4. {
  5. $act = isset($_GET['act'])? $_GET['act'] : 'home';
  6. switch($act)
  7. {
  8. case 'detect_server':
  9. // 创建udp socket
  10. $socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
  11. socket_set_option($socket, SOL_SOCKET, SO_BROADCAST, 1);
  12. $buffer = json_encode(array('cmd'=>'REPORT_IP'))."\n";
  13. // 广播
  14. socket_sendto($socket, $buffer, strlen($buffer), 0, '255.255.255.255', \Statistics\Web\Config::$ProviderPort);
  15. // 超时相关
  16. $time_start = microtime(true);
  17. $global_timeout = 1;
  18. $ip_list = array();
  19. $recv_timeout = array('sec'=>0,'usec'=>8000);
  20. socket_set_option($socket,SOL_SOCKET,SO_RCVTIMEO,$recv_timeout);
  21. // 循环读数据
  22. while(microtime(true) - $time_start < $global_timeout)
  23. {
  24. $buf = $host = $port = '';
  25. if(@socket_recvfrom($socket, $buf, 65535, 0, $host, $port))
  26. {
  27. $ip_list[$host] = $host;
  28. }
  29. }
  30. break;
  31. }
  32. include ST_ROOT . '/Views/header.tpl.php';
  33. include ST_ROOT . '/Views/main.tpl.php';
  34. include ST_ROOT . '/Views/footer.tpl.php';
  35. }