Просмотр файла gzdoc.php

Размер файла: 3.05Kb
  1. <!-- автор TheJove http://profiwm.ru/us2462 -->
  2. <?
  3. /*
  4. ** Title.........: PHP4 HTTP Compression Speeds up the Web
  5. ** Version.......: 1.20
  6. ** Author........: catoc <catoc@163.net>
  7. ** Filename......: gzdoc.php
  8. ** Last changed..: 18/10/2000
  9. ** Requirments...: PHP4 >= 4.0.1
  10. ** PHP was configured with --with-zlib[=DIR]
  11. ** Notes.........: Dynamic Content Acceleration compresses
  12. ** the data transmission data on the fly
  13. ** code by sun jin hu (catoc) <catoc@163.net>
  14. ** Most newer browsers since 1998/1999 have
  15. ** been equipped to support the HTTP 1.1
  16. ** standard known as "content-encoding."
  17. ** Essentially the browser indicates to the
  18. ** server that it can accept "content encoding"
  19. ** and if the server is capable it will then
  20. ** compress the data and transmit it. The
  21. ** browser decompresses it and then renders
  22. ** the page.
  23. **
  24. ** Modified by John Lim (jlim@natsoft.com.my)
  25. ** based on ideas by Sandy McArthur, Jr
  26. ** Usage........:
  27. ** No space before the beginning of the first '<?' tag.
  28. ** ------------Start of file----------
  29. ** |<?
  30. ** | include('gzdoc.php');
  31. ** |?>
  32. ** |<HTML>
  33. ** |... the page ...
  34. ** |</HTML>
  35. ** |<?
  36. ** | gzdocout();
  37. ** |?>
  38. ** -------------End of file-----------
  39. */
  40. ob_start();
  41. ob_implicit_flush(0);
  42. function CheckCanGzip(){
  43. global $HTTP_ACCEPT_ENCODING;
  44. if (headers_sent() || connection_aborted()){
  45. return 0;
  46. }
  47. if (strpos($HTTP_ACCEPT_ENCODING, 'x-gzip') !== false) return "x-gzip";
  48. if (strpos($HTTP_ACCEPT_ENCODING, 'gzip') !== false) return "gzip";
  49. return 0;
  50. }
  51. /* $level = compression level 0-9, 0=none, 9=max */
  52. function GzDocOut($level=3, $debug=0){
  53. $ENCODING = CheckCanGzip();
  54. if ($ENCODING){
  55. print "\n<!-- Use compress $ENCODING -->\n";
  56. $Contents = ob_get_contents();
  57. ob_end_clean();
  58. if ($debug){
  59. $s = "<center><font style='color:#C0C0C0;
  60. font-size:9px; font-family:tahoma'>Not compress
  61. length: ".strlen($Contents)."; ";
  62. $s .= "Compressed length: ".
  63. strlen(gzcompress($Contents, $level)).
  64. "</font></center>";
  65. $Contents .= $s;
  66. }
  67. header("Content-Encoding: $ENCODING");
  68. print "\x1f\x8b\x08\x00\x00\x00\x00\x00";
  69. $Size = strlen($Contents);
  70. $Crc = crc32($Contents);
  71. $Contents = gzcompress($Contents, $level);
  72. $Contents = substr($Contents, 0, strlen($Contents) - 4);
  73. print $Contents;
  74. print pack('V', $Crc);
  75. print pack('V', $Size);
  76. exit;
  77. }else{
  78. ob_end_flush();
  79. exit;
  80. }
  81. }
  82. ?>