Saturday, December 31, 2011

Protect javascript code using php

Leave a Comment
protect or hide your javascript code using this short and simple script, thus you can have your own script with privacy.


the operation is same to my previous post. "Protect Css file using php".


here's the code:

*******************************************************
ingredients: 2 php page (page1.php, page2.php)

***************** page1.php ****************************
<?php
session_start();
header("Cache-Control: no-cache, must-revalidate"); 
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past 
$_SESSION['javascript']="key"; /* this is the session value, you can change the word "key" to any word you wish. */
?>
<html>
<script type="text/javascript" src="/page2.php"></script>
</html> 
******************************************************

************** page2.php ********************************
<?php
session_start();
header("Cache-Control: no-cache, must-revalidate"); 
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past 
$key=$_SESSION['javascript'];
if($key!='key') // if the "$key" is not equal to session value,the word "Protected Page" will appear
{

die('<h1>Protected Page</h1>');

} // else, the javascript code will extract

session_destroy();  // destroy the curent session

?>
alert("HELLO WORLD!"); // you can add more js codes here 

******************************************

 so, if a visitor tries to access page1.php the output will be a message box with "HELLO WORLD!" like this one:




and if he/she access the page2.php which contained the javascript code, a word "protected page" will appear:





This is not the best solution, but always try to do an  experiment. perhaps you can make a better solution(s) than this article.
Post by :Pinoyphp

0 comments:

Post a Comment