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 %>Please enter your details:>/p>
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.
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