Counter เป็นเครื่องมือตัวหนึ่งที่จะคอยนับและบอกจำนวนตัวเลยผู้เข้าเยี่ยมชม
Homepage โดย Counter แสดง Counter Number ทุกครั้ง
เมื่อมีการ Reload หรือเข้าเยี่ยม Homepage
ครั้งต่อไปก็จะต้องแสดง New Counter Number New
อีก
วิธีการ
- กำหนด Path directory ที่เก็บ
file Counter
- สร้าง text file ชื่อ hits.txt
พิมพ์ค่าลงไป 1 แล้ว save
- นำ hits ไปไว้ใน Path directory
ที่กำหนด
หลักการและเงื่อนไขการ
Text Counter
- เมื่อมีการ Access File Homepage
แล้วให้เปิด
file hits.txt
- จากนั้นอ่านค่า
Counter เก่า
- บวกค่าใหม่เข้าไปอีก
1
- เขียนค่า Counter
ตัวใหม่ใน hitst.txt ลงทับค่าเก่า
ตัวอย่างและคำอธิบาย
Counter
counter.asp
<%
Set FileObject = Server.CreateObject("Scripting.FileSystemObject")
HitsFile = Server.MapPath ("/aspsamp/test_asp/counter") & "\hits.txt"
'กำหนด Path ของ Web server directory
'กำหนด File hits.txt เก็บตัวเลข Counter
Set InStream= FileObject.OpenTextFile (HitsFile, 1, false )
'กำหนด FileObject.OpenTextFile ให้เปิด File
OldHits = Trim(InStream.ReadLine)
NewHits = OldHits + 1
'เพิ่มค่า Counter 1 ที่ครั้งที่เปิด File
Set OutStream= FileObject.CreateTextFile (HitsFile, True)
OutStream.WriteLine(NewHits)
'กำหนด FileObject.CreateTextFile ให้เขียนทับข้อมูลเก่า
%>
<CENTER><FONT SIZE=3 FACE="MS Sans Serif"><B>
This page has been accessed <FONT COLOR=RED><%=NewHits%></FONT> times.
</FONT></B></CENTER>
<%'แสดง Text Counter ออกสู่หน้าจอ%>
|

|