使用php从mysql中读取tac_plus的log页面改进

2011年4月1日 | 分类: Linux | 标签: , , , ,

之前做的页面没有分页的功能,现在改进了一下,添加了分页的功能,下边是代码:

1.index.php

<html>
<head>
<title>Tacacs+ Log Analysis Tool</title>
</head>
<center>
<br>
<br>
<br>
<body>
<font size=”12″ face=”arial black”>
Tacacs+ Accounting
</font>
</body>
<br>
<br>
<?php
include(“search.php”);
?>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<font size=”2″ color=”#005AB5″>
Copyright By MIS
</font>
</body>
</center>
</html>

2.search.php

<html>
<head>
<title>Tacacs+ Log Analysis Tool</title>
</head>
<center>
<br>
<br>
<body>
<form action=”acctlog.php” method=”GET”>
<font face=”dotum”>
Date: <input type=”text” name=”date”/>
Username: <input type=”text” name=”username” />
ServerIP: <input type=”text” name=”serverip” />
<input type=”submit” value=”Search”/>
</font>
</form>
</center>
</body>
</html>

3.acctlog.php

<META HTTP-EQUIV=”Content-Type” CONTENT=”text/html; charset=utf-8″>
<html>
<head>
<title>Tacacs+ Log Analysis Tool</title>
</head>
<center>
<br>
<br>
<br>
    <h1>Tacacs+ Accounting</h1>
</body>
<br>
<?php
include(“search.php”);
$link=mysql_connect(“localhost”,”root”,”abcd.1234″);
mysql_select_db(“test”, $link);
$date = $_GET['date'];
$username = $_GET['username'];
$serverip = $_GET['serverip'];
$perpagenum = 50;
$p = “SELECT count(*) FROM acct where date like ‘%$date%’ and username like ‘%$username%’ and serverip like ‘%$serverip%’”;
$total = mysql_fetch_array(mysql_query($p));
$Total = $total[0];
$Totalpage = ceil($Total/$perpagenum);
if(!isset($_REQUEST['page'])||!intval($_REQUEST['page'])||$_REQUEST['page']>$Totalpage)//page可能的四种状态
{
$page=1;
}
else
{
$page=$_REQUEST['page'];
}
$startnum = ($page-1)*$perpagenum;
if($date==” and $username==” and $serverip==”)
         $q=”select * from acct order by id limit $startnum,$perpagenum”;
    elseif($date!=” and $username==” and $serverip==”)
         $q=”select * from acct where date like ‘%$date%’ order by id limit $startnum,$perpagenum”;
    elseif($date==” and $username!=” and $serverip==”)
         $q=”select * from acct where username like ‘%$username%’ order by id limit $startnum,$perpagenum”;
    elseif($date==” and $username==” and $serverip!=”)
         $q=”select * from acct where serverip like ‘%$serverip%’ order by id limit $startnum,$perpagenum”;
    elseif($date!=” and $username!=” and $serverip==”)
         $q=”select * from acct where date like ‘%$date%’ and username like ‘%$username%’ order by id limit $startnum,$perpagenum”;
    elseif($date!=” and $name==” and $ip!=”)
         $q=”select * from acct where date like ‘%$date%’ and serverip like ‘%$serverip%’ order by id limit $startnum,$perpagenum”;
    elseif($date==” and $name!=” and $ip!=”)
    else
         $q = “SELECT * FROM acct where date like ‘%$date%’ and username like ‘%$username%’ and serverip like ‘%$serverip%’”;
mysql_query(“SET NAMES GB2312″);
$rs = mysql_query($q, $link);
if(!$rs){die(“Valid result!”);}
?>
<center><table width=100% cellspacing=0 cellpadding=3 border=1>
<center>
                        <tr>
                                <td>Date</td>
                                <td>Time</td>
                                <td>ServerIP</td>
                                <td>Username</td>
                                <td>TTY</td>
                                <td>ClientIP</td>
                                <td>Status</td>
                                <td>TaskID</td>
                                <td>Service</td>
                                <td>Privilege</td>
                                <td>Information</td>
                        </tr>
</center>
<?php
while($row = mysql_fetch_row($rs))
{
?>
<center>
                                <tr>
                                        <td><?=$row[1]?></td>
                                        <td><?=$row[2]?></td>
                                        <td><?=$row[3]?></td>
                                        <td><?=$row[4]?></td>
                                        <td><?=$row[5]?></td>
                                        <td><?=$row[6]?></td>
                                        <td><?=$row[7]?></td>
                                        <td><?=$row[8]?></td>
                                        <td><?=$row[9]?></td>
                                        <td><?=$row[10]=$row[10]!=”"?$row[10]:”-”?></td>
                                        <td><?=$row[11]=trim($row[11])!=”"?$row[11]:”-”?></td>
                                </tr>
</center>
<?php
}
?>
</table>
<br>
<br>
<?php
$per = $page – 1;//上一页
$next = $page + 1;//下一页
$aa = $_SERVER['PHP_SELF'];
if($Totalpage==’0′)
{
echo “<center>共有”.$Total.”条记录,每页”.$perpagenum.”条,”;
echo “共1页,当前是第1页”;
}
else{
echo “<center>共有”.$Total.”条记录,每页”.$perpagenum.”条,”;
echo “共”.$Totalpage.”页,当前是第”.$page.”页”;
if($page > 1)
{
echo “<a href=’$aa?date=”.$date.”&username=”.$username.”&serverip=”.$serverip.”‘>首页</a>”;
echo “<a href=’$aa?date=”.$date.”&username=”.$username.”&serverip=”.$serverip.”&page=”.$per.”‘> 上一页</a>”;
}
if($page != $Totalpage)
{
echo “<a href=’$aa?date=”.$date.”&username=”.$username.”&serverip=”.$serverip.”&page=”.$next.”‘> 下一页</a>”;
echo “<a href=’$aa?date=”.$date.”&username=”.$username.”&serverip=”.$serverip.”&page=”.$Totalpage.”‘> 尾页</a></center>”;
}
//else
//{
//echo “<center>No message</center>”;
//}
}
?>
<br>
<body>Copyright By MIS</body>
</center>

Written by an6097

目前还没有任何评论.
注意: 评论者允许使用'@user空格'的方式将自己的评论通知另外评论者。例如, ABC是本文的评论者之一,则使用'@ABC '(不包括单引号)将会自动将您的评论发送给ABC。使用'@all ',将会将评论发送给之前所有其它评论者。请务必注意user必须和评论者名相匹配(大小写一致)。