本改编的主要思想是:利用session功能,在第一次登陆时记下数据库的用户名和密码,在原程序中不显示用户名和密码,也避免多次重复填写用户密码。
一、db.inc.php
<?php
class ZDbMan
{
var $db;
var $strDbHost, $strDbUser, $strDbPass, $strDbName;
var $strQuery, $strErr;
var $strTableName;
var $nPage, $nPageSize;
var $strOutput;
function ZDbMan ()
{
$this->db = 0;
$this->strDbHost = "";
$this->strDbUser = "";
$this->strDbPass = "";
$this->strDbName = "";
$this->strErr = "";
$this->strTableName = "";
$this->nPage = 1;
$this->nPageSize = 20;
$this->strOutput = "";
}
function OpenDb ()
{
$this->db = mysql_connect ($this->strDbHost, $this->strDbUser, $this->strDbPass);
mysql_select_db ($this->strDbName);
}
function CloseDb ()
{
mysql_close ($this->db);
$this->db = 0;
}
function DoQuery ()
{
$nQueryResult = mysql_query ($this->strQuery, $this->db);
$err = mysql_error();
if ($err) {
$this->strErr .= $err;
return;
}
$this->strOutput .= "<table border=1>";
while ($aryRow = mysql_fetch_array ($nQueryResult)){
$str = "<tr>";
for ($i = 0; $i < count ($aryRow); $i ++){
$str .= "<td>" . $aryRow[$i] . "</td>";
}
$str .= "</tr>";
$this->strOutput .= $str;
}
$this->strOutput .= "</table>";
}
function SetRecordList ()
{
global $PHP_SELF;
if (($this->db == 0) || ($this->strTableName == ""))
return;
$strQuery = "SELECT Count(*) AS Rec_Count FROM " . $this->strTableName;
$nQueryResult = mysql_query ($strQuery, $this->db);
if ($aryRow = mysql_fetch_array ($nQueryResult))
$nCount = $aryRow["Rec_Count"];
else
$nCount = 0;
$nFirstRecord = ($this->nPage - 1) * $this->nPageSize;
if ($nFirstRecord >= $nCount)
$nFirstRecord = $nCount - $this->nPageSize;
if ($nFirstRecord < 0)
$nFirstRecord = 0;
$nLastRecord = $nFirstRecord + $this->nPageSize;
if ($nLastRecord >= $nCount)
$nLastRecord = $nCount - 1;
if ($nFirstRecord > 0)
$this->strOutput .= " <a href=" . $PHP_SELF . "?strTN=" . $this->strTableName . "&nPage=" . ($this->nPage - 1) . ">PrevPage</a>";
else
$this->strOutput .= " PrevPage";
if ($nLastRecord < $nCount - 1)
$this->strOutput .= " <a href=" . $PHP_SELF . "?strTN=" . $this->strTableName . "&nPage=" . ($this->nPage + 1) . ">NextPage</a>";
else
$this->strOutput .= " NextPage";
$this->strOutput .= "<br>";
$strQuery = "SELECT * FROM " . $this->strTableName . " LIMIT " . $nLastRecord;
$nQueryResult = mysql_query ($strQuery, $this->db);
$err = mysql_error();
if ($err) {
$this->strErr .= $err;
return;
}
$nCurRecord = 0;
$this->strOutput .= "<table border=1>";
while (($nCurRecord < $nFirstRecord) && ($aryRow = mysql_fetch_array ($nQueryResult)))
$nCurRecord ++;
while (($nCurRecord < $nLastRecord) && ($aryRow = mysql_fetch_array ($nQueryResult))){
$str = "<tr>";
for ($i = 0; $i < count ($aryRow); $i ++){
$str .= "<td>" . $aryRow[$i] . "</td>";
}
$str .= "</tr>";
$this->strOutput .= $str;
$nCurRecord ++;
}
$this->strOutput .= "</table>";
}
function SetTableList ()
{
global $PHP_SELF;
if ($this->db != 0) {
$this->strOutput .= "<table>";
$this->strOutput .= "<tr><td><font class=ftTitle>Tables</font></td></tr>";
$nResult = mysql_list_tables ($this->strDbName);
$nTableCount = mysql_num_rows ($nResult);
for ($i = 0; $i < $nTableCount; $i++) {
$strTableName = mysql_tablename ($nResult, $i);
$str = "<tr><td><a href=" . $PHP_SELF . "?strTN=" . $strTableName . ">";
$str .= $strTableName . "</a></td></tr>";
$this->strOutput .= $str;
}
$this->strOutput .= "</table>";
}
}
function SetOutput ()
{
$this->OpenDb ();
if ($this->strTableName != "")
$this->SetRecordList ();
if ($this->strQuery != "")
$this->DoQuery ();
$this->SetTableList ();
$this->CloseDb ();
}
function SetDbVariables ($strHost, $strUser, $strPass, $strDb)
{
$this->strDbHost = $strHost;
$this->strDbUser = $strUser;
$this->strDbPass = $strPass;
$this->strDbName = $strDb;
}
function SetQueryVariables ($strQuery)
{
$this->strQuery = $strQuery;
$this->strTableName = "";
}
function SetTableVariables ($strName, $nPage)
{
$this->strTableName = $strName;
$this->nPage = $nPage;
$strQuery = "";
}
}
?>
二、dblogin.php
<?php
// 档名 dblogin.php
if($user){
if(mysql_pconnect("localhost",$user,$password)){
session_start();
session_register("user");
session_register("password");
// echo "ok!";
Header("Location: dbm.php");
exit;
}
}else{
?>
<script language="javascript">
if (self != top)
top.location = self.location;
</script>
<?
}
?>
<form action=<?php printf ($PHP_SELF) ?> method=post>
<table width="289" border="1" cellspacing="0" cellpadding="1" bordercolor="#999999" align="center">
<tr bgcolor="#999999">
<td>
<div align="center"><font color="#FFFFFF" size="4">数据库登录</font></div>
</td>
</tr>
<tr>
<td>
<table width="267" border="0" cellspacing="0" cellpadding="5" align="center">
<tr>
<td width="94">
<div align="right">用户:</div>
</td>
<td width="169">
<input type="text" name="user" size="16" maxlength="16" value="<?php echo $user; ?>">
</td>
</tr>
<tr>
<td width="94">
<div align="right">口令:</div>
</td>
<td width="169">
<input type="password" name="password" size="16" maxlength="16">
</td>
</tr>
<tr>
<td colspan="2">
<div align="center">
<input type="submit" name="login" value=" 登 录 ">
</div>
</td>
</tr>
</table>
</td>
</tr>
</table>
</form>
三、dbm.php
<script language="javascript">
if (self != top)
top.location = self.location;
</script>
<html>
<head>
<title>db</title>
</head>
<frameset cols="150,*" rows="*" border="0" frameborder="0">
<frame src="dbshow.php" name="left">
<frame name="main">
</frameset>
<noframes>
<body bgcolor="#FFFFFF">
</body>
</noframes>
</html>
四、dbshow.php
<?
session_start();
// session_register("user");
// session_register("password");
if(!$user){
Header("Location: dblogin.php");
exit;
}else{
?>
<base target="main">
<?
}
$pconnect=mysql_pconnect("localhost",$user,$password);
$dbs=mysql_list_dbs($pconnect);
$num_dbs=mysql_numrows($dbs);
for($i_dbs=0; $i_dbs<$num_dbs; $i_dbs++){
$db = mysql_dbname($dbs, $i_dbs);
?>
<a HREF=db01.php?db01=<?echo $db?>><?echo $db?></a><BR>
<?
}
?>
五、db.php
<?
session_start();
require "db.inc.php";
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>Db Manager</title>
</head>
<style type="text/css">
<!--
.font {font-size: 9pt}
.ftTitle {font-size:12pt; color:#510051}
-->
</style>
<body>
<?php
$dbman = new ZDbMan;
$dbman->SetDbVariables ("localhost", $user,$password,$db);
if (isset ($strTN)){
if (!isset ($nPage))
$nPage = 1;
$dbman->SetTableVariables ($strTN, $nPage);
}else{
if (isset ($strQuery))
$dbman->SetQueryVariables ($strQuery);
}
if((isset ($strTN))||(strQuery)){
$dbman->SetOutput ();
printf ($dbman->strOutput);
}
?>
<hr>
<form action=<?php printf ($PHP_SELF) ?> method=post>
<table>
<tr><td>Query</td>
<td><textarea name=strQuery rows=4 cols=64></textarea>
</td>
</tr>
<tr><td><input type=submit value=submit></td>
<td><input type=reset value=reset></td>
</tr>
<tr><td><?echo $user ?></td>
<td><?echo $password ?></td>
<td><?echo $db ?></td>
</tr>
</table>
</form>
</body>
</html>
|