Web Development

How to Hide your JavaScript with PHP!

How To Hide Your JavaScript With PHP!

I hate the thought of JavaScript that you don’t want to be copied being copied… Here’s a little script that will hide it so that no one can copy it!

The Script

On the page where the JavaScript is placed, add:

<?
session_start();

if (!isset($_SESSION['access'])){
$_SESSION['access'] = true;
$access = true;
}
?>

<html>

<head>

<script language=”JavaScript” src=”script.php”></script>

</head>

<body>

Body goes here…

</body>

</html>

And now create a new file called script.php and place your JavaScript there:

<?
session_start();
if($access)
{
header("Content-type: text/javascript");
?>
//any javascript can go in here
alert("woohoo it works at last!");
<?
$access = false;
}
?>

As you can see it uses a session. When you open the page where the JavaScript is placed it creates a session which allows the JavaScript to be viewed. But if you open script.php by its self, no session is created!

Try opening script.php in your browser window and you’ll notice you can’t view the code!

Enjoy!

About the author

Written by Ben Sinclair.

http://www.webmaster-resources101.com Ben Sinclair is the Webmaster of Webmaster Resources 101 (http://www.webmaster-resources101.com/) and Webmaster Forums 101 (http://www.webmaster-forums101.com/)

If you found this post useful you may also want to check these out:

  1. Building a Javascript Array
  2. JavaScript Tutorial Part I- Some Basics
  3. Making a JavaScript Tree Menuing System
  4. Basic JavaScript Date and Time Functions
  5. Presenting a “Good-bye” Message Without Popups Using JavaScript
  6. JavaScript Double Click Trapper
  • http://id.benjaminoakes.com/ Benjamin Oakes

    It’s a good idea in spirit, but the JavaScript will still be cached (and viewable) by the browser. There’s not much of a way around that. Minification will be more worthwhile.

    • Anonymous

      Approved