Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compress a html file with generated from multiple php source file without aid of apache module?

Tags:

html

php

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 ?

like image 251
The Mighty Programmer Avatar asked Oct 02 '22 04:10

The Mighty Programmer


2 Answers

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

like image 93
Zac Avatar answered Oct 10 '22 21:10

Zac


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

<?php
   ob_start("ob_gzhandler");
?>
like image 27
undone Avatar answered Oct 10 '22 20:10

undone