Saturday, December 20, 2008

My First site in PHP.

I tried my hands on creating some new featured site using PHP. This came into the shape of Ignou MCA Helper site which is having sections like: MCA Assignments, MCA Lab manuals and MCA Question papers,

My idea behind this site was that through a common platform, we can share our knowledge in the form of assignments and lab manuals. I also added a section for question papers to share old year's question paper, as these are very useful, while preparing for an exam.


I hope those will help you and you will find it interesting.

http://ignou.110mb.com/Index.html

Saturday, September 27, 2008

Enabling secure file download in Asp .Net 2.0

I was trying my hands on Asp .net after a long time, and then came across an interesting problem. I was trying to write a function to make a secure download of web based files, that is I was trying to hide the path of file from user, who is going to download a file from my site. Igoogled a lot, but could not find the exact thing, that I was looking for. Then I collected the pieces of codes from my search, and created that function out of it. This function can be called from any event and will initiate the download without letting users know the path of target file.

The code goes here:

public void download(string fileName, string parentDirectory)
{
string path = Server.MapPath(pare);
FileInfo f1 = new FileInfo(path + "/" + fileName);
        if (f1.Exists)
        {
            Response.Clear();
            Response.AddHeader("Content-Disposition", "attachment; filename=" + f1.Name);
            Response.AddHeader("Content-Length", f1.Length.ToString());
            Response.ContentType = "application/octet-stream";
            Response.WriteFile(f1.FullName);
            Response.End();
        }
        else
            Response.Write(path + "/" + fileName);
}

Note: To use this code, one must use the System.IO namespace.

Wednesday, June 11, 2008

How to install JDK 6 on Linux (Fedora Core)

I came across a problem of installing JDK on linux. This was interesting because I had to install JDK on a remote server machine. Here's the process to do that.

  1. Downloaded file using wget (because I was told that there is only wget installed on the remote machine) from this location: http://www.java.net/download/jdk6/6u2/promoted/b02/binaries/jdk-6u2-ea-bin-b02-linux-i586-12_apr_2007-rpm.bin
  2. Now changed the permission of the downloaded file to 755 like this:
    chmod 755 jdk-6u2-ea-bin-b02-linux-i586-12_apr_2007-rpm.bin
  3. Then ran it by calling ./jdk-6u2-ea-bin-b02-linux-i586-12_apr_2007-rpm.bin Here I needed to type yes after going through the license document as my acceptance to the terms.
  4. I was made to find the path of the actual location of JDK installation and indicate the fedora to accept my choice by using updatedb;locate javac |grep bin.
  5. Now I required to run the alternative command to instruct Fedora to recognize Sun’s JVM.
    alternatives --install /usr/bin/java java /usr/java/jdk1.6.0_02/bin/java 100
    alternatives --install /usr/bin/jar jar /usr/java/jdk1.6.0_02/bin/jar 100
    alternatives --install /usr/bin/javac javac /usr/java/jdk1.6.0_02/bin/javac 100
  6. Finally I required to configure alternative to use Sun’s JVM as the default JVM by calling /usr/sbin/alternatives --config java
Now the installation process is over. This can be checked by saying java -version.

Here we need to remember that we have to login as root to the machine to do all the installation.

Thursday, May 22, 2008

Some Libraries in JavaScript

Here I have come again with some of the interesting libraries. These are very useful and saves hours of time in coding. Here are some of the fabulous ones:

  1. http://www.prototypejs.org/: this is a JavaScript framework that eases dynamic web development with it's custom commands. It has functionalities like:
    1. Ajax interface
    2. Utility methods
    3. Helper methods
    4. Extending the DOM
    5. Class Object

  2. http://script.aculo.us/: This has complete solution for all your requirements of making your site lively with lot of custom made effects like:
    1. Appear() and Fade()
    2. Puff()
    3. DropOut()
    4. Shake()
    5. Highlight()
    6. SwitchOff()
    7. BlindDown() and BlindUp()
    8. SlideDown() and SlideUp()
    9. Pulsate()
    10. Squish()
    11. Fold()
    12. Grow()
    13. Shrink()

  3. http://extjs.com/: creating custom component for your website using javascript and css is never that easy, as extjs made. Here are some of the tools provided by ExtJs:
    1. Grid
    2. TabPanel
    3. Window
    4. tree
    5. layout manager
    6. combobox
    7. form
    8. toolbar & menus

Here I also would like to share a fantastic site for cross borwser compatible codes in JavaScript. This site have a complete solution of cross browser problems. If you stuck up some where in this topic, then go here: http://www.quirksmode.org/

Wednesday, May 21, 2008

Utility function to get current X and Y position on mouse/cursor (Cross Browser Support

This class is again a cross browser supported and is an interesting utility function:

function demo(obj)
{
var posX=0;
var posY=0;
if(typeof(obj.offsetParent) != "undefined")
{
for(var x=0, y=0;obj; obj=obj.offsetParent)
{
x += obj.offsetLeft;
y += obj.offsetTop;
}
posX=x;
posY=y;
}
else
{
posX=obj.offsetLeft;
posY=obj.offsetTop;
}
alert("X: "+posX+" Y: "+posY);
}

This function can be called from any HTML code like:

onclick="demo(this)"


Saturday, May 17, 2008

Utility Class to change local time in GMT and GMT time in Local

Again I have come up with another interesting JavaScript utility. I was assigned this task to convert local time in GTM time, GMT time in Local Time and then format the date into dd/mm/yyyy hh:mmAM/PM format. After a good two hour googling and haunting, I came to create this utility class:

function localTime()
{
this.GMTTime=function(date)
{
var utc=date.getTime() + date.getTimezoneOffset()*60000;
var gmtDate=new Date(utc);
return gmtDate.toLocaleString();
}
this.LocalTime=function(date)
{
var utc=date.getTime() - date.getTimezoneOffset()*60000;
var gmtDate=new Date(utc);
return gmtDate.toLocaleString();
}
this.toString=function(time)
{
var Month=time.getMonth();
var Date=time.getDate();
var Year=time.getFullYear();
var Hour=time.getHours();
var Minute=time.getMinutes();
var AmPm;

if(Hour < ampm="AM" hour="GMTHour;">
else{ AmPm="PM"; Hour=Hour-12; }

if(Month < month="0">
if(Date < date="0">
if(Hour < hour="0">
if(Minute < minute="0">

var timeString=Month+"/"+Date+"/"+Year+" "+Hour+":"+Minute+""+AmPm;
return timeString;
}

}

This function can be called from any where like this:

var curTime=new Date();
var lTime=new localTime();
var gmtTime=new Date(lTime.GMTTime(curTime));
var localTime=new Date(lTime.LocalTime(gmtTime));
document.write("GMT Time: "+lTime.toString(gmtTime));
document.write("
");

document.write("Loca Time: "+lTime.toString(localTime));

Thursday, May 15, 2008

Interesting Javascript functions

I think JavaScript is fate for me. But during the course I also have come across some interesting javascript functions. These are such interesting that I could not resist my self from publishing them here. Here goes some of them:

1. Function for opening a popup window:

 function pop_window()
{
var confirmWin = null;
confirmWin = window.open('PopUpWindow.aspx','anycontent',
'width=455,height=435,status');
}
call in html of anyother lang"


2. Previewing image before upload:
unction setImage(file)
{
Thumb = document.getElementById('img1');
Thumb.innerHTML = ''
}
put a p tag with some id say img1.
use a file upload control to upload the image with setImage as callback function.

3. Stop image from copying:
function clickIE4(){
if (event.button==2){
alert(message);
return false;
}
}

function clickNS4(e)

{

if (document.layers||document.getElementById&&!document.all)

{

if (e.which==2||e.which==3)

{

alert(message);

return false;

}

}

}

if (document.layers)

{
document.captureEvents(Event.MOUSEDOWN);
document.onmousedown=clickNS4;
}

else if (document.all&&!document.getElementById){

document.onmousedown=clickIE4;

}

document.oncontextmenu=new Function("alert(message);return false")


or you can use a simple process as well
use oncontextmenu="return false" in body tag.

Note: We all know that one cannot stop people from stealing images from a website, as all the images are first downloaded to the client's computer and then displayed. But still one can pretend to be secure using this function.


4. Function to get current x and y position of mouse(cross browser supported)
function demo(obj){
var posX=0;
var posY=0;
if(typeof(obj.offsetParent) != "undefined"){
for(var x=0, y=0;obj; obj=obj.offsetParent){
x += obj.offsetLeft;
y += obj.offsetTop;
}
posX=x;
posY=y;
}
else{
posX=obj.offsetLeft;
posY=obj.offsetTop;
}
alert("X: "+posX+" Y: "+posY);
}

5. Method to add event to a function (cross borwser support)
function test(event)
{
if(!event)
var event = window.event;
}


I found them interesting. You may also.

Wednesday, May 14, 2008

How to make an image draggable (Google Map Type)

These days I am working more on JavaScript and have come across a lot of interesting features of it. In due course I made a demo page using JavaScript to make in image dragable in

element. It gives the feel of Google Map. Here is the code for the same:

Style:

#divDemo
{
overflow: hidden;
position:absolute;
top: 100px;
left: 100px;
border: 2px solid #666666;
width: 400px;
height: 300px;
cursor:pointer;
}
img
{
border: 1px solid #333333;
}

JavaScript function:


//global variables
var object = null;
var offset = null;
//capture events
document.onmousemove = mouseMove;
document.onmouseup = mouseUp;

function mousePoint(x,y)
{
this.x = x;
this.y = y;
}
//function to get current x and y position of mouse
function mousePosition(evnt)
{
var x = evnt.clientX;
var y = evnt.clientY;
return new mousePoint(x,y);
}

//function to get offset of mouse from it's original position, say drag of mouse
function getMouseOffset(target, evnt)
{
evnt = evnt || window.event;
var mousePos = mousePosition(evnt);
var x = mousePos.x - target.offsetLeft;
var y = mousePos.y - target.offsetTop;
return new mousePoint(x,y);
}

function mouseUp(evnt)
{
object = null;
}
//function to change the position of image with movement of mouse
function mouseMove(evnt)
{
if (!object) return;
evnt = evnt || window.event;
var mousePos = mousePosition(evnt);

// if draggable, set new absolute position
if(object)
{
var top = mousePos.y - offset.y;
var left = mousePos.x - offset.x;
var right = (left + object.width) - document.getElementById("divDemo").clientWidth;
var bottom = (top + object.height) - document.getElementById("divDemo").clientHeight;
if(top <= 0 && left <= 0 && right >= 0 && bottom >= 0)
{
object.style.position = 'absolute';
object.style.top = top + "px";
object.style.left = left + "px";
return false;
}
else
{
object.style.position = 'absolute';
object.style.top = object.style.top;
object.style.left = object.style.left;
return false;
}
}
}
//function to make an image/element draggable
function draggable(item)
{
if(item)
{
item = document.getElementById(item);
item.onmousedown = function(evnt)
{
object = this;
offset = getMouseOffset(this, evnt);
return false;
};
}
}

Body section:

in this section you simply need to put div having id say "divDemo". In this div put an image which is larger in size than the parent div. Give it an id say "img1". Now call draggable function on page load with id of image as parameter. Now you are ready to run it into any browser. Right, any browser.

I found it very interesting and hope you will find it interesting too.

Monday, May 12, 2008

Speed optimization in ASP .Net web applications

Following are some of the points, that need to be kept in mind while developing a web based application in ASP .Net:
  • First and most popular way to speed up a web application is to use caching.
  • Disable ViewState property of any control that does not need view state.
  • Put all code of page_load under if(!isPostback) other than codes that specifically need to be loaded on every page load.
  • Use javascript in a separate file and embed them rather than putting it into the same file. Also remove any unused javascripts from file.
  • Do not throw an exception unless doing so is necessary.
  • Disable session state, when not using it.
  • Use StringBuilder for large string operations.
  • In case of using web services use asynchronous call to web services. Also wait for end of the call before the page is fully loaded.
  • Use Threading when downloading huge amount of data.
Apart from the above points, the most important part is the Data Access part of a web application, which reduces the performance of an application. Every time we go for some query of data or other data accesses from database, we reduce the speed of the application. So, following points should be taken care of while establishing a data connection:
  • Use DataReader: If you're just consuming rather than caching data, and operate in a forward-only, read-only way, the data reader represents a better option.
  • Use Caching of Data: However all data cannot be cached or in some cases when data changes every moment caching may not be efficient, still retrieving cached data is faster than retrieving it from database. Caching should be used very carefully because, the longer time you cache a value, the more stale it becomes.
  • Use useful properties of SQLDataSource: Instead of writing a query that includes the "Where", or "Sort By" clauses use the caching, filtering and sorting properties of the SQLDataSourse provider.
  • Use Stored Procedures: Stored Procedures should be used whenever it is possible, as SQL server builds and stores execution plans for procedures.
  • Use Paging: In case of large data handling, one should use paging to make it efficient and fast. There are two types of paging available in ASP .Net: default paging and custom paging. Custom Paging is better among the two, as it requests less data from database than default paging.