Skip to content

Make your js files hard to read when release to public by obfuscated it

Notifications You must be signed in to change notification settings

riyantowibowo/javascript-obfuscator-with-php

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Javascript Obfuscator With PHP

This PHP file is using obfuscator.io to obfuscate js files.

Installation

Install the javascript-obfuscator package, ensure you have node.js and npm installed

npm i -g javascript-obfuscator

Define your js directory in obfuscate.php file line 3

$directory = __DIR__ . '/js';

Define your prefix in obfuscate.php file line 8. Prefix is used to indicate which js files will be obfuscated, so you have to add prefix to your js files first. Create your own prefix that someone else can't guess it.

$prefix = "staging_";

Setup

Configure for your release or production mode, for this example there is a function called getObfuscateJsFileName() in index.php file. Set your $prefix as you defined before in this function and set $serverName by fill your domain.

function getObfuscateJsFileName($fileName) {
	// Your prefix in obfuscate.php
	$prefix = "staging_";

	// Get the server name
	$serverName = $_SERVER['SERVER_NAME'];

	// Check if the file name starts with the obfuscator prefix
	if (preg_match('/^'.$prefix.'(.*)\.js$/', $fileName, $matches)) {

		// Return obfuscated file name if in release or production mode
		if ($serverName === 'yourdomain.com') {
			// Remove the prefix and add "min" before the file extension
			return $matches[1] . '.min.js';
		}

	}

	// Return the original file name if in staging or development mode like for your localhost
	return $fileName;
}

Everytime you want to call the obfuscated js files, in script tag you need to call this function, example:

<script src="js/<?php echo getObfuscateJsFileName('staging_filename.js'); ?>"></script>

Execute

Run or open obfuscate.php

Credits

https://github.com/javascript-obfuscator/javascript-obfuscator

https://github.com/pkprajapati7402/Ping-Pong-Game

About

Make your js files hard to read when release to public by obfuscated it

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published