The simplest solution is to just include PHP code inside the <script></script> section of your HTML templates / web pages because chances are, the file extension is .php:
<script> var jsVar = "<?php echo $phpVar ?>"; </script>
Even if it’s not ending in .php, such as in .html or .htm, you can configure your server to parse all .html or .htm files for any PHP code, though with a little extra server burden.
Another solution for this is to make your server parse all files ending in .js. Just create a .htaccess if it doesn’t exist in the directory in which you wish to include and run PHP code inside all .js files. Add these lines at the end of .htaccess:
AddType application/x-httpd-php .js AddHandler x-httpd-php5 .js <FilesMatch "\.(js|php)$"> SetHandler application/x-httpd-php </FilesMatch>
You could also create a file with a .php extension that returns all the proper headers, and then alias it with mod_rewrite.
This is an alternative to configuring your server to parse .js files if you don’t have the ability to do this.
the best method good bro
Pingback: 10+ .htaccess snippets to optimize your website « The Blog of Jordan Rynard
Pingback: .htaccess snippets to optimize your website « shankarsoma; Social Media & Digital Marketing Blog
Pingback: Top .htaccess snippets to optimize your website | W3Corner
Pingback: htaccess SEO: 10 suggerimenti utili « Seo
Pingback: 11 tuyá»‡t chiÃªu .htaccess tá»‘i Æ°u website cá»§a báº¡n | V-Share Team
Pingback: 10 Useful .htaccess snippets Web Developer and Designer Should Know - Design Freebies
I’ve got an easy way without disturbing the .htaccess file. Is is very easy, just see how this works…
//And this is the code of some_file.php file
Pingback: 10+ .htaccess snippets to optimize your website | Justin Design and Coding Reference
Hey, thanks a lot. This saved me a lot of time in my project. Great job!
Thanks! Worked in codeigniter too 🙂
Comments are closed.