My JavaScript book is out! Don't miss the opportunity to upgrade your beginner or average dev skills.

Friday, December 22, 2006

[PHP] New version of GzOutput class

I've just updated my recent GzOutput.class.php (more recent than last file published on Ajaxian :D) and tested successful on many browsers.

The difference from other cache-manager files, functions or classes is that GzOutput is totally indipendent from folders or page type, You could use them to increase JSON responses (using text/plain as Content-Type) as well as XML, (X)HTML, CSS or every kind of document You need.

You could combine this class with a JavaScript compressor (to improve download speed) and you don't need to care about output changes because ETag is based on sha1 and not on every mktime of each file (it means better server performances).

The only think that You need to remember is that You don't need to write everything before to use this class (empty spaces, some char or pages can't be write before its usage).

Four public methods are really simple to use:

create, sending an output string and a Content-Type type to use cache if it's available (if not, forces next download to use cache)

createNew, sending an output and a Content-Type to force everytime the download (it's a must for Ajax interactions with XML, JSON or serialized strings)

createFromList, sending an array of files and a Content-Type to create a single generic output file including each file content and using create properties for output

createNewFromList, sending an array of files and a Content-Type to create a single generic output file including each file content and using createNew properties for output

That's all, do You like it ? :-)

16 comments:

Anonymous said...

ottimo... avevo visto l'articolo l'altro ieri su ajaxan e lo avevo provato cmq implementerò questo metodo sul mio revokebb

Bello grazie

Andrea Giammarchi said...

Thank You but remember that it's not the same file (and I'm not the same author) published on Ajaxian, this is another class that doesn't do anything automatically ( that's why I prefere this personal version :P ).

Ajaxian posted file uses dedicated folders and output just for css or JS while with this class You can create every other kind of output too ;-)

Anonymous said...

okok i get it...

do i need to speak in english? :-)

Anonymous said...

andrea la classe ha un errore... provala in altervista per esempio o qualche altro server... aggiunge un "<" in piu grazie

Andrea Giammarchi said...

Marco, please write in English and please tell me which error did You find on Altervista ... I've successful tested this class on PHP 5.1, 5.2 and 4.3 without problems and it doesn't modify or add any part of JavaScript then Your error should be on your JS and not on this class.

However, link me an example page or a JavaScript code that doesn't work if You think the error is created by this class, regards and happy new Year :-)

Anonymous said...

http://hwmod.altervista.org/test/inc/js/cachejs.php?act=index.tpl

here and emtpy array = http://hwmod.altervista.org/test/inc/js/cachejs.php?act=

there is a character "‹"

Andrea Giammarchi said...

Marco, I hope You're not talking about JavaScript Compressor because this class is totally different and shouldn't require any kind of JS compressor (You could crunch or just clean code removing comments but JavaScript Compressor as Packer and others are not useful if You adopt this solution to solve size problems :-))

Anonymous said...

i am talking about your script. Cmq i solved the error changing one your function

Andrea Giammarchi said...

... changing what ? :-)

Anonymous said...

lol your code isn't compatible with some host... so i deleted gzencode and i put a simple ob_start("ob_gzhandler"):

Andrea Giammarchi said...

lol ... as I thought ... You probably didn't read that this class has been created to don't use ob_gzhandler that's not the same thing :D

However, I think that if your free host doesn't allow developers to use zlib probably the ob_gzhandler doesn't do anything too then if You have solved with ob_start (very old solution) it's quite strange for me but I hope a good thing for You ... and I suppose You've not changed any part of function, just removed this class.

See you :-)

Anonymous said...

i modified it in order to see your changes... if you try your code in altervista.org you will see that they have zlib and all the things...

if your code doesn't work in all the situations it isn't an alternative way.

Andrea Giammarchi said...

Sorry Marco, but I've successful tested my code in 6 different hosts with 6 differents configurations ... I'm sorry for Altervista, I've never used them but if gzencode is not usable it's not my problem ... I suppose ... then simply use old ob_gzhandler (because other codes that use gzencode shouldn't work as mine).

Anonymous said...

Hi Andrea,
Do you think it's a bad idea to implement a cache system like the Rakaz solution?

Andrea Giammarchi said...

You can do them by yourself :-)

However, it will be probably implemented as static method on next release.

Anonymous said...

You're right :D

Thanks