程式|動態
一、讓網站和使用者實現互動
你可以製作一個像SendUserInfo.htm似的網頁,只用把<form method="POST" action="--WEBBOT-SELF--">改成<form method="POST" action="UserInfo.asp">就可以把你所提交的資料傳送給UserInfo.asp處理。
UserInfo.Asp的原始碼 Request.Form("UserName")、Request.Form("UserGender")、Request.Form("UserInfo")中UserName、UserGender、UserInfo就是SendUserInfo.htm中要提交資料的name表示。你可以把SendUserInfo.htm和UserInfo.asp對比來看。下面是UserInfo.asp的原始碼的部分解釋
<% ASP採用的是VBScript語言,你也可以更改為JavaScript語言。 %>
<%@ Language=VBScript %>
<% 'UserInfo.asp
Option Explicit
Response.Expires=0
Dim StrName, StrGender, StrM, StrMsg
'擷取SenderUserInfo.htm中姓名:name="UserName"傳送過來的資料
StrName = Trim(Request.Form("UserName")) 'Trim函數用來除首尾空格
'擷取SenderUserInfo.htm中性別::name="UserGender"傳送過來的資料
StrGender = Trim(Request.Form("UserGender"))
'擷取SenderUserInfo.htm中留言::name="UserInfo"傳送過來的資料
StrM = Trim(Request.Form("UserInfo"))
StrMsg = Replace(StrM,vbcrlf,"<Br>" & vbcrlf)
%>
<HTML><BODY>
<% '顯示擷取的資料 %>
姓名: <%= StrName%><Br><Br>
性別: <%= StrGender%><Br><Br>
留言: <Br><Br>
<%= StrM%><Br><Br>
</BODY></HTML>
二、網站計數器
功能:所有訪問過本網頁的IP地址
實現方法:把所有訪問過本網頁的IP地址存放在Count.Asp中,每過一段時間,查看一下,登陸到本網站的IP地址,如果在Count.ASP中不存在,說明這個使用者以前沒有來過,加入到Count.asp,如果已經存在,則說明該使用者以前已經登陸過本網站,可以忽略。
1:獲得訪問過本網頁的IP地址,存入OLdIP數組中
CountFile=Server.mappath("Count.Asp")
Set StreamF=CreateObject("Scripting.FileSystemObject")
dim OldIP()
Set ThisFile=StreamF.OpenTextFile(CountFile,1,False)
do while not ThisFile.AtEndOfStream
Thisline = ThisFile.readline
'使用了 Preserve 關鍵字,就只能調整數組最後維的大小,並且不能改變數組的維數。
'數組只有一維,該維是最後的也是僅有的一維,就可以修改該數組的大小.
Redim preserve OldIP(Countly)
OldIP(Countly) = Thisline
'Countly 記載這ThisFile的行數
Countly = Countly + 1
loop
ThisFile.Close
2:開始檢查當前訪問者是否把當前的IP地址加入到Count.ASP中,用OLDIP數組來判斷
Flage =1
ArriveIP = Request.ServerVariables("REMOTE_ADDR")
'Response.Write ArriveIP
for I = 1 to Countly
'Request.ServerVariables("REMOTE_ADDR") 獲得發出請求機器的IP
if (ArriveIP = OldIP(I-1)) then
Flage=0
end if
next
3:如果當前的IP步在Count.asp中,加入到Count.asp,判斷是否加入到Count.asp是根據2中的Flags來判斷
Set WriteF = StreamF.OpenTextFile(CountFile, 8, 0)
if Flage=1 Then
Countly = Countly +1
WriteF.WriteLine ArriveIP
End if
WriteF.Close
4:顯示訪問過本網頁的IP地址
<%
Set ThisFile=StreamF.OpenTextFile(CountFile,1,False)
do while not ThisFile.AtEndOfStream
Thisline = ThisFile.readline
Response.Write ThisLine&chr(13)
loop
ThisFile.Close
%>
三、投票系統的建立
功能:對欄目進行投票,選擇你喜歡的欄目。
實現方法:把投票的結果存在Result.txt檔案中,當前工作的網頁是Voting.asp,提交的對象是Voted.asp,當單擊查看按鈕時,可以查看當前選票的結果。中間存在的Bug,就是當提交之後,繼續重新整理Voted.asp,這樣選票結果會自動的添加,想想這是為什嗎?用什麼方法來解決?你時候碰到過類似的網站。
Voting.asp的關鍵是:
1:顯示查看結果
<script language=javascript>
function windowOpen(loadpos)
{ window.open(loadpos,"surveywin","toolbar,resizable,scrollbars,dependent,
width=400,height=480");
}
</SCRIPT>
2:提交資料給Voted.asp <form method="POST" action="Voted.asp">
3:Voted.asp的關鍵是對下面兩句話,第一行儲存的是軟體欄目選票的數量,第二行儲存的是電腦工作室欄目的選票數量。下面一段程式,就是根據提交的資料自動的更新軟體欄目的選票數量或者電腦工作室欄目的選票數量。
<%
Set FileS= Server.CreateObject("Scripting.FileSystemObject")
If Request.Form("R1")="Soft" then
Set FileR= FileS.OpenTextFile(Server.MapPath("Result.txt"), 1, True)
Soft = FileR.Readline
Studio = FileR.Readline
FileR.Close
Soft=Int(Soft)+1
Set FileR= FileS.OpenTextFile(Server.MapPath("Result.txt"), 2, True)
FileR.WriteLine Soft
FileR.WriteLine Studio
FileR.Close
Else
Set FileR= FileS.OpenTextFile(Server.MapPath("Result.txt"), 1, True)
Soft = FileR.Readline
Studio = FileR.Readline
FileR.Close
Studio=Int(Studio)+1
Set FileR= FileS.OpenTextFile(Server.MapPath("Result.txt"), 2, True)
FileR.WriteLine Soft
FileR.WriteLine Studio
FileR.Close
End If
%>
4:下面一段程式時擷取兩個欄目的選票數量,同時計算出百分比,和得到選票的數量。
<%
Set FileS= Server.CreateObject("Scripting.FileSystemObject")
Set FileR= FileS.OpenTextFile(Server.MapPath("result.txt"), 1, True)
OSoft=FileR.Readline
OStudio=FileR.Readline
FileR.Close
nCount = Int(OSoft)+Int(OStudio)
Soft= (100 * Int(OSoft) ) / Int(nCount)
Studio= (100 * Int(OStudio)) / Int(nCount)
Soft = FormatNumber(Soft, 2)
Studio = FormatNumber(Studio, 2)
%>
四、顯示線上IP地址
功能:顯示當前網頁線上人數和IP地址。
方法:把當前的網頁線上IP地址存入People.asp中,每過一分鐘自動重新整理一次,用來統計當前網頁的線上人數和IP地址。
1:把當前的線上IP地址和訪問時間存入ly數組中。
Set ThisFile=StreamF.OpenTextFile(CountFile,1,False)
Countly=0
do while not ThisFile.AtEndOfStream
Thisline = ThisFile.readline
'使用了 Preserve 關鍵字,就只能調整數組最後維的大小,並且不能改變數組的維數。
'數組只有一維,該維是最後的也是僅有的一維,就可以修改該數組的大小.
Redim preserve ly(Countly)
ly(Countly) = Thisline
'Countly 記載這ThisFile的行數
Countly = Countly + 1
loop
ThisFile.Close
2:開始重新整理訪問當前網頁IP地址
sj中存的當前的系統時間
sameip=0
for i=1 to (Countly-1)/2
'取得 偶數列
'如果在剛才統計的線上IP地址和當前的時間超過了一分鐘,則上出該IP
if DateDiff("s",ly(i*2),sj)>60 then
ly(i*2-1)=""
ly(i*2)=""
Countly=Countly-2
end if
'擷取剛才線上的IP地址,如果現在還線上
'Request.ServerVariables("REMOTE_ADDR") 獲得發出請求機器的IP
if Request.ServerVariables("REMOTE_ADDR")=ly(i*2-1) then
sameip=1
ly(i*2)=sj
end if
next
3:將最新的訪問當前網頁的IP地址存入People.asp
'開始向People.asp這個檔案寫資料
set OutFile=StreamF.CreateTextFile(CountFile)
for i=0 to Countly-1
if ly(i)<>"" then
outFile.WriteLine ly(i)
end if
next
if sameip=0 then
outFile.WriteLine Request.ServerVariables("REMOTE_ADDR")
outFile.WriteLine sj
outFile.Close
end if