Networked Dynamic Information Technology - www.NetDit.com Empowering Articles for Web Developers -------------------------------------------------------------------------------------- - Learning ASP Contents: Disclaimer Introduction Installing Personal Web Server Your First ASP Page Getting Your Hands Dirty With VBScript Logging In Using ASP - Access2000 - Disclaimer Warranty, Disclaimer and Copyright Policy This material is provided on an "as-is" basis, and makes no warranty or representation, express or implied, with respect to its quality performance or fitness for a particular purpose. In no event shall be liable for direct, indirect, special, incidental, or consequential damages arising out of the use of this material. No patent liability is assumed with respect to the use of the information contained herein. Although every precaution has been taken in the preparation of this manual, assumes no responsibility for errors or omissions. Neither is any liability assumed for damages resulting from the use of the information contained herein. This information is provided with the understanding that is not engaged in rendering medical, legal, accounting or other professional service. If legal advice or other expert assistance is required, the services of a competent professional person should be sought. By using this material, the user assumes complete responsibility for any and all damages resulting from that use. Use of this program and materials requires agreement to the terms of this warranty and disclaimer. If you do not agree to the terms of this warranty, do not use this material. - Introduction This text features Learning ASP Series. Web site maintenance is a job that never ends. You constantly need to keep your web site updated so that your visitors get a feeling that you are always evolving and dynamic. Once you have a quality web site, you have to promote it relentlessly. - Installing Personal Web Server Welcome to the first chapter of your Basic ASP Learning Series. By the time you are through with these chapters, you'll have learnt enough to make a small shopping cart in ASP. Most of the chapters will be organized in a linear fashion, so that you always have to work according to your previously acquired knowledge. Sometimes I'll sound like writing the entire chapter in a bulleted form, but that'll be just to keep the unnecessary (I mean that is out of the scope of this reference) stuff out. Installing Personal Web Server Beginning of Chapter One If you want to work with ASP (Active Server Pages) on your personal computer, you need to install the Personal Web Server (PWS). As the name indicates, it's a server. Unlike the normal HTML pages, dynamic pages (either created with CGI, PHP, ASP or Cold Fusion) require some sort of a server to carry on their business. Without a server, you can create a dynamic page, but you cannot view it decently in the run-time mode. Since I mostly work with the Active Server Pages using PWS, I'll tell you how to install the PWS. It ships free with the Win98 CD, and should be in the add-ons folder. From there, install the PWS on your computer. The installation program creates a folder, inetpub in the directly you specify during the installation. This inetpub, further has a folder, wwwroot. So if you install your PWS on your C drive, the wwwroot path should be C:\INETPUB\WWWROOT All the files that you create, you store under wwwroot. What we generally do is, we create separate folders for separate projects under the wwwroot folder. So if I created bytesworth folder in wwwroot folder, the full path should be C:\INETPUB\WWWROOT\BYT And when I have to view the page on my browser, I'll have to type http://localhost/byt as the URL. After you've installed the PWS, it's icon appears on your desktop, and at the bottom right of your screen. Click or double-click on the icon. On the main window, you should see the message: Web publishing is on. Your Home page is available at http://servername The servername is the name you use instead of localhost. But if you are confused about this name game, just use localhost and it should always work if you haven't been naughty and messing around with your computer's basic setup. Before you proceed further, open the Windows Explorer, go to c:\inetpub\wwwroot and create a new folder here by the name of "learnASP" (how boringly predictable! So you can name it something else). We'll be storing ALL our files in this folder. Hence, whenever we want to run a newly created file, we'd type in the location bar: http://localhost/learnASP/newfile.asp and press Enter. Ok, next, click on the Advanced tab. Select Enable Default Document In the Default Document(s) box, type the list of file names you would like to use as your default file once the name of your site is type in the browser. If you have no idea what's the default file, it is the file that answers your call when you type a specific URL without a specific file. For instance, if I type http://www.byt.com the file that actually gets loaded by default is DEFAULT.ASP. This depends on the setting. Some servers use INDEX.ASP or INDEX.HTML or INDEX.SHTML. It all depends on the file parsing set up on your web- hosting server. Then click on the Edit Properties button, after selecting the Home folder. There are three select boxes, viz., "Read", "Execute", "Script". Select all of them. The selections tell the server that we want to execute server side scripts in our ASP pages. ASP pages won't run if the server can't read and execute them. Close the window, and your PWS is running. Of course do not run two servers simultaneously. So if you installed the Apache Web Server and left it running, and then loaded PWS, there is going to be some problem for you to sort out. - Your First ASP Page ASP pages (Active Server Pages page sounds a bit melodramatic but its easier to refer to them like this) are nothing but web pages. The difference is that they have an extension .ASP and you write the code in a Server Side Scripting language. Do we know what's Server Side Scripting? This doesn't actually matter if we delve into such nitty-gritty, but let's sound a bit philosophically intellectual. Now, there are two sorts of scripting languages: Server Side (the hosting server where all the pages and programs reside) Client Side (the web surfer using the browser - actually the browser is the client) No, we are not talking about some corporate politics with all this "siding" talk. Ok, before I begin with my bad sense of humor, "Client Side Scripting" is what we generally see when we code basic Java Scripts in our web pages to validate HTML forms or implementing those cool image rollover effects or opening custom popup windows etc. When you view the source of an HTML page, and if the Java Script has been written in the page itself (they have external Java Scripts too, but then that's a different story), then you can see the script with all it's gory details. A simple example of a Client Side Script written in Java Script is: And then you can use it when the page loads: A Server Side Script, on the other hand, does not manifest itself when you try to use the "View Source" option of your browser. It is a server side matter so unless you have the actual access to the server, you cannot view the portion containing the server side coding. But yes, the rest of the HTML matter is visible. A typical ASP page looks like: <%@Language=VBScript%> <% Option Explicit %> <% Dim SayThis SayThis = "Finally, I'm learning ASP!" Response.Write SayThis %> Provided you've configured your PWS in an amicable manner, this docile file should show up without fuss. You can copy/paste this page as it is using your preferred editor, and save the file as "basic.asp". As you know, a basic HTML web page begins with the tag, when you begin to work with an ASP page, try to make the first line as: <%@Language=VBScript%> <% Option Explicit %> The first line tells the concerned authorities that the following code is going to contain VBScript syntax. The second line forces us to declare every variable before we use it. We'll come back to this later when we are in a more aware state of mind. My preferred script for ASP is VBScript, so most of my ponderings will be in this language. A few daredevils write their ASP pages in Perl too, but personally I feel it is an overkill and they are just trying to either show off or the features they want to use only exist in Perl. For that matter, even C++ is used for making ASP pages. Anyway, done with the first line, are we? As you can observe, we have all the quintessential HTML tags in an ASP page too, but they are only necessary if you intend to display the page to your visitors. If the page just includes a script that performs some programming function and then loads some other page, you can simply have the pure ASP code with first line as <% and the last line is %>. - Getting Your Hands Dirty With VBScript Beginning of Chapter Three I know some people don't like VBScript for being Microsoft-Centric, but it's very easy to get familiar with, and once you know how to create your ASP pages in VBScript, you can move on to any programming language of your choice. First thing first: HTML tags and ASP scripts don't socialize well, and although it is not a politically correct agenda, you have to segregate them. Whenever you are writing the VBScript code for ASP, enclose the code within <% %>. So if you want to initialize a VBScript variable, do it like this: <% Dim name name = "Tutan Khamen" %> For instance, if you want to use the variable "name" in your HTML (ASP, actually) page, all you have to do is: <%@Language=VBScript%> <% Option Explicit %> Testing a VBSCript</titlegt; </head> <body> <% Dim name name = "Tutan Khamen" %> <%=name%>! An ancient Egyptian mummy! </body> </html> Another example: <%@Language=VBScript%> <% Option Explicit %> <html> <head> <title>Testing a VBSCript <% Dim number1, number2 number1 = 33 number2 = 44 %> The product of <%=number1%> and <%=number2%> is <%=number1 * number2%> So now its amply clear that whenever you want to insert Server Side code, or a part of it, you enclose it within <% %>. You can try making short programs and test run the page. - Logging In Using ASP - Access2000 After receiving a few queries about how to store passwords using ACCESS and ASP, and then use them as "logins", I thought, well, why not write in a separate article, instead of attaching multiple ASP files that are full of confusing comments and variables only to be decipherable by my brain? I'm assuming you've installed, and are running PWS (Personal Web Server) on your machine, if you are not already working on a server that supports ASP. First of all, create a database, for instance, customers. Define a table with all the fields you require (include email and password). After the database has been created, you need to create a DNS in order to access this database through your ASP pages. If you have never created it, this is how you do it: Go to the Control Panel (My Computer - Control Panel), and click on the icon that should be saying "ODBC Data Sources (32bit)". In the resulting window, select the "System DSN" tab. Then click on the "Add..." button. From the given list of Database drivers, select "Microsoft Access Driver (*.mdb)" and click the "Finish" button. You reach a place where you have to enter the "Data Source Name". Enter it, anything, for instance, "customers". Then click the "Select..." button. This lets you select the Access database you created. Press Ok, press Ok, and press Ok. Your DSN is created. In the first part, I'll write about storing the passwords. Before this, let's make an include file to create and initialize the session variables that we are going to need (we can use cookies, but some clever folks disable cookies on their browsers). File name: sessions.inc <% if session("email")="" then session("email")="notlogged" session("pass")="" end if %> This file you can include in every page as so that you can use them whenever you need them. Now accepting login and password. For this you require a normal HTML form. You can have "n" number of fields in a form, but here, our primary concern is, getting the email as login, and the accompanying password. Here's the form:

Please enter your details:>/p>

Enter Email: >input type="edit" name="email" size="15">
Enter Password: >input type="password" name="pass" size="15">
We validate the form before it proceeds to the "action" file so that there is very little server-side processing. A simple validation: Note: Put the following Javascript above the tag. So now when the user clicks on "Submit", he/she goes to "storelog.asp" In between, you can have a file to confirm the form fields and give the user an option to modify them before finally saving. A few things. In order to use a database through ASP, you need to have a DNS created for that database on the server. STORELOG.ASP should somewhat look like this: <% dim sEmail, sPass, noError noError="y" sEmail=request.form("email") sPass=request.form("pass") ' The following lines setup a connection to the DNS we created above Dim toDatabase 'To connect to the DNS Dim toRecordset 'To connect to the individual tables Set toDatabase = Server.CreateObject("ADODB.Connection") toDatabase.Open "customers" Set toRecordset = Server.CreateObject("ADODB.Recordset") toRecordset.Open "logins", toDatabase, 2 ' 2 = Opens the recordset in "Write Mode" ' Let us say "logins" is some table you created in the database. toRecordset.AddNew toRecordset("email")=sEmail toRecordset("password")=sPass on error resume next toRecordset.Update if err.number<>0 then ' do something if some error occurs. ' one error could be that the email already exists in the database. noError="n" end if toRecordset.Close Set toRecordset = Nothing toDatabase.Close Set toDatabase = Nothing if noError="y" then ' If the info was saved smoothly. session("email")=sEmail session("pass")=sPass end if ' Here you can display some message that the record has been saved. %> This saves the login information of a new customer. Now, how do we use it in the future? First, the login form, that could be on any page. Remember you can use somewhat same validation Javascript here too, so I'm not repeating it, but just mentioning it.

Please login by entering your email and password.

Enter Email:
Enter Password:
LOGIN.ASP At the top of the page, along with other ASP commands, include this too: <% response.buffer=true %> This is required if you want to send the user to some page after he/she has successfully logged in. <% dim sEmail, sPass, noError noError="y" sEmail=request.form("email") sPass=request.form("pass") ' The following lines setup a connection to the DNS we created above Dim toDatabase 'To connect to the DNS Dim toRecordset 'To connect to the individual tables Set toDatabase = Server.CreateObject("ADODB.Connection") toDatabase.Open "customers" fndSQL="select * from logins where email='" & sEmail & "' and password='" & sPass & "'" Set toRecordset=toDatabase.execute(fndSQL) if toRecordset.eof then response.write "Your details are not in the database, please try again, or register yourself." else session("email")=toRecordset("email") session("pass")=toRecordset("password") end if toRecordset.Close Set toRecordset = Nothing toDatabase.Close Set toDatabase = Nothing response.redirect "To some URL" %> From now onwards, whenever you want to perform some action that should only be performed if the user is logged in, just check the value is session("email"), like: <% if session("email")<>"notlogged" then ' do things for the logged in customer else ' tell the customer that he she is not logged in. end if %> -------------------------------------------------------------------------------------- Networked Dynamic Information Technology - www.NetDit.com Empowering Articles for Web Developers