function.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /**
  3. * 获得当前的域名
  4. *
  5. * @return string
  6. */
  7. function get_domain()
  8. {
  9. /* 协议 */
  10. $protocol = (isset($_SERVER['HTTPS']) && (strtolower($_SERVER['HTTPS']) != 'off')) ? 'https://' : 'http://';
  11. /* 域名或IP地址 */
  12. if (isset($_SERVER['HTTP_X_FORWARDED_HOST']))
  13. {
  14. $host = $_SERVER['HTTP_X_FORWARDED_HOST'];
  15. }
  16. elseif (isset($_SERVER['HTTP_HOST']))
  17. {
  18. $host = $_SERVER['HTTP_HOST'];
  19. }
  20. else
  21. {
  22. /* 端口 */
  23. if (isset($_SERVER['SERVER_PORT']))
  24. {
  25. $port = ':' . $_SERVER['SERVER_PORT'];
  26. if ((':80' == $port && 'http://' == $protocol) || (':443' == $port && 'https://' == $protocol))
  27. {
  28. $port = '';
  29. }
  30. }
  31. else
  32. {
  33. $port = '';
  34. }
  35. if (isset($_SERVER['SERVER_NAME']))
  36. {
  37. $host = $_SERVER['SERVER_NAME'] . $port;
  38. }
  39. elseif (isset($_SERVER['SERVER_ADDR']))
  40. {
  41. $host = $_SERVER['SERVER_ADDR'] . $port;
  42. }
  43. }
  44. return $protocol . $host;
  45. }
  46. /**
  47. * 获得网站的URL地址
  48. *
  49. * @return string
  50. */
  51. function site_url()
  52. {
  53. return get_domain() . substr($_SERVER['PHP_SELF'], 0, strrpos($_SERVER['PHP_SELF'], '/'));
  54. }
  55. //导出称word
  56. function output_word($data,$fileName=''){
  57. if(empty($data)) return '';
  58. $data = '
  59. <html xmlns:v="urn:schemas-microsoft-com:vml"
  60. xmlns:o="urn:schemas-microsoft-com:office:office"
  61. xmlns:w="urn:schemas-microsoft-com:office:word"
  62. xmlns="http://www.w3.org/TR/REC-html40">
  63. <head><meta http-equiv=Content-Type content="text/html;
  64. charset=utf-8">
  65. <meta name=ProgId content=Word.Document>
  66. <meta name=Generator content="Microsoft Word 11">
  67. <meta name=Originator content="Microsoft Word 11">
  68. <xml><w:WordDocument><w:View>Print</w:View></xml></head>
  69. <body>'.$data.'</body></html>';
  70. $filepath = tmpfile();
  71. $len = strlen($data);
  72. fwrite($filepath, $data);
  73. header("Content-type: application/octet-stream");
  74. header("Content-Disposition: attachment; filename={$fileName}.doc");
  75. header('Content-Description: File Transfer');
  76. header('Content-Type: application/octet-stream');
  77. header('Content-Disposition: attachment; filename='.$fileName.'.doc');
  78. header('Content-Transfer-Encoding: binary');
  79. header('Expires: 0');
  80. header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  81. header('Pragma: public');
  82. header('Content-Length: ' . $len);
  83. rewind($filepath);
  84. echo fread($filepath,$len);
  85. }
  86. function clear_runtime($path = RUNTIME_PATH){
  87. //给定的目录不是一个文件夹
  88. if(!is_dir($path)){
  89. return null;
  90. }
  91. $fh = opendir($path);
  92. while(($row = readdir($fh)) !== false){
  93. //过滤掉虚拟目录
  94. if($row == '.' || $row == '..'|| $row == 'index.html'){
  95. continue;
  96. }
  97. if(!is_dir($path.'/'.$row)){
  98. unlink($path.'/'.$row);
  99. }
  100. clear_runtime($path.'/'.$row);
  101. }
  102. //关闭目录句柄,否则出Permission denied
  103. closedir($fh);
  104. return true;
  105. }
  106. //获取ip
  107. function getIPaddress(){
  108. $IPaddress='';
  109. if (isset($_SERVER)){
  110. if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])){
  111. $IPaddress = $_SERVER["HTTP_X_FORWARDED_FOR"];
  112. } else if (isset($_SERVER["HTTP_CLIENT_IP"])) {
  113. $IPaddress = $_SERVER["HTTP_CLIENT_IP"];
  114. } else {
  115. $IPaddress = $_SERVER["REMOTE_ADDR"];
  116. }
  117. } else {
  118. if (getenv("HTTP_X_FORWARDED_FOR")){
  119. $IPaddress = getenv("HTTP_X_FORWARDED_FOR");
  120. } else if (getenv("HTTP_CLIENT_IP")) {
  121. $IPaddress = getenv("HTTP_CLIENT_IP");
  122. } else {
  123. $IPaddress = getenv("REMOTE_ADDR");
  124. }
  125. }
  126. return $IPaddress;
  127. }