This has only really been tested on Media Temple (gs) servers.
<FilesMatch "^.*?api.*?$"> SetHandler php5-script </FilesMatch>
In that above example, any file that includes the string “api” will be processed as PHP. Feel free to alter that RegEx.
Could you use this for stylesheets?
yes you can use that for stylesheet.
code will be like this…
Nice one can you give some explanatory example how all its work
i dont think i can understand this without looking at a working example
If I’ve understood it correctly, an example would be to have a file called:
Link to it in your HTML:
// JS Code
// Do stuff with variable
It quite valuable thing you shared as a trick and in some cases it will be really needed. However, before using it we have to clear what is more favorite for us: performance or this way to get PHP vars inside JS. Because when applying this trick, JS file is going to load much more slower, because it is loading as PHP file.
In some other cases, it would be much faster… Avoiding a trip back to the server to fetch initialization data, which would also be php parsed.
In the case of a single page app for example where you might need a configuration file loaded inside your app.js file of requires.
On the other hand, you can put it in a data-attribute of a hidden div in your index.php file.
can u post me all lastest updates in my mail,Thanx
Awesome trick, :) I love it. Thanks
Please explain more because it’s not enough to understand.
I’ll give you the example that brought me to this page.
Here’s how I did it:
myfolder/myscript.js is what all the pages link to. so in ‘myfolder’, I made an htaccess file that has Chris’s code in it only I changed the ‘api’ to ‘MyWhackyExpression’.
Nice trick and thanks for sharing! However I have a question, usually the .js and .css files tend to get cached by the browsers. In this case all the php code written in the file will be executed just once, first time the script file is loaded. Any ideas how to overcome this problem?
you can do something like this:
Remember that caching is a good thing. (Caching = fast = good).
Appending a random string to the end of file names does break caching, so that might be good for development, but not so much for “live” websites. If you want to go down this road I might suggest setting a variable and using that variable at the end of your file names, so you can adjust in one place to break cache, but not break cache every single time.
it was a problem for Iulian, so, problem solved :)
Please I want to know if I can change the photo script if my cpanel has higher vasion of php
Why not include a php file like it was js
you still can run php inside js, you don’t need to mess with htaccess and you don’t compromise performance.
That would work, but I’d be careful saying “you don’t compromise performance.” You’re asking your server to process this file every single time it’s requested, rather than just serve it up. You might be fine doing it this way, but I suspect it’s not a common practice for that reason.
Annnnnd. Now I realize the snippet this comment is posted on is specifically about doing this exact thing. So yeah good call, you can do it just as you’ve described.
you’re asking the server to process the php/js file every single time it’s requested too… i don’t see any difference from the htaccess trick…
It wasn’t a real problem. I just wanted to have a better image of the entire concept of using php in css and js. Personally I wouldn’t use this since is a performance issue as it was proved, but it’s a cool trick to have in your sleeve. :)
what about security if i am going to use such this hacks ??
Messing around today, i noticed this works, but only in Chrome:
To make that route we add this to some accessible class:
No rewrites, masking, files, or template engines needed.