![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]()
![]() |

|
| Programming tutorials All Knowledge Info and links to posted here |
![]() |
|
What Are Active Server Pages?
|
LinkBack | Thread Tools | Display Modes |
|
|
#1 (permalink) |
|
Administrator
Posts: 18,715
Join Date: Jan 2006
Rep Power: 10
IM:
|
Active Server Pages (ASPs) are Web pages that contain server-side scripts in addition to the usual mixture of text and HTML (Hypertext Markup Language) tags. Server-side scripts are special commands you put in Web pages that are processed before the pages are sent from your Personal Web Server to the Web browser of someone who's visiting your Web site. . When you type a URL in the Address box or click a link on a Web page, you're asking a Web server on a computer somewhere to send a file to the Web browser (sometimes called a "client") on your computer. If that file is a normal HTML file, it looks exactly the same when your Web browser receives it as it did before the Web server sent it. After receiving the file, your Web browser displays its contents as a combination of text, images, and sounds. In the case of an Active Server Page, the process is similar, except there's an extra processing step that takes place just before the Web server sends the file. Before the Web server sends the Active Server Page to the Web browser, it runs all server-side scripts contained in the page. Some of these scripts display the current date, time, and other information. Others process information the user has just typed into a form, such as a page in the Web site's guestbook. To distinguish them from normal HTML pages, Active Server Pages are given the ".asp" extension. What Can You Do with Active Server Pages? There are many things you can do with Active Server Pages.
The appearance of an Active Server Page depends on who or what is viewing it. To the Web browser that receives it, an Active Server Page looks just like a normal HTML page. If a visitor to your Web site views the source code of an Active Server Page, that's what they see: a normal HTML page. However, the file located in the server looks very different. In addition to text and HTML tags, you also see server-side scripts. This is what the Active Server Page looks like to the Web server before it is processed and sent in response to a request. What Do Server-Side Scripts Look Like? Server-side scripts look a lot like HTML tags. However, instead of starting and ending with lesser-than ( < ) and greater-than ( > ) brackets, they typically start with <% and end with %>. The <% is called an opening tag, and the %> is called a closing tag. In between these tags are the server-side scripts. You can insert server-side scripts anywhere in your Web page--even inside HTML tags. Do You Have to Be a Programmer to Understand Server-Side Scripting? There's a lot you can do with server-side scripts without learning how to program. For this reason, much of the online Help for Active Server Pages is written for people who are familiar with HTML but aren't computer programmers. |
|
|
|
|
|
|
|
|
#2 (permalink) |
|
Administrator
Posts: 18,715
Join Date: Jan 2006
Rep Power: 10
IM:
|
The date and time described in this section are those that are on the server. Date To display the current date by itself in a Web page, type: <% =date %></pre> at the point where you want it to appear. When you view the page in your browser, you should see something like this: Thu, Jan 23, 1997 Note: Even though "=date" is a short script, it's actually made up of two parts. The "date" part tells the server, "Get me the date." The equal sign (=) tells the server to display the date in the Web page. If you typed just: <% date %></pre> the server would get the current date from your system, but that's all. It wouldn't display it. There are times when it makes sense to use an ASP function without the equal sign. Time To display the current time by itself, type: <% =time %></pre> where you want it to appear. When you view the page, you should see something like this: 4:19:46 PM Now (Date and Time) To display the current date and time, type: <% =now %></pre> where you want them to appear. When you view the page, you should see something like this: 1/23/97 4:19:46 PM Changing the Way Date and Time are Displayed You can also use Active Server Pages (ASP) functions to customize the way the current date and time are displayed on your Web page. To do this, use the now function together with the following formatting functions. Month and Monthname To display the number of the current month in a Web page, type: <% =month(now) %></pre> where you want it to appear. When you view the page in your browser, you'll see a 1 if the current month is January, 2 if it's February, and so on. To display the name of the current month, type: <% =monthname(month(now)) %></pre> where you want it to appear. Day To display the day of the current month, type: <% =day(now) %></pre> where you want it to appear. When you view the page, you'll see a number between 1 and 31. Year To display the current year, type: Using Variables, and Forms in Active Server Pages Forms are a convenient way to communicate with visitors to your Web site. Using forms, you can create a survey form and ask visitors to fill it out. When they fill out the form, you can process the results automatically. With forms, there are two steps: first you create the form, and then you process it. To create a form for an Active Server Page, just create a standard HTML form. To try out this example, create an HTML file ("form_response.html") and cut-and-paste the following text into it. <center><table border="1" cols="1" width="500"> <tbody><tr> <td bgcolor="#bfffbf">form_response.html try !</td> </tr> <tr> <td bgcolor="#bfbfff"><html> <head><title>Asking for information</title></head> <body> <form method="post" action="form_response.asp"> Your name: <input type="text" name="name" size="20"><BR> Your email: <input type="password" name="email" size="15"><BR> <input type="Submit" value="Submit"> </form> </body> </html></td> </tr> </tbody></table></center> Active Server Pages provide a mechanism for processing forms that, unlike CGI scripting, doesn't involve serious programming: the Request.Form. Considering the form above, we may create the file bellow and get a response. <center><table border="1" cols="1" width="500"> <tbody><tr> <td bgcolor="#bfffbf">form_response.asp try !</td> </tr> <tr> <td bgcolor="#bfbfff"><html> <head><title>Responding to a form</title></head> <body> Your name is <% =Request.Form("name") %> <BR> Your email is <% =Request.Form("email") %> </body> </html></td> </tr> </tbody></table></center> To display the contents of each field in the form, type: <% =Request.Form(fieldname) %></pre> where fieldname is the name of the field. Creating a Variable You'll probably want to do more with your forms than display their contents in a Web page. For example, based on the contents of the form, you may want to create a variable and insert that variable in different places of your response page. You may need to create a variable. To do that, just make up a name and set it equal to the contents of the field. For example, if you have a field called "CatName" in your form, you can save it into a variable called "TheName" by typing: <% TheName = Request.Form("CatName") %></pre> If you want to display "VisitorName" several times within a text you only need to include the variable in the text. For example: My cat´s name is <% =TheName %>. Do you want to see <% =TheName %>?. Example The form in this example asks users to introduce their names and their favorite color: red, blue, or green. When the form is received, the server responds displaying these data. <center><table border="1" cols="1" width="500"> <tbody><tr> <td bgcolor="#bfffbf">nameandcolor.html try !</td> </tr> <tr> <td bgcolor="#bfbfff"><html> <head><title>Name and Color</title></head> <body> <FORM ACTION="nameandcolor.asp" METHOD=POST> Let me know your Name and Favorite Color: <P>YOUR NAME: <INPUT TYPE="TEXT" NAME="YOURNAME" SIZE=20> <P>COLOR: <INPUT TYPE="RADIO" NAME="COLOR" VALUE="1" CHECKED>Red <INPUT TYPE="RADIO" NAME="COLOR" VALUE="2">Green <INPUT TYPE="RADIO" NAME="COLOR" VALUE="3">Blue <P> <INPUT TYPE="SUBMIT" VALUE="OK"> </FORM> </body> </html></td> </tr> </tbody></table></center> Now, create an ASP file ("nameandcolor.asp") and cut-and-paste the following text into it. <table border="1" cols="1" width="500"> <tbody><tr> <td bgcolor="#bfffbf">nameandcolor.asp try !</td> </tr> <tr> <td bgcolor="#bfbfff"><html> <head><title>Name and Color</title></head> <body> <% TheName = Request.Form("YOURNAME") %> <% colornumber = Request.Form("COLOR") %> Hi, <% =Thename %>.<BR> I know your favorite color is <% if colornumber = "1" then %> red <% end if %> <% if colornumber = "2" then %> green <% end if %> <% if colornumber = "3" then %> blue <% end if %>. </body> </html></td> </tr> </tbody></table> |
|
|
|
|
|
#3 (permalink) |
|
Administrator
Posts: 18,715
Join Date: Jan 2006
Rep Power: 10
IM:
|
<center><table border="1" cols="1" width="350"> <tbody><tr> <td><% AA="water" If AA="water" Then response.write ("I want to drink water") Else response.write ("I want to drink milk") End If %> </td> </tr> </tbody></table></center> We may use it this way: <center><table border="1" cols="1" width="350"> <tbody><tr> <td><% AA="water" If AA="water" Then %> I want to drink water <% Else %> I want to drink milk <% End If %> </td> </tr> </tbody></table></center> In both cases we have checked a condition (AA="water"), and we have get a positive instruction (to write the sentence "I want to drink water"). We are allowed to execute any kind of instructions (including If....then....Else) and as many instructions as we want . For....Next This instructions is also similar in different programming languages. Let's see a typical example. <center><table border="1" cols="1" width="350"> <tbody><tr><td>example.asp Try !</td></tr> <tr> <td>I want to say "Hello" 10 times<BR> <% For mynumber = 1 to 10 %> <% =mynumber %> Hello<BR> <% Next %> END</td> </tr> </tbody></table></center> In this case we have defined a variable ("mynumber") and using the For...Next instruction we have repeated 10 times line 4. Similarly to If....Then....Else instruction, we are allowed to execute any kind of instructions and as many of them as we want . The For...Next instruction allows to define the value of the increment. <center><table border="1" cols="1" width="350"> <tbody><tr> <td><% For mynumber = 1 to 20 STEP 2 response.write("Hello<BR>") Next %></td> </tr> </tbody></table></center> <center><table border="1" cols="1" width="350"> <tbody><tr> <td><% For mynumber = 20 to 1 STEP -2 response.write("Hello<BR>") Next %></td> </tr> </tbody></table></center> In both cases we will get the same response ("Hello" 10 times). The increment may be positive or negative as shown in the example. Do While...Loop Again, we will define a condition and one or more instructions: <center><table border="1" cols="1" width="350"> <tbody><tr> <td><% mynumber=0 Do While mynumber<10 response.write("Hello<HR>") mynumber=mynumber+1 Loop %> </td> </tr> </tbody></table></center> In this example the condition is "mynumber<10" and the instructions defines a response text and an increment of the variable "mynumber". In the example, mynumber will be increased until it gets a value of 10. Then the loop will be abandon. Several instruction may be used within the loop. Do Until....Loop Quite similar to the previous one, it also includes a condition and one or more instructions: <center><table border="1" cols="1" width="350"><tbody><tr> <td><% mynumber=0 Do Until mynumber=10 response.write("Hello<HR>") mynumber=mynumber+1 Loop %> </td> </tr></tbody></table></center> In this example the condition is "mynumber=10", so mynumber will increased until it is equal to 10, and then the loop will be abandon. Let's see an example using this Do Until...Loop: <center><table border="1" cols="1" width="350"> <tbody><tr> <td><% myfirstnumber=0 mysecondnumber=0 Do Until myfirstnumber=15 Do Until mysecondnumber=15 response.write("X") mysecondnumber=mysecondnumber+1 Loop Response.write ("<BR>") myfirstnumber=myfirstnumber+1 mysecondnumber=myfirstnumber Loop Response.write ("END") %> </td></tr></tbody></table></center> The result of the script is this one: <center><table border="1" cols="1" width="350"> <tbody><tr> <td>XXXXXXXXXXXXXXX XXXXXXXXXXXXXX XXXXXXXXXXXXX XXXXXXXXXXXX XXXXXXXXXXX XXXXXXXXXX XXXXXXXXX XXXXXXXX XXXXXXX XXXXXX XXXXX XXXX XXX XX X END</td> </tr> </tbody></table></center> Select Case....End Select This is a very useful instruction in case we want to check different values for variable. Lets check an example: <center><table border="1" cols="2" width="350"> <tbody><tr> <td width="10"> 1 </td> <td><% 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 mynumber=3 Select Case mynumber Case 1 Response.write ("Number 1") Case 2 Response.write ("Number 2") Case 3 Response.write ("Number 3") Case 4 Response.write ("Number 4") Case 5 Response.write ("Number 5") Case Else Response write ("Mynumber is higher than 5") End Select %></td> </tr> </tbody></table></center> In this example above, we have defined mynumber as 3, so they are executed the instructions following line 8 (in this case only one instruction is executed, but they may be several instructions). Case Else is not necessary. Let's try a second example: <center><table border="1" cols="2" width="350"> <tbody><tr> <td width="10"> 1 </td> <td><% 2 3 4 5 6 7 8 9 10 11 12 13 username=request.form("username") Select Case username Case "Peter" Response.write ("Hello, Peter") Case "John" Response.write ("Hello, John") Case "Joe" Response.write ("Hi, Joe") Case Else Response write ("I do not know you") End Select %></td> </tr> </tbody></table></center> Let's see a different example: <center><table border="1" width="550"> <tbody><tr> <td bgcolor="#bfffbf">backgroundform.html</td> </tr> <tr> <td bgcolor="#bfbfff"><html> <head><title>Chose background color</title></head> <form action="backgroundresponse.asp" method="post"> Which color do you prefer to use as your background? <BR> <input type="radio" name="kindofcolor" value="defined" checked> Defined color <select name="definedcolor"> <option value="#FFFFFF">White</option> <option value="#FF0000">Red</option> <option value="#00FF00">Green</option> <option value="#0000FF">Blue</option> </select> <BR> <input type="radio" name="kindofcolor" value="custom"> Custom color <input type="text" size="8" name="mycolor"></input> <BR><input type="Submit" value="Submit"></input> </form> </body> </html></td> </tr> </tbody></table></center> <center><table border="1" width="550"> <tbody><tr> <td bgcolor="#bfffbf">backgroundresponse.asp</td> </tr> <tr> <td bgcolor="#bfbfff"><% kindofcolor=Request.form("kindofcolor") Select Case kindofcolor case "defined" colorofbackground=Request.form("definedcolor") Select Case colorofbackground case "#FFFFFF" texttoshow="White" case "#FF0000" texttoshow="Red" case "#00FF00" texttoshow="Green" case "#0000FF" texttoshow="Blue" End select case "custom" colorofbackground=Request.form("mycolor") texttoshow="Custon color" End select %> <html> <head><title>Chose background color</title></head> <body bgcolor="<% =colorofbackground %>"> <center> <H1><% =texttoshow %></H1> </center> </form> </body> </html> </td> </tr> </tbody></table></center> You may try this example: Subroutines Subroutines have the same utility in ASP as it has in other languages. In the next two examples, we have asked our visitors his name, and depending on that answer a different response is sent to the client. The response will be the same in both cases, but in the second one subroutines are used. The use of subroutines may be very useful when there are a lot of instructions to be perform within a subroutine. This way it will allow us to simplify the structure of our script. <center> Example 1 </center> <center><table border="1" cols="1" width="350"> <tbody><tr> <td><% TheName=request.form("name) if TheName="John" then response.write ("Hi, John. How are you?") response.write ("<br>Did you know I got married last month?") else response.write ("Hi. How are you?") end if %> </td> </tr> </tbody></table></center> <center>Example 2</center> <center><table border="1" cols="1" width="350"> <tbody><tr> <td><% TheName=request.form("name) if TheName="John" then ResponseToJohn() else ResponseToUnknown() end if Sub ResponseToJohn() response.write ("Hi, John. How are you?") response.write ("<br>Did you know I got married last month?") End Sub Sub ResponseToUnknown() response.write ("Hi. How are you?") End Sub %> </td> </tr> </tbody></table></center> In order to call a subroutine, we will use this kind of code: Whatever() Where Whatever is the name of the subroutine (it is recommended to use a very descriptive name of the task we want to perform within the subroutine to make it easier to understand the script). We may also provide information to the subroutine in order to perform the specified task. The data will be provided this way: Whatever(data1, data2 ... dataN) In the following example we will provide different data to a unique subroutine depending on the Name of the person provided throw a form: <center> Example 3 </center> <center><table border="1" width="350"> <tbody><tr> <td><% TheName=request.form("name) if TheName="John" then ResponseToVisitor(35,Sue,New York) else if TheName="Peter" then ResponseToVisitor(33,Sally,Los Angeles) else response.write("Who are you?") end if end if Sub ResponseToVisitor(AA,BB,CC) response.write ("I know your are" & AA & "years old, ") response.write ("you are married to" & BB & ", and") response.write ("you are living in " & CC) End Sub %> </td> <td>1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19</td> </tr> </tbody></table></center> In line 14 it is specified AA is the first variable to be get, BB the second one, and CC the third one. The values for the three variables are provided in the same order in line 5 or line 8. The example above also shows subroutines are very useful to avoid repeating a specific number of tasks several times within the script, so that the script looks more organized and it is smaller. Include/virtual Server Site includes or SSI is a very simple programing language (see a small tutorial) but it also has a very limited number of instructions. We will consider only one option SSI allows us to use within our asp scripts: include/virtual. In the next example we will use the include option of SSI in a our asp script (response.asp). This command allows as to add a set of instructions from different files (file1.txt and file2.txt bellow) and execute them. <center> Example 4 </center> <center><table border="1" cols="1" width="350"> <tbody><tr> <td>response.asp</td> </tr> <tr> <td><% TheName=request.form("name) if TheName="John" then %> <!--#include virtual="/file1.html" --> <% else %> <!--#include virtual="/file2.asp" --> <% end if %> </td> </tr> </tbody></table></center> <center><table border="1" cols="1" width="350"> <tbody><tr> <td>File1.html</td> </tr> <tr> <td>Hi, John.<br> I know your are 31 years old, you are married to Sue, and you are living in New York.</td> </tr> </tbody></table></center> <center><table border="1" cols="1" width="350"> <tbody><tr> <td>File2.asp</td> </tr> <tr> <td><% for i=1 to 3 response.write(Thename & "...<BR>") next response.write("Who are you?") %></td> </tr> </tbody></table></center> In this case, if the name of the person who is visiting our page is John, then we will respond with file1.html. If not, then we will execute some asp instructions from file2.asp. The include file must be a text file (.txt, .html, .htm, .shtml, .asp...). Although we have used file1.html and file2.asp, the script will work exactly in the same way with file1.txt and file2.txt (changing the name of the files would have no effect). By using SSI and asp we may also get a secret page: <center><table border="1" cols="1" width="350"> <tbody><tr> <td>secret_page.asp Try !</td> </tr> <tr> <td><% UserName=request.form ("username") Password=request.form("password") if UserName="myusername" and Password="mypassword" then %> <!--#include virtual="/cgi-bin/secret_info.txt" --> <% else %> <Form Action=secretpage.asp method=post> Username: <input type=text name=username size=15><BR> Password: <input type=text name=password size=15><BR> <input type=Submit Value=Send> </form> <% end if %></td> </tr> </tbody></table></center> <center><table border="1" cols="1" width="350"> <tbody><tr> <td>secret_info.txt</td> </tr> <tr> <td>This is my secret information:<BR> My name is John.<BR> My surname is Smith.<BR> <BR>End of secret information.</td> </tr> </tbody></table></center> In this case it is convenient to save secret_info.txt file in the cgi-bin directory (the .txt file is not accessible by visitors from this directory, but it will be accessible from our top directory). |
|
|
|
|
|
#4 (permalink) |
|
Administrator
Posts: 18,715
Join Date: Jan 2006
Rep Power: 10
IM:
|
The first time a user accesses to a our pages some connections and disconnections took place. During this process the server and the client will interchange information to identify each other. Due to this exchange of information our server will be able to identify a specific user and this information may be use to assign specific information to each specific client. This relationship between computers is call a session. During the time a session is active, it is possible to assign information to a specific client by using Session method. We will use an example to explain this method: Let's suppose we want to allow specific user to access the information on our site or directory and we want to show a username in all pages visited by the user. In this case we may use the Session method. In this example, we will ask the username of the person in our index.asp page <center><table border="1" width="550"> <tbody><tr> <td bgcolor="#bfffbf">respondtoforms.asp</td> <td> </td> </tr> <tr> <td bgcolor="#bfbfff"><% IF Request.form="" THEN %> <html> <title>Our private pages</title> <body> In order to access this pages fill the form below:<BR> <form method="post" action="index.asp"> Username: <input type="text" name="username" size="20"><BR> Password: <input type="password" name="password" size="15"><BR> <input type="Submit" value="Submit"> </form> </body> </html> <% ELSE %> <% IF Request.form("username")="Joe" AND Request.form("password")="please" THEN %> <% Session("permission")="YES" Session("username")="Joe" %> <html> <title>Our private pages</title> <body> Hi <% =Session("username") %>, you are allow to see these pages: <BR> <A HREF="page1.asp">Page 1</A><BR> <A HREF="page2.asp">Page 2</A> </body> </html> <% ELSE %> Error in username or password <% END IF %> <% END IF %> </td> <td> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 </td> </tr> </tbody></table></center> Let's explain how this page works: In line 1 it is checked whether information is submitted throw a form. If the answer is negative (Request.form=""), a form is displayed asking for username and password. After filling the form and submitting it, as Request.form is not "" and the script will jump to line 15. In line 17 they are checked the username and password. If user name is "Joe" and Password is "please", then two variables are set for the client (lines 21-22): Session("permission")="YES" Session("username")="Joe" These variables will be kept in the server during the time the session is active (normally it will expire after 20 minutes without contact). Finally, if username and password are correct, a response page with links is send to the client with the name of the user in the top. In this example, if the username or password are incorrect the response page will include the text in line 38. Now, let's suppose the user clicks in the link "Page 1" (page1.asp). The code of page1.asp will be the following one: <center><table border="1" width="550"> <tbody><tr> <td bgcolor="#bfffbf">page1.asp</td> <td> </td> </tr> <tr> <td bgcolor="#bfbfff"><% IF Session("permission")="YES" THEN %> <html> <title>Page 1</title> <body> Hi <% =Session("username") %>, welcome to Page 1 <BR> This page is empty at the moment, but it will be very interesting in the next future </body> </html> <% ELSE %> You are not allowed to access this page <% end IF %> </td> <td> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 </td> </tr> </tbody></table></center> In line 1 it is check whether the value for Session("permission") is "YES". If the answer is positive a page with information is send to the client. If the answer is negative, the text in line 15 is send. NOTES:
For a better understanding of this method we will create a counter which will be shown in the same page. In order to make it work, copy the code below to your server: <center><table border="1" width="550"> <tbody><tr> <td bgcolor="#bfffbf">counter.asp</td> <td> </td> </tr> <tr> <td bgcolor="#bfbfff"><% Aplication.Lock Application("pagevisits")=Application("pagevisits" )+1 Application.Unlock %> <html> <title>Page under construction</title> <body> Under construction<BR><BR> Page views: <% =Application("pagevisits") %> </body> </html> </td> <td> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 </td> </tr> </tbody></table></center> In the first part of this code, as Application method is shared between different clients, it is necessary to prevent other clients from modifying the information in Application("pagevisits"). Application.Lock will avoid that by stopping the information to be shared, and Application.Unlock will allow the information to be shared again. Line 3 increases the value for the counter. Finally a html code is send to the client, including the value of the counter. NOTES:
The Dictionary object In order to learn how Dictionary object works we will create a small script which will translate number 1 to 10 from English to Spanish. <center><table border="1" width="550"> <tbody><tr> <td bgcolor="#bfffbf">translate.asp</td> <td> </td> </tr> <tr> <td bgcolor="#bfbfff"><% SET MyDictionary=CreateObject("Scripting.Dictionary") MyDictionary.Add "one","uno"EnglishNumber="four" SpanishNumber=MyDictionary.Item (EnglishNumber) Response.Write(SpanishNumber) %></td> <td> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 </td> </tr> </tbody></table></center> How the script works
<center><table border="1" width="550"> <tbody><tr> <td bgcolor="#bfbfff">if MyDictionary.Exists ("ten")=True then Response.Write("this key is included in the dictionary") lse Response.Write("Error: no such a key in the dictionary") end if</td> </tr> </tbody></table></center> Example: Translation of a number from English to Spanish This example uses most of the elements explained above.
</td> </tr> <tr> <td bgcolor="#bfbfff"><html> <title>Page under construction</title> <body> <% if request.form="" then Sendform() else SET MyDictionary=CreateObject("Scripting.Dictionary") MyDictionary.Add "one","uno"EnglishNumber=request.form("EnglishNumber") Response.Write("English number: " & EnglishNumber) if MyDictionary.Exists (EnglishNumber)=True then SpanishNumber=MyDictionary.Item (EnglishNumber) Response.Write("<BR>Spanish number: " & SpanishNumber) else Response.Write("<BR>Spanish number: " & "No translation available") end if end if %> <% Sub Sendform() %> <form action=translation.asp method=post> Write a number in English<BR> <input type=text size=30 name=EnglishNumber><BR> <input type=submit Value="Enter to my Secret Page"> </form> <% End Sub %> </body> </html> </td> <td> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 </td> </tr> </tbody></table></center> Example: Password protected information In this example keys and items are used as usernames and passwords. It is very similar to the one above. <table border="1" width="550"><tbody><tr> <td bgcolor="#bfffbf">secretpage.asp try !</td> <td> </td> </tr> <tr> <td bgcolor="#bfbfff"><% if request.form="" then Sendform() else SET MyDictionary=CreateObject("Scripting.Dictionary") MyDictionary.Add "John","123" MyDictionary.Add "Peter","456" MyDictionary.Add "Anna","789" Username=request.form("Username") Password=request.form("password") if MyDictionary.Exists (Username)=True AND Password=MyDictionary.Item (Username) then SecretInfo() else Response.Write("Error: incorrect userame or password") end if end if %> <% Sub Sendform() %> <form action=secretpage.asp method=post> Username: <input type=text size=30 name=Username><BR> Password: <input type=password size=30 name=Password><BR> <input type=submit Value="Submit"> </form> <% End Sub %> <% Sub SecretInfo() %> <html> <head> <title>My Secret Page</title> </head> <body bgcolor=FFFFFF> <center> <h1>This is my secret info</h1> Hello !<BR> Do you want to be my friend? </center> </body> </html> <% End Sub %> </td> <td> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42</td></tr></tbody></table>Cookies method is very similar to Session method: the basic difference is that with Cookies method the information is save in the clients computer and not in the server, so it is more suitable for sites with a lot of visitors. This method implies sending information to the client and requesting it whenever the information is needed. Additionally, we will learn how to delete the information save in the clients computer when it is not necessary anymore. When the visitor gets to our asp file we may save information related with him in his computer. The order will be like this one: <% response.Cookies ("whatever")="information" %> When this line is executed, the visitor will have the information in his computer, and whenever we need that information, we may request it using this code: <% =request.Cookies ("whatever") %> or <% variable1=request.Cookies ("whatever") %> Let's try an example using Cookies method: let's consider we have visitors checking our site several times and we want to let them know how many times they have accessed to our computer. <center><table border="1" cols="1" width="550"> <tbody><tr><td>cookiesexample.asp try !</td></tr> <tr> <td><% If Request.Cookies ("NumberVisits")="" Then %> <% Response.Cookies ("NumberVisits")=1 %> This is your first visit to this page. Welcome. <% Else %> <% VarNumberVisits=Request.Cookies ("NumberVisits") VarNumberVisits=VarNumberVisits+1 Response.Cookies("NumberVisits")=VarNumberVisits %> Welcome back to this page. You have visited this page <% =VarNumberVisits %> times. <BR>Check my great links <BR>..... <BR>..... <% End If %> </td> </tr> </tbody></table></center> Cookies method may be used to show visitors specific information we have requested throw a form, as for example a list of links related to a specific theme, information to allow access to the content of a page or to personalize the page (background color, frames or not frames...), information to fill a form automatically, etc. Open and Read content from a text file Example 1: This one will be the basic code we need to open a text file: <center><table border="1" cols="2" width="350"> <tbody><tr> <td width="15">1 2 3 4 5 6 7 8 9 10 11 12</td> <td nowrap="nowrap"><% Set fs = CreateObject("Scripting.FileSystemObject") Set wfile = fs.OpenTextFile("c:\Mydir\myfile.txt") filecontent = wfile.ReadAll wfile.close Set wfile=nothing Set fs=nothing response.write(filecontent) %> </td> </tr> </tbody></table></center> Line 2 will create the appropriate environment which allows to perform the operations involving files in the server. We have defined a variable named "fs" to do it (we may change the name of this variable). In line 4 we have create a new variable named "wfile" and we have apply the method OpenTextFile to variable "fs". We have also define which is the exact location of the file we want to open in this line (the complete path is necessary). In line 5 we have read all the content of the file to a variable named "filecontent" using the instruction "ReadAll". Lines 7 to 9 are use to let the server know we have finished all operations involving files. In line 11 we have response to the client with the content in the variable "filecontent". Example 2: Let's suppose we have a file with different kind of information in each line (a name in the first line, the last name in the second one, and the age in the third one), and we want to use them separately. This one will be the script we may use: <center><table border="1" cols="2" width="350"> <tbody><tr> <td width="15">1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17</td> <td nowrap="nowrap"><% Set fs = CreateObject("Scripting.FileSystemObject") Set wfile = fs.OpenTextFile("c:\Mydir\myfile.txt") firstname = wfile.ReadLine lastname = wfile.ReadLine theage = wfile.ReadLine wfile.close Set wfile=nothing Set fs=nothing %> Your first name is <% =firstname %><BR> Your last name is <% =firstname %><BR> Your are <% =firstname %> years old<BR> </td> </tr> </tbody></table></center> This example is very similar to the previous one, but in this case each line we have read from "myfile.txt" has been saved to a different variable (lines 5 to 7), and they have been used in lines 15 to 17 to respond to the client. Example 3: This example will read all lines in the file, and the response page will include the content of each line with its line number. <center><table border="1" cols="2" width="350"> <tbody><tr> <td width="15">1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17</td> <td nowrap="nowrap"><% Set fs = CreateObject("Scripting.FileSystemObject") Set wfile = fs.OpenTextFile("c:\Mydir\myfile.txt") counter=0 do while not wfile.AtEndOfStream counter=counter+1 singleline=wfile.readline response.write (counter & singleline & "<br>") loop wfile.close Set wfile=nothing Set fs=nothing %> </td> </tr> </tbody></table></center> In line 6 we will define the variable "counter", and in line 7 to 11 we will repeated instructions within the Do_While _Loop until the file does not reach the end of the file (the condition is "not wfile.AtEndOfStream"). Example 4: Let's suppose we have a file with a number in line 1 and a second number in line 2. <center><table border="1" cols="2" width="350"> <tbody><tr> <td width="15">1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16</td> <td nowrap="nowrap"><% Set fs = CreateObject("Scripting.FileSystemObject") Set wfile = fs.OpenTextFile("c:\Mydir\myfile.txt") number1 = Clng(wfile.ReadLine) number2= Clng(wfile.ReadLine) number1and2 = number1 + number2 response.write (number1and2) wfile.close Set wfile=nothing Set fs=nothing %> </td> </tr> </tbody></table></center> In the previous examples we were able to save the value in a line to a variable, but that variable was a string class variable. In this example we have saved the content of line 1 and line 2 to variables "number1" and number2" by using the function "Clng". This function has allow us to add both numbers in line 9 and send the result to the client (line 10). Create and Write a text file Example 1: The basic code we need to create a file is very similar to that one we have used to open a file: <center><table border="1" cols="2" width="350"> <tbody><tr> <td width="15">1 2 3 4 5 6 7 8 9 10 11 12 13 14</td> <td nowrap="nowrap"><% thetext="Write this text in the file" Set fs = CreateObject("Scripting.FileSystemObject") Set wfile = fs.CreateTextFile("c:\Mydir\myfile.txt", True) wfile.Write (thetext) wfile.close Set wfile=nothing Set fs=nothing response.write("Text created") %> </td> </tr> </tbody></table></center> The differences to instructions when opening a file are line 6 and line 7: The method used in line 6 is "CreateTextFile"; it is necessary to indicate the complete path to the file we want to create; in line 6 we may use the instruction True (to allow over-writing an existing file) or False (if the file exits, it is not over-written). Line 7 will write in the file the string in variable "thetext". We may also use this instruction to add content to the file wfile.WriteLine (thetext1) wfile.WriteLine (thetext2) ... In this case we will write the content in variable "thetext1" in line 1, content in "thetext2" in line 2 etc. Example 2: Let suppose we want to record the IP address of all visitor to our page to a file named "mylog.txt". <center><table border="1" cols="2" width="420"> <tbody><tr> <td width="15">1 2 3 4 5 6 7 8 9 10 11 12 13 14</td> <td nowrap="nowrap" width="20%"><% VisitorsIP=Request.ServerVariables ("REMOTE_ADDR") Set fs = CreateObject("Scripting.FileSystemObject") Set wfile = fs.OpenTextFile("c:\Mydir\mylog.txt", 8,false,0) wfile.WriteLine (VisitorsIP) wfile.close Set wfile=nothing Set fs=nothing response.write("IP registered") %> </td> </tr> </tbody></table></center> The IP address is requested in line 2 (check Functions and Procedures). In this case we have open the file "mylog.txt" in line 6 with the instruction "forappending". this instruction will allow us to open the file and add at the end of it the IP address of our last visitor. Global.asa is a text file locate in your main directory (/global.asa). Bellow is shown the basic extructure of a global.asa file. <center><table border="1" cols="1" width="550"> <tbody><tr> <td bgcolor="#bfffbf">global.asa</td> </tr> <tr> <td bgcolor="#bfbfff"><SCRIPT LANGUAGE="VBScript" RUNAT="Server"> Sub Application_OnStart ........ End Sub Sub Application_OnEnd ........ End Sub Sub Session_OnStart ........ End Sub Sub Session_OnEnd ........ End Sub </SCRIPT> </td> </tr> </tbody></table></center> This file will be activated in this cases:
Active Users Counter Just copy the code in the table to a text file and save it in the main directory of your site ("/global.asa"). <center><table border="1" width="550"> <tbody><tr> <td bgcolor="#bfffbf">global.asa</td> <td> </td> </tr> <tr> <td bgcolor="#bfbfff"><SCRIPT LANGUAGE="VBScript" RUNAT="Server"> Sub Application_OnStart application("activevisitors")=0 End Sub Sub Application_OnEnd End Sub Sub Session_OnStart application.lock application("activevisitors")=application("activev isitors")+1 application.unlock End Sub Sub Session_OnEnd application.lock application("activevisitors")=application("activev isitors")-1 application.unlock End Sub </SCRIPT> </td> <td> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 </td></tr> </tbody></table></center> The first time a visitor gets to our pages, global.asa will be executed, and consequently, application("activevisitors") in line 4 will get a value equal to "0". Immediately (as a new session has started), in line 12, application("activevisitors") will be increased by one. Each time a new visitor gets to our pages application("activevisitors") will be increased by one, and identically, each time a session is finished, this parameter will be reduce by one (line 18). In case we want to show the number of visitors in our page, we must use this kind of code : <table border="1" cols="1" width="550"> <tbody><tr> <td bgcolor="#bfffbf">index.asp</td> </tr> <tr> <td bgcolor="#bfbfff">There are <% =application("activevisitors") %> active visitors.</td> </tr> </tbody></table> |
|
|
|
|
|
#5 (permalink) |
|
Administrator
Posts: 18,715
Join Date: Jan 2006
Rep Power: 10
IM:
|
First, let´s check this two pages: <center><table border="1" width="550"> <tbody><tr> <td bgcolor="#bfffbf">javascript.html</td> <td> </td> </tr> <tr> <td bgcolor="#bfbfff"><html> <title>My page</title> <body> <script language="javascript" src="javascript.asp"></script> </body> </html> </td> <td>1 2 3 4 5 6 7 8</td> </tr> </tbody></table></center> <center><table border="1" width="550"> <tbody><tr> <td bgcolor="#bfffbf">javascript.asp</td> <td> </td> </tr> <tr> <td bgcolor="#bfbfff">document.write ("hello")</td> <td>1</td> </tr> </tbo |