偶做留言本的時候想起做這么個,具體思路也許不好,做出來只是拋磚引玉,希望有更好的方法!
ip添加頁是用了一個listbox, textbox,兩個button,而在其他的頁上則直接用當前ip對比數據庫中的ip,代碼如下!
限制ip添加頁html代碼
<%@ page language="c#" autoeventwireup="true" codefile="ip.aspx.cs" inherits="admin_ip" %>
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>無標題頁</title>
<link href="../images/news.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:sqldatasource id="sqldatasource1" runat="server" connectionstring="<%$ connectionstrings:book %>"
providername="<%$ connectionstrings:book.providername %>" selectcommand="select [ip] from [ip]">
</asp:sqldatasource>
<table align="center" >
<tr>
<td rowspan="2" >
<asp:listbox id="iplxb" runat="server" datasourceid="sqldatasource1" datatextfield="ip"
datavaluefield="ip" height="194px" width="153px"></asp:listbox></td>
<td >
填寫標準的ip地址到左下文本框里面,然后點擊按紐添加!<br />
<asp:regularexpressionvalidator id="regularexpressionvalidator1" runat="server" controltovalidate="iptb"
display="dynamic" errormessage="ip地址格式不正確" validationexpression="([0-9]{2,3})([.])([0-9]{1,3})([.])([0-9]{1,3})([.])([0-9]{1,3})"></asp:regularexpressionvalidator></td>
</tr>
<tr>
<td >
<asp:linkbutton id="linkbutton1" runat="server" onclick="linkbutton1_click">刪除選中的行</asp:linkbutton></td>
</tr>
<tr>
<td >
<asp:textbox id="iptb" runat="server" width="150px">61.139.33.22</asp:textbox></td>
<td >
<asp:button id="button1" runat="server" onclick="button1_click" text="增加" width="80px" /></td>
</tr>
</table>
</div>
</form>
</body>
</html>
限制ip添加頁cs代碼
using system;
using system.data;
using system.configuration;
using system.collections;
using system.web;
using system.web.security;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.webcontrols.webparts;
using system.web.ui.htmlcontrols;
public partial class admin_ip : system.web.ui.page
{
protected void page_load(object sender, eventargs e)
{
server.execute("chklog.aspx");
}
protected void button1_click(object sender, eventargs e)
{
iplxb.items.add(iptb.text);
odb.insert("insert into ip (ip) values ('" + iptb.text + "')");
}
protected void linkbutton1_click(object sender, eventargs e)
{
for (int i = 0; i < iplxb.items.count; i++)
{
if (iplxb.items[i].selected)
{
odb.insert("delete from ip where ip='"+iplxb.selecteditem.text+"'");
iplxb.items.remove(iplxb.selecteditem.text);
}
}
}
}
被需要限制ip的頁面調用頁的代碼
protected void page_load(object sender, eventargs e)
{
string ip = request.userhostaddress.tostring();
if (convert.toint32(odb.scr("select count(*) from [ip] where ip='" + ip + "'")) > 0)
response.write("對不起,您的ip被限制訪問,請咨詢管理員");
}
}
新聞熱點
疑難解答
圖片精選