Sunday, August 5, 2012

Protect Your CSS and Javascript Codes




Update 20th of March 2013: Here's a updated version to include a simple minification before sending your Javascript / CSS codes to the browser especially Chrome:



http://webtrick101.blogspot.com/2013/03/protect-your-external-css-and.html


Actually there is no 100% way to protect your CSS and Javascript codes as if the person who intended to copy is using Google Chrome browser, your codes will be shown no matter how you protect them. But there is a good thing about protecting your CSS and Javascript codes. Google Chrome won't display the comments or 100% your original codes. Your codes will be made conformed to Chrome standard before being displayed. Without the 100% original codes, the intruder won't be able to copy your codes that easily.

Here's how you use Chrome to view any CSS and Javascript:


Simply right click on any webpage


Google Chrome's Inspect Element facility can inspect anything on a web site including your protected CSS and Javascript. Anyway, it is still a good practice to protect your CSS and Javascript if you think your hardwork should be protected and shared only someone asks you nicely for the codes. Here they are in the following.

The HTML:


.
.
.
<link rel="stylesheet" type="text/css" href="mycss.php">
<script language="JavaScript" src="myjs.php"></script>
.
.
.


The PHPs:


For mycss.php:

<?php

error_reporting(0); // make sure it is not reporting any warning that will ruin the output
if (preg_match('/xxx.xxx.xxx.xxx/',$_SERVER['REMOTE_ADDR'])) {

print<<<MYCSS
a {text-decoration:none;color:#777777;}
.
.
.
MYCSS;

}

?>



For myjs.php:

<?php

error_reporting(0); // make sure it is not reporting any warning that will ruin the output
if (preg_match('/xxx.xxx.xxx.xxx/',$_SERVER['REMOTE_ADDR'])) {

print<<<MYJS
var ie=0;
.
.
.
MYJS;

}


?>


You can also use $_SERVER['REMOTE_HOST'] if your shared server keeps on changing the IP address. You need to use your Domain Name instead of the xxx.xxx.xxx.xxx (IP Address) above.

No comments:

Post a Comment