Welcome to WebmasterWorld Guest from 54.211.101.8

Forum Moderators: Ocean10000 & incrediBILL & phranque

Executing js file as php?

   
11:58 am on Jul 19, 2013 (gmt 0)

10+ Year Member



I must be using wrong search terms or something but everything I've tried so far did not work.

The last thing I tried was
<Files "filename.js">
SetHandler application/x-httpd-php
</Files>

It still returns code as plain text. What is the correct rule to use? I've verified it matches the right file by adding an extra header and this was returned correctly. I'm using Apache 2.2.24 and PHP 5.3.25.
1:12 pm on Jul 19, 2013 (gmt 0)

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



this thread might give you some ideas to try - Serving PHP with Apache: AddType and AddHandler:
http://www.webmasterworld.com/apache/4557229.htm [webmasterworld.com]
3:25 pm on Jul 19, 2013 (gmt 0)

10+ Year Member



I still didn't find a working version. Instead I opted for an actual php file with js alias like

RewriteRule ^filename\.js$ filename.php
8:46 pm on Jul 19, 2013 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



It may not have answered your original question, but that's probably a better approach anyway :) If you can't be open and forthright with your own server, when can you be honest?
12:36 am on Jul 20, 2013 (gmt 0)

WebmasterWorld Senior Member Top Contributors Of The Month



I think the .js to .php rewrite is the way I would go too, personally.

A couple of notes...

1.) I'd make sure to use a server relative path to the file, just to make sure the location is always correct and also add an [L] to the rule, which should always be used unless you know exactly why you don't want it.

RewriteRule ^filename\.js$ /filename.php [L]

2.) I'd make sure I set a header so there was no question a browser interpreted the file and executed it as .js rather than something else. (I've run into issues previously with serving images via PHP to set an expires header and if you don't have the headers set correctly by the PHP file so the browser knows it's an image some browsers "choke" and won't display them.)

# At the top of the PHP file
header("Content-type: text/javascript");
3:20 am on Jul 20, 2013 (gmt 0)

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



welcome to WebmasterWorld, JD_Toims!
3:24 am on Jul 20, 2013 (gmt 0)

WebmasterWorld Senior Member Top Contributors Of The Month



Thanks phranque!
 

Featured Threads

My Threads

Hot Threads This Week

Hot Threads This Month