[solved]Modding PHP5 for new file extensions

Alles, was PHP betrifft, kann hier besprochen werden.

[solved]Modding PHP5 for new file extensions

Postby SilentThunderStorm » 16. September 2012 19:43

What I want to do is to turn on PHP5 interpretation for both .css and .js files, while still returning the proper MIME header.

I want to do this so that I can keep the pages I design extremely neat, minimize http requests, and maximize caching.

In other words:

<link rel='stylesheet' href='dynamicsheet.css'>
<script src='dynamicscript.js'></script>

The requested files would be interpreted as .php allowing them to determine the client platform, browser, and the page requested (as well as anything else you might think of), and intelligently load and concatenate the files, as well as handle load order, dependencies, etc. etc. etc.

Basically, I want to be able to handle these files the same way one handles a make file, or a manifest.

This way, I could tailor the css sheets for the device that was loading the site (for example, tablets would get the tablet .css, etc.) and would cache the sheet needed for that device.

Same with scripts... the scripts loaded by the backend would be dynamically built to match the client device and browser, and would be cached on that machine.

Similarly, it would allow me to break down the .css and .js into more easily manageable files on server side, while the file that the browser actually loaded ('dynamicsheet.css', for example) would then properly manage and compile the required stylesheets and scripts needed for that specific page.

In this way, I could have a single http request load up jQuery, Less, Bootstrap, and both site-wide and page specific scripts...all in a single file, and including proper caching.

Currently, the normal solution to this is requesting and transmitting huge chunks of code that don't even apply to that machine, only to not used it... and doing so through a ton of http requests.

There are so many good reasons for dynamically generating .css and .js that I can't believe it has not been a standard practice for years already.

My problem is that while I can program both client side and web stuff... I have very little knowledge or experience when it comes down to the actual internals of a web server.

Everywhere that I have seen someone ask a question even remotely like this they are told "don't do that", or "just name them all .php". Often the server overhead of interpreting any file other than .php is seen as a "waste of server resources"... but for the resons listed above, I disagree.

Naming all the .css and .js files to .php would leave them with the wrong header information, which would generate ugly errors on the client end.

As for 'wasting server resources' by interpreting files that don't require it? Two points...First, *MY* files would require it, so there is no waste... but also Secondly, it should be possible to allow .css and .js to remain as is, but to allocate new file types, such as .ccss and .cjs (compiled css and compiled javascript), which *are* interpreted and which have their own proper MIME type.

Does anyone know how to convince the server to do so?

I have heard that I need to modify the httpd.conf, OR the php5.ini, OR etc, etc etc.... yet in each case, no accurate or explicit instructions follow.

Does anyone have a clue how to get this done?

I can't believe it is really THAT hard to do.

Any help would be greatly appreciated, with much kharmic love being fed to Buddha in your name and all.... puppies will smile.
SilentThunderStorm
 
Posts: 4
Joined: 22. March 2012 19:13
XAMPP Version: 5.6.3
Operating System: Windows 8 Consumer Preview

Re: Modding PHP5 for new file extensions

Postby Altrea » 16. September 2012 21:12

Hi SilentThunderStorm,

From my point of view, building or processing a css or js file on runtime would not make sense. You can't properly cache these requests and sending each asset request through your php interpreter will slow down your requests instead of speed them up.

If you want to redruce requests and use compression and caching use a script like Minify or Combine
Make use of good expire headers and use recommend techniques like Graceful degradation or Progressive enhancement to give your clients exact the functionality they can use.

If you still want to parse css or js files with your php interpreter, see this example for setting the correct handler.

best wishes,
Altrea
We don't provide any support via personal channels like PM, email, Skype, TeamViewer!

It's like porn for programmers 8)
User avatar
Altrea
AF Moderator
 
Posts: 6830
Joined: 17. August 2009 13:05
XAMPP Version: 5.5.19
Operating System: Windows 10 Pro x64

Re: Modding PHP5 for new file extensions

Postby SilentThunderStorm » 17. September 2012 22:46

| You can't properly cache these requests and sending each asset request through

Well, the browser would certainly cache them, as it would have literally no way of knowing that the file it requested ('whetever.css', for example) was dynamically generated. It would also have no way of knowing that a different browser on a different device would be receiving a completely different file.

Look at images generated by GD... the browser assumes that those are normal images, and chaches them... how would a dynamic script file be any different?

| your php interpreter will slow down your requests instead of speed them up.

This is debatable... for example, if you working on a large site, which takes more server overhead? Parsing a file so that you only deliver to a client the files that it needs in a single http request? Or serving dozens of http requests to serve a bunch of completely unneeded and probably completely unused code?

IE Shims (running on Chrome), CSS reset sheets (unneeded when the css was written for the specific platform), scripting for touch interfaces of desktops, modernizr running on a modern browser... the list of code that a browser might request and download, but not run, is endless.

This argument is also avoided by simply defining new file types, such as the .ccss and .cjs above... then basic, simple .css and .js can be named left alone, and would not be parsed, while the files that need to be dynamic can be.

| If you want to redruce requests and use compression and caching use a script like Minify or Combine

These would not produce the needed behavior.

While combining multiple files under a single http request is one of the main advantages I seek, this would do literally nothing for customizing the package on a browser by browser basis.

The primary advantage, here, is to offload to the client *ONLY* what the client needs... not a large, precompiled, generic library.

| If you still want to parse css or js files with your php interpreter, see this example for setting the correct handler.

Thank you, however... this looks like exactly what I was looking for!

I am surprised, however, that the header would have to be handled by the PHP directly in the file... I had assumed that you could simply plug in an additional line to the .htaccess file to ensure the proper header.

Again, many thanks!
SilentThunderStorm
 
Posts: 4
Joined: 22. March 2012 19:13
XAMPP Version: 5.6.3
Operating System: Windows 8 Consumer Preview

Re: Modding PHP5 for new file extensions

Postby SilentThunderStorm » 17. September 2012 23:30

OK... got it, I think:

AddHandler application/x-httpd-php .ccss
AddHandler application/x-httpd-php .cjs
AddType text/css .ccss
AddType application/x-javascript .cjs

Add this to .htaccess file, and any .ccss or .js files served from underneath it will be interpreted as PHP, but return with the proper MIME types.

Thank you for the help!
SilentThunderStorm
 
Posts: 4
Joined: 22. March 2012 19:13
XAMPP Version: 5.6.3
Operating System: Windows 8 Consumer Preview


Return to PHP

Who is online

Users browsing this forum: No registered users and 3 guests