|
|
Wednesday, 8 September 2010
Popkin Functions
Category:- ASP :-: Date:- May 31, 2004 :-: Number of times Read:- 6072 -----------------------------------------------------------------------------------------------------------------------------------------------
Having worked in PHP for past 1 month has spoiled my habits. Lazy as I am, working in ASP now seems a bit too much work simply because of writing that long object call to write a line. PHP has a short function call so why doesn’t ASP have it? Now that it doesn’t have it, do we have to use that long call? Am I not making sense? Think I’m mad? Well, let me explain. In PHP, if you have to print anything, you do it like this:
PHP Code:
<?php
print("Hello There!! This is something in a line."); ?> alternatively, you can also do it like this:
PHP Code:
<?php
echo("Hello There!! This is something in a line."); ?> Calling a 4-5 character long function does it all. But ASP makes us labour hard. To write that same line in ASP, we do it like this:
ASP Code:
<%
Response.Write("Hello There!! This is something in a line.") %> Now is that fair? I mean that we just call print() or echo() in PHP to print something but in ASP we have to do Response.Write(). If Microsoft isn’t going to help us making our code shorter(& thus helping our poor fingers), we are gonna help ourselves to make us warm & happy. Here’s what I’ve thought. We can make functions by name of print() & do a Response.Write() in it once. Now everytime we need to print something, we just call that print() function & pass the value on to it. Nice & easy. Now another thing is the redirection. We have to do Response.Redirect() everytime we want to redirect the user to another URL. Can’t we do something about that? Can’t we make it short? I admit that PHP don't have a short one in here as to redirect to another URL, we've to pass headers in PHP but we can make it short. That’s the beauty of having the ability to use custom functions. Like in previous case, here we can make a function by name of lets say, reDir(), which accepts the URL as a parameter & then using Response.Redirect(), it redirects the user. And how about having a function like mail() of PHP, so that we can send eMails by just calling a function. Ok, so here are all the functions bunched together. Their sample usage have been explained later.
ASP Code: <%
' ***** This function is used to print something ***** Private Function print(pVal) Response.Write(pVal) End Function
' ***** This function is used to redirect to a URL ***** Private Function reDir(pURL) Response.Redirect(pURL) End Function
' ***** This function is used to get value from ServerVariables collection ***** Private Function sVar(pVal) sVar = Request.ServerVariables(pVal) End Function
' ***** This function is used to send eMail ***** Private Function mail(pTo, pFr, pCc, pBc, pSu, pMes, pBf, pMf, pIm) pTo = TRIM(LCASE(pTo)) pFr = TRIM(LCASE(pFr)) pCc = TRIM(LCASE(pCc)) pBc = TRIM(LCASE(pBc)) pBf = TRIM(LCASE(pBf)) pMf = TRIM(LCASE(pMf)) pIm = TRIM(LCASE(pIm)) IF (pBf="") OR (pBf="text") THEN pBf = "CdoBodyFormatText” ELSE pBf = "CdoBodyFormatHTML” END IF IF (pMf="") OR (pMf="text") THEN pMf = "CdoMailFormatText" ELSE pMf = "CdoMailFormatMIME" END IF IF (pIm="") OR (pIm="1") THEN pIm = 1 ELSEIF pIm="0" THEN pIm = 0 ELSE pIm = 2 END IF
Set objMail = CreateObject("CDONTS.NewMail") WITH objMail .From = pFr .To = pTo IF NOT(pCc="") THEN .CC = pCc END IF IF NOT(pBc="") THEN .BCC = pBc END IF .Subject = pSu .BodyFormat = pBf .MailFormat = pMf .Importance = pIm .Body = pMes .Send END WITH Set objMail = NOTHING End Function %> So much for the functions now, lets get down to their usage. The first function is print(). Its used to print a message or line, just like you use Response.Write(). The second function is reDir(). Its used to redirect the user, just like Response.Redirect(). Third function is sVar(). Its used to get some info from ServerVariables collection, just like you use Request.ServerVariables(). Fourth & last function is mail(). Now this function is good, it enables you to send an eMail by just calling & passing parameters to it. I’ll explain its parameters. The first parameter is pTo, which wants the eMail address where you want to send the eMail. Second parameter pFr is where you’d put your eMail address, ie., the sender’s eMail address. Third parameter is pCc, to be used if you want to send Carbon Copy of your eMail to someone. Its optional but if you want to use parameters after it, you have to specify it blank, you can’t skip it. Fourth parameter is pBc, to send Blind Carbon Copy of the eMail. Fifth parameter is pSu for setting the Subject of the eMail. Sixth parameter is pMes, the message or body of the eMail. Seventh parameter is pBf, the specifier for the mail body format. If you want to send HTML formatted eMail, then you need to set its value as "html". The default is "text". So you can let it remain blank if you want to send text formatted eMail. The eighth parameter is pMf, which specifies the mail format of the eMail. If you are sending an HTML formatted eMail, then you need to set this as "mime". Otherwise it can be text format which is default option. The nineth & last parameter is pIm, which sets the importance of the eMail. Set the value to "0" for eMail of Low importance, & "2" for High Importance. "1? is for Normal & is default.
One thing that needs to be kept in mind is that the mail() function above uses CDONTS component to send eMails. This is deprecated as its no longer comes in the new versions of IIS, IIS5.1(on Windows XP) & IIS6(Windows 2003). CDOSYS is the component that's used & if you know how to send eMails using CDOSYS, you can easily change the function to send eMails using CDOSYS or any other eMail component. This function will however work well on Windows 2000 or earlier versions running IIS5.0
Now how about some examples? Sure, no problem. Here they are:
ASP Code: <%
'***** print something with the print() function ***** print("Hello There") strVar = "How are you doing?" print(strVar)
'***** redirect using the reDir() function ***** reDir("http://example.com") strURL = "mypage.asp" reDir(strURL)
'***** get user's IP address using sVar() function ***** strIP = sVar("REMOTE_ADDR")
'***** send an eMail with the mail() function ***** strTo = "buddy@example.com" '** Receiver's eMail strFrom = "me@example.com" '** Sender's eMail strSubject = "Hey there" '** Subject of the eMail strCC = "buddy2@example.com" '** send carbon copy strBody = "Hi Buddy How are you?" '** HTML formatted Body of eMail strBFrt = "html" '** Body format of eMail set to HTML strMFrt = "mime" '** Mail format of eMail set to MIME strImp = "2" '** Importance set to High mail(strTo, strFrom, strCC, '', strSubject, strBody, strBFrt, strMFrt, strImp) '*** This sends the eMail. Note the empty parameter after strCC. That's for pBc, the Blind Carbon Copy. '*** We don't want to use pBc, so we've left it empty. Similarly, you can leave out any parameters. '*** But remember that pTo, pFr, pSu & pMes are important & required. %> Quite a start? Well, using these functions, a lot of my time is saved, so I can hope that yours will be saved too.
All said and done, there's another advantage of using these functions besides the ease of use. Though its easy to remember & use these functions & their functionality, these functions also make your code clean & smaller in size, which means that the server executes it fast.
|