using PHP to obfuscate Javascript

Posted on 2008-06-24
Last Modified: 2013-11-05
Hello experts--

I have a web application loads 20k lines of Javascript one time into a huge combined .JS file that I would like to obfuscate for protection.  Ideally, I'd like to download a nifty library or function that someone has wrote for  javascript obfuscation in PHP, but I am open to suggestions.   Any ideas on how I can accomplish this?
Question by:Discomonkey
  • 3
  • 2
LVL 82

Expert Comment

ID: 21859386
Refer to the PHP Version of packer:
LVL 48

Expert Comment

ID: 21859399

Author Comment

ID: 21859930
My initial tests seem to be failing, I am getting a bunch of <li> </li> in my source?

$js = $os->build_javascript(); // returns string
$packer = new JavaScriptPacker($js, 'None', true, false);
$packed = $packer->pack();
print $packed;
3<img src="'+Ext.BLANK_IMAGE_URL+'"class="icon-pref-autorun"/>\
4<a id="viewShortcuts"href="#">Shortcuts</a><br />\
5<span>Choose which applications appear in your shortcuts.</span>\
8<img src="'+Ext.BLANK_IMAGE_URL+'"class="icon-pref-autorun"/>\
9<a id="viewAutoRun"href="#">Auto Run Apps</a><br />\
10<span>Choose which applications open automatically once logged in.</span>\
13<img src="'+Ext.BLANK_IMAGE_URL+'"class="icon-pref-quickstart"/>\
14<a id="viewQuickstart"href="#">Quick Start Apps</a><br />\
15<span>Choose which applications appear in your Quick Start panel.</span>\

Open in new window

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

LVL 82

Expert Comment

ID: 21861507
Where is the javascript code you want to pack. Assuming you wanted to pack the following:
var x="hello world";

then you would need to "feed" the javascript statements to the packer (notice how the PHP variable $script is being assigned the actual javascript code which is then fed to JavaScriptPacker):

$script = <<<EOF_JS
var x="hello world";
$packer = new JavaScriptPacker($script, 'None', false, true);
$packed = $packer->pack();
echo $packed;

Open in new window


Author Comment

ID: 21861776
$os->build_javascript() is a method that returns a very long string that represents a combination of several javascripts.   I call this method from my head like this like:

<script type="text/javascript"  src="javascript.php"></script>

so it pulls all of the javascript I need.  Ordinarily, I just print the response, but I am trying to use the packer now to pack it then print it.

Is it valid to feed my javascript the way I am currently doing so?  I realize that some of the javascript variable names that are in my javascript code are dependent on values in my database, which will make obfuscation of those javascript variables impossible!
LVL 82

Accepted Solution

hielo earned 250 total points
ID: 21866055
>>Is it valid to feed my javascript the way I am currently doing so?  
IF infact $os->build_javascript() returns only javascript code then yes.

>>I realize that some of the javascript variable names that are in my javascript code are dependent on values in my database
Again, the code you need to feed to packer is only the javascript code - the code resulting AFTER evaluating your db data/variables.

As for the code above (ID:21859930), perhaps you have invalid javascript code. What are those trailing \ supposed to be? IF you are constructing javascript dynamically, then you can't have a single statement span across multiple lines (like you do in PHP) without concatenating them.

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question