5

eg.

file structure

+Site
|   +private_files
|   |  header.php
|   |  footer.php
|   |  head.php
|   |  anlytics.php
|   |  blah1.php
|   |  blah2.php
|   |  blah3.php
|   index1.php
|   index2.php
  • every serving file requires header.php,footer.php, head.php, analytics.php.

    besides

  • index1.php requires blah1.php and blah2.php.
  • index2.php requires blah2.php,blah3.php.

page structure

<!DOCTYPE html>
<html>
     <?php include 'private/head.php'?>
<body>
     <?php include 'private/header.php'?>
//rest code
     <?php include 'private/footer.php'?>
</body>
</html>

How compressed HTML files can be served without aid of apache module ?

10
  • Hi, you can use online compressor for html, js and css and then include in index.php Commented Dec 16, 2013 at 4:46
  • Do you want to compress an HTML output or you have compress HTML file which you want to send to user? Commented Dec 16, 2013 at 4:57
  • @viswanath polaki Since site is dynamic,each time litle bit html differ, I can't make use of online services :/ Commented Dec 16, 2013 at 5:00
  • @undone I want to compress html what it's dynamic in nature .. I want a automatic solution. Commented Dec 16, 2013 at 5:01
  • Another question: header.php is included before any kind of output? right? Commented Dec 16, 2013 at 5:02

4 Answers 4

2
+25

I am not very sure about the reason behind "without aid of apache module". If you mean you do not have the access (or permission) to change apache settings. You can still use gzip from php itself.

Just in your header.php (the first included php which could export any content in your script)

ini_set('zlib.output_compression_level', 1); //the compress level you want, 1 is lowest
ob_start('ob_gzhandler');

For further reading of ob_gzhandler - http://php.net/ob_gzhandler

If you mean you do not want even use any system command which supported by apache or other system modules. You just look for a solution complete by php itself. Yes you can use ob_get_clean() get all the content, then apply some gzip compress to the string, and echo it after. Also, change header to let browser know content is gzip(ed). However, I do not think you would like the performance. Besides, you need to lots extra work, feels completely wasted ;-)

For further reading of ob_get_clean - http://php.net/ob_get_clean

Sign up to request clarification or add additional context in comments.

3 Comments

Yes.. No Apache Module access.. I've tried above method/code on local machine but 'Google Page Speed' warned me to enable compression :/
Bad news is: you have to enable apache [mod_deflate]; besides, Google Page Speed is not just about your html page itself, you also need compress most of your page elements, such as: js, css; you need turn on [mod_deflate] in apache settings files. The easiest way to determine compress, is check your page compress status in your browser, such as: use Chome, enable [developer tools], check the [Network] tab, try reload page, if you see the [size] is smaller than [content], that means it is compressed.
The presented solution is not 100% correct. zlib compression is favoured over ob_gzhandler, which is written in the same manual page the author refers to. Please use the version proposed on php.net/manual/en/… and check if the included code uses the flush() function. Using it will break compression.
1

put this before <!DOCTYPE html> <html><?php include 'private/head.php'?> <body>:

<?php
   ob_start("ob_gzhandler");
?>

Comments

1

I will break your question into two parts. 1. Compressing your Css & Js in the site and this can even be done on the fly. Please check out http://manas.tungare.name/software/css-compression-in-php/ 2.Compression of HTML files on the fly. In this case have you tried services like cloudflare. They offer compression on the fly. A PHP case would involve curl, by creating a new file that first receives the request. Then the file gets the url content via curl. The receive content can then be compressed then echoed. This is pretty crude.

Comments

0

you can use HTML Minify

 $filter = new Zend_Filter_Minify_Html();
 return $filter->filter($view->render($name));

Its not gzip compression , but reduce size of page
Click to Zend Module here

2 Comments

i have already given the link please check. there are some other minification tool also exits
I have not used Zend framework before. solution you told is dependent on other Zend modules ? I have to copy files and include in project but I am unable to find what exactly files I need. One is obvious to i.e "copy content of link you provided " It was written it depends upon Zend_Filter (not found) Zend_Filter_Exception (not found) Zend_Filter_Minify_CSS (Found). Zend_Filter_Minify_JavaScript (Found). Also, I have doubt will it work for HTML5, since no HTML5 tag found in code:?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.