473,320 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,320 developers and data experts.

Don't want to send your comments with the code?

hsriat
1,654 Expert 1GB
I am not sure how many of you would have faced this problem, but I faced this many times. I am not good at memorizing things, so I give lot of code comment in my code. And never wanted that code to reach the client. So I used to remove comments from each file by a PHP code and then upload that minified file to the server.

Then an idea came across my mind. And I never removed comments afterwards. May be some of use would already be using this thing, but it's new to me, so thought of posting here.

Here it is...
  • Instead of a regular name folder eg. js or scripts, in which you save your JavaScript files, save them in a random name folder. Something like js_kl32lnfdn.
  • We call our external JavaScript files as [html]<script type="text/javascript" src="/scripts/myscript.js"></script>[/html]
    Instead of this, make a PHP function, something like this:[php]function send_script($script)
    {
    echo "<script type=\"text/javascript\" src=\"/js.php?js=".$script."\"></script>"; //don't use the XML style closing tag (<script .... />) IE would not read it.
    }[/php]Keep this function in a saperate file.
  • In the root folder, save this PHP file (js.php)[php]<?php

    //define the folder name, so that it can be changed anytime.
    define("FOLDER_NAME", "js_kl32lnfdn");

    //path for the original file
    $js_original = $_SERVER['DOCUMENT_ROOT']."/".FOLDER_NAME."/".$_GET['js'];

    //header for the javascript file (will give a warning if not provided)
    header("content-type:text/javascript; charset:UTF-8");

    //alert error if file doesn't exisit
    if (!file_exists($js_original))
    die('alert(\'JavaScript file error!\')');

    //include minificator class
    require('jsmin-1.1.0.php');

    //read and minify the content
    $minified = JSMin::minify(file_get_contents($js_original));

    //header for caching
    header("Expires: ".gmdate("D, d M Y H:i:s", (time()+9000)) . " GMT");

    //year to write on copyright notice
    $year = getdate();
    $year = $year['year'];

    //send your copyright notice
    echo "/* Copyright ".$year." - www.your-website.com */";

    //send the minified version
    echo $minified;

    ?>[/php]
    The minificator PHP class is here: JSMin

What will happen is..
  • The JavaScript file will get minified by the js.php file, which in itself will use a minificator class (not made by me).
  • A file with path js_kl32lnfdn/script1.js will be called as js.php?js=script1.js. So one would never come to know the path to the original (commented) file.
  • For futher security, like if you only want the logged in user to be able to call the javascript or other things, you can add some verification code just in the starting of the js.php.


Hope you will like it. But if there's any drawback, do tell me. :)

Regards,
Harpreet
May 16 '08 #1
2 3547
acoder
16,027 Expert Mod 8TB
Haven't tested, but can't see why it can't work.

More useful than trying to hide actual code which is more trouble than it's worth.
May 17 '08 #2
FLEB
30
It's an interesting idea-- both as an obfuscation device and as an automatic space-saving measure.

Instead of using an obscure directory name for the scripts, you would be better off, IMO, putting an .htaccess file into the cleartext scripts directory with a "deny from all" directive (or a full-fledged password-protected directory if you want clear web access to privileged people). Or, you could store the scripts outside the document-root of the server, making them impossible to directly access via the Web. That way there are no cryptic names to remember (just call it "clear-scripts"), and there is no access for anyone-- even if they guess the directory name.

Also, for extra fun, you might want to look into a mod_rewrite rule that would automatically redirect any calls for your scripts to the pre-processor. It's a slick trick, although kind of a pain to debug.
Aug 7 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Angelos | last post by:
Any Suggestions for an Authentication System ... Do you have any Links to suggest ? My current Authentication works ok but it has a major BUG !!! BUG: If I use the same Authentication mechanism...
4
by: Nataraj M | last post by:
Hi, I just don't want anybody derive a class from my class. For example: /////////////////////// //MY CODE class MyClass { .... }; ///////////////////////
8
by: Heather | last post by:
I want to make a message box appear on the NotInList event for several combo boxes. The message box will alert the user that if they need to add a value they must go through the database...
7
by: Anne M | last post by:
I have a report based on query..which is a team list with names and their role on team ie coach, assistant and player. Report is almost what I want and my knowledge is limited so I need some...
5
by: BabyATX13 via AccessMonster.com | last post by:
I don't want to use "Bankers Rounding" For example I want to be able round 2.525 up to 2.53 Is there a simple way to do this? I have tried to read back issues on this problem and have yet to find...
4
by: #kiss# | last post by:
hey guy !! i m a beginer on C prog...i m using DEV-C IDE but when i m running a .EXE the dos window open , run the .EXE and closed quickly, how could i do if i don't want the dos window...
2
by: M | last post by:
Hello. I need an example of how to call an assembly function/method from a webform page. I don't want to use a codebehind (nor embedded c# code), like so: <asp:DataGrid runat="server"...
4
by: Sibasis Ganguly | last post by:
I want a vb code by which ,when I open the database directly that created form will open , I don't want to click on form button then opening the form. If there is any code for my problem please...
3
by: David Jackson | last post by:
Hey, I'm new to C#, so sorry if this is a dumb question. I have a WinForms app with a form. The form has two combo boxes which which have SelectedIndexChanged methods. The user selects a...
21
by: jehugaleahsa | last post by:
Hello: I had an hour-long discussion with my boss today. Last night, right before I dozed off, I realized some of his code resulted in duplicate processing. I tried to explain it to him and he...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.