Monday, July 20, 2009

Read client side IP Address

-------server side code for ASP Classic--------

ip_address=Request.Servervariables("REMOTE_HOST")
Response.write(ip_address)

-------End server side code -----------------

Thursday, April 2, 2009

Type conversion in ASP

From string to Integer conversion
Dim finalvalue="10"
IntValue=CInt(finalvalue)


From integer to String conversion
Dim finalvalue=10
i=CStr(finalvalue)

Session Object properties in ASP

The Session Object in ASP is a great tool for the modern web site. It allows you to keep information specific to each of your site's visitors. Information like username,password, etc.

Create Session
Session("username") = "Amit"

Accessing SessionID
Dim mySessionID
mySessionID = Session.SessionID

Set Session Timeout time
Session.Timeout = 240

Session Abandon(Clear)
Session.Abandon

Array declaration in ASP

You can create an array of specific size or you can create a dynamic sized array. Below we have examples of both types of arrays.

Dim myFixedArray(3) 'Fixed size array
Dim myDynArray() 'Dynamic size array

Example for fixed size Array
Dim myFixedArray(3)
myFixedArray(0) = "Item number1"
myFixedArray(1) = "Item number2"
myFixedArray(2) = "Item number3"
myFixedArray(3) = "Item number4"

Example for Dyanamic size Array
Dim myDynArray()
myDynArray(0) = "Item number1"
myDynArray(1) = "Item number2"
myDynArray(2) = "Item number3"
myDynArray(3) = "Item number4"

Current date format in ASP

The Format Date Time function takes two arguments: a date and (optional) an integer from 0 through 4. The meanings of these numbers are as follows:

0 - This is the default setting. A short date DD/MM/YYYY will be used.
1 - A long date defined by the computer's regional settings.
2 - A short date defined by the regional settings.
3 - (time)A time using the time format defined by the regional settings.
4 - (time)A time using military time HH:MM (H = hour, M = Minute)

OUTPUT :
0 = 4/2/2009
1 = Friday, October 21, 2005
2 = 4/2/2009
3 = 12:00:00 AM
4 = 00:00

Type 1.
CurrentDate=FormatDateTime(Date, 0)
Response.Write(CurrentDate)

OutPut : 4/2/2009


Type 2.
CurrentDate=FormatDateTime(Date, 1)
Response.Write(CurrentDate)

OutPut : Thursday, April 02, 2009

Tuesday, March 17, 2009

Useful Functions in Classic ASP

1. Mid function to trim text for specific length
for 1 to 5 letter triming.
strtext=mid("text want to trim",1,5)

2. Replace function with Syntax
xyz=replace(Amit pal, " ", "G")

3. Find Server name by server variables
strServerURL = "http://" + Request.ServerVariables("SERVER_NAME")

4. Split Fuction Syntax
current_date=date
current_day=split(current_date,"/")

5. Trim function for trimimg blank spaces
email=trim(request.form("email"))

6. Count recordset's total number of rows for any Query's resultset

Method 1:

rs.open sqlQuery,ObjConn
rs.cursorlocation=3
Dim countRow
countRow=rs.Recordcount

Method 2:

rs.open sqlQuery,ObjConn,3
Dim countRow
countRow=rs.Recordcount

ASP Source Code for sending email

dim tend12
tend12="Dear User How r u?"
Set objMail = Server.CreateObject("CDO.Message")
objMail.From = "
info@sample1.com"
objMail.To = "
abc@sample1.com"
objMail.BCC = "
amit@sample1.com"
objMail.Subject = "Sample Subject"
objMail.HTMLBody = tend12
objMail.Send
Set objMail = nothing

Source Code for sending email with file attachment in Classic ASP

dim tend
tend="body text will be here"
Set objMail = Server.CreateObject("CDO.Message")
objMail.From = "abc@xyz.in"
objMail.To = "abc@xyz.in"
objMail.CC = "info@xyz.in"
objMail.BCC = "abc@xyz.in,mn0@xyz.in,dfg@xyz.in"
objMail.Subject = "subject will be here"
'objMail.BodyFormat = 0
'objMail.MailFormat = 0
objMail.HTMLBody = tend

' For attachement file
Dim strServerURL
'Server Name including http:// protocol
strServerURL = "http://" + Request.ServerVariables("SERVER_NAME")
Dim strServerURLFull
'Path to subweb directory
strServerURLFull = strServerURL & "/2008"
Dim strTargetURL
'Directory where file is located
strTargetURL = strServerURLFull & "/xyz"

dim strpath
strpath = strTargetURL & "/" & locatedfilename
objMail.AddAttachment(strpath)

if locatedfilename2 <> "" then
dim strpath2
strpath2 = strTargetURL & "/" & locatedfilename2
objMail.AddAttachment(strpath2)
end if

objMail.Send

Source code to protect from SQL Injection with Classic ASP

'Function to sql injection by url inputs

Function Injection (strValue)
IF strValue <> "" Then
If ( Instr(strValue,"union") > 0 OR Instr(strValue,"where") > 0 OR Instr(strValue,"insert") > 0 OR Instr(strValue,"delete") > 0 OR Instr(strValue,"Truncate") > 0 OR Instr(strValue,"update") > 0 OR Instr(strValue,"like") > 0 OR Instr(strValue,"drop") > 0 OR Instr(strValue,"create") > 0 OR Instr(strValue,"modify") > 0 OR Instr(strValue,"rename") > 0 OR Instr(strValue,"alter") > 0 OR Instr(strValue,"cast") > 0 OR Instr(strValue,"href") > 0 OR Instr(strValue,"a href") > 0 OR Instr(strValue,";") > 0) Then
Response.redirect(".././index.asp")
End If
End IF
End Function

'End sql injection Function


'call Function to sql injection by url inputs

Injection Request.ServerVariables("QUERY_STRING")

Best Way to protect our site from SQL injection call SQL Injection function in Global file/Global.asa/connection file.

SQL Server Database connection source code in classic ASP

Connection Code

Dim strConnection,objConn
Set objConn = Server.CreateObject("ADODB.Connection")


strConnection ="Provider=SQLOLEDB;Data Source=671.151.47.212; Database=DBNews; uid=sample;pwd=sample@123;"
objConn.open strConnection

End Connection Code