#freeze
閲覧総計:&counter();  (本日:&counter(today);  昨日:&counter(yesterday);)

 Webプログラミング入門課題の定番「アクセスカウンタ」を作ってみた。 09/08/13

【特徴】Cookie機能を利用し、12時間内の再訪問の場合は、カウンタがカウント
アップしない様に工夫してみた。
 また、どのページでもこのアクセスカウンタが利用できる様に「Webユーザコント
ロール(*.ascx)」で部品化した。
・カウント記録ファイルとして「ファイル名+Counter.txt」ファイルが自動作成されます。

(ポイント)
・「こんなに簡単に作れちゃうんだ。」を体感。
・汎用部品の作り方(Webユーザコントロール)の手法確認

開発環境:VWD2005+AJAX1.0+Toolkit + SQL Server2005
サーバ:ASP.NET2.0+AJAX1.0 + SQL Server2005

【稼動サンプル】
http://www.kuri6005.useiis7.net/4AspNet10/Counter/ascxCounter.aspx
#ref(Counter01.JPG);

【CounterControl.ascx】アクセスカウンタ部品
 <%@ Control Language="VB" ClassName="CounterControl" %>
 
 <script runat="server">
 
     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
         Dim HOLDTIME As Integer = 720 'Cookie保持時間(分)
         'ポストバック表示でない場合
         If Not Page.IsPostBack Then
             'アクセスカウンタ
             Dim sr As String = Server.MapPath(System.IO.Path.GetFileNameWithoutExtension(Request.ServerVariables("URL")) & "Counter.txt")
             Dim cokcount As String
             If System.IO.File.Exists(sr) Then
                 '*Counter.txtファイルが既に存在する場合
                 'countデータの読込み
                 Dim count As String = System.IO.File.ReadAllText(sr)
                 'Cookieハンドルの定義
                 Dim cok As HttpCookie = Request.Cookies(System.IO.Path.GetFileNameWithoutExtension(Request.ServerVariables("URL")) & "CokCheck")
                 If Not (cok Is Nothing) Then
                     'Cookie情報があった場合
                     ltrCounter.Text = count
                     cok.Values("cokcount") = count
                     cok.Expires = DateTime.Now.AddMinutes(HOLDTIME)
                     Response.Cookies.Add(cok)
                 Else
                     'Cookie情報がなかった場合
                     'インスタンスをセット
                     cok = New HttpCookie(System.IO.Path.GetFileNameWithoutExtension(Request.ServerVariables("URL")) & "CokCheck")
                     count += 1 'カウンタをカウントアップする
                     ltrCounter.Text = count
                     System.IO.File.WriteAllText(sr, count)
                     cok.Values("cokcount") = count
                     cok.Expires = DateTime.Now.AddMinutes(HOLDTIME)
                     Response.Cookies.Add(cok)
                 End If
             Else
                 '*Conuter.txtファイルが存在しない場合
                 'Cookieハンドルの定義
                 Dim cok As HttpCookie = Request.Cookies(System.IO.Path.GetFileNameWithoutExtension(Request.ServerVariables("URL")) & "CokCheck")
                 'インスタンスをセット
                 cok = New HttpCookie(System.IO.Path.GetFileNameWithoutExtension(Request.ServerVariables("URL")) & "CokCheck")
                 Dim count As String
                 count = 1
                 ltrCounter.Text = count
                 System.IO.File.WriteAllText(sr, count)
                 cok.Values("cokcount") = count
                 cok.Expires = DateTime.Now.AddMinutes(HOLDTIME)
                 Response.Cookies.Add(cok)
             End If
         End If
     End Sub
     
 </script>
 
 
 <asp:Literal ID="ltrCounter" runat="server"></asp:Literal>


【ascxCounter.aspx】稼動確認フォーム
 <%@ Page Language="VB" %>
 
 <%@ Register Src="CounterControl.ascx" TagName="CounterControl" TagPrefix="uc1" %>
 
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
 <script runat="server">
 
 Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
         Label1.Text = "こんにちは、" & TextBox1.Text & " さん!"
 End Sub
 </script>
 
 <html xmlns="http://www.w3.org/1999/xhtml" >
 <head runat="server">
     <title>無題のページ</title>
 </head>
 <body>
     <form id="form1" runat="server">
     <div>
         アクセスカウンタ (Webユーザコントロール(*.ascx)仕様) 09/08/13<br />
         <br />
         閲覧数:
         <uc1:CounterControl ID="CounterControl1" runat="server" />
         <br />
         <br />
         あいさつを返してくれる<br />
         お名前:<asp:TextBox ID="TextBox1" runat="server" Width="152px"></asp:TextBox>
         <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="送信" /><br />
         <asp:Label ID="Label1" runat="server"></asp:Label></div>
     </form>
 </body>
 </html>


【参考にしたページ】
1.アクセスカウンタを作ろう
http://ufcpp.net/study/aspx/counter.html

2.自分自身のファイル名を取得する法
http://www.microsoft.com/japan/msdn/community/gdn/ShowPost-37253.htm



【サーバ設置時のハマリポイント】
 このアクセスカウンタ部品CounterControl.ascxをサーバに設置し、利用しようと
したところエラーが発生し、最初はうまく動いてくれなかった。原因は、対象フォル
ダに「書き込み権限」の許可がされていなかった為、*Counter.txtファイルが書き込
めなかったからだった。
 皆さん、サーバ対象フォルダの「書き込み権限」の許可設定を忘れないようにしま
しょう。 09/08/13

(設定操作)
 サーバにloginし、「ファイルのフォルダの管理(ファイルマネジャ)」で
対象プログラムを設置したフォルダのWeb Visitorの「書き込み」にチェックを付け、
「Apply」ボタンをクリックし、情報更新を行う。
#ref(textRW02.JPG);
- 1 -- [[1]] &new{2012-08-05 (日) 04:15:32};
- 1 -- [[1]] &new{2012-08-05 (日) 04:15:35};
- 1 -- [[-1']] &new{2012-08-05 (日) 04:15:37};
-- 1' -- [[1]] &new{2012-08-05 (日) 04:15:38};
- 1 -- [[1]] &new{2012-08-05 (日) 04:15:39};
- 1 -- [[1]] &new{2012-10-09 (火) 07:01:10};
- 1 -- [[1]] &new{2012-10-09 (火) 07:01:32};
- 1 -- [[-1']] &new{2012-10-09 (火) 07:01:40};
-- 1' -- [[1]] &new{2012-10-09 (火) 07:01:42};
- 1 -- [[1]] &new{2012-10-09 (火) 07:01:50};
- 1 -- [[1]] &new{2014-01-01 (水) 22:31:22};
- 1 -- [[1]] &new{2014-01-01 (水) 22:31:35};
- 1 -- [[-1']] &new{2014-01-01 (水) 22:31:38};
-- 1' -- [[1]] &new{2014-01-01 (水) 22:31:40};
- 1 -- [[1]] &new{2014-01-01 (水) 22:31:45};

#comment_nospam
- 1 -- [[1]] &new{2014-01-01 (水) 22:31:29};
- 1 -- [[1]] &new{2012-10-09 (火) 07:01:21};
- 1 -- [[1]] &new{2012-08-05 (日) 04:15:34};
#vote(参考になった[2],ふつう[1],参考にならなかった[0])


トップ   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS