首先感谢大家对XajaxBook的关心和支持,新版中放弃了Smarty,当初加入也是为了学习一下smarty,其实臃肿的adodb也可以去掉,但 自己已经习惯了用这个数据库操作类了就不再另外写了,代码也很简单,大家可以拿自己的数据库操作类来替换掉,新版加入了管理功能,可以删除回复留言,查看 留言者IP地址,管理帐号密码就放在config文件里面,没有用数据库,大家可以修改成数据库的安全性会好一点,感冒折磨的我什么都不想做了,原谅我的 偷懒吧

演示:http://www.im502.com/demo/XajaxBook/
管理帐号:admin 密码:123456

应用案例:http://www.797audio.com/gb/

废话少说,看目录结构:

QUOTE:|+adodb  
|+images
|+xajax
|-al.js
|-init.php
|-db.config.php
|-index.php
|-gb.sql
数据库:

QUOTE:CREATE TABLE `aj_book` (
   `id` int(8) NOT NULL auto_increment,
   `nickname` varchar(200) NOT NULL,
   `email` varchar(200) NOT NULL,
   `homepage` varchar(200) NOT NULL,
   `ip` varchar(200) NOT NULL,
   `post` text NOT NULL,
   `posttime` datetime NOT NULL,
   `reply` text NOT NULL,
   PRIMARY KEY   (`id`)
) ENGINE=MyISAM   DEFAULT CHARSET=utf8 COMMENT=’post’ AUTO_INCREMENT=34 ;


— 导出表中的数据 `aj_book`

INSERT INTO `aj_book` (`id`, `nickname`, `email`, `homepage`, `ip`, `post`, `posttime`, `reply`) VALUES
(1, ‘Lily’, ‘adfa’, ‘asdfa’, ‘127.0.0.1’, ‘dsafdfd’, ‘2007-10-19 11:30:22’, ‘re’),
(2, ‘Tom’, ‘adfaw’, ‘wewqq’, ‘127.0.0.1’, ‘fadfdafadf’, ‘2007-10-19 11:31:17’, ”, ‘0000-00-00 00:00:00’),
db.config.php

[Copy to clipboard] [ – ]CODE:<?php
/**
* XajaxBook 留言簿数据库配置文件
* ============================================================================
* 本软件欢迎转载 但请保留作者信息
* 网站地址: http://www.im502.com
* ============================================================================
* @file:    db.config.php
* @author:     im502 <im502@126.com>
* @version: V1.0 Beta
* @update:     2007-11-5
* ———————————————
*/
// database host
$db_host = ‘localhost’;
// database name
$db_name = ‘mydb’;
// database username
$db_user = ‘root’;
// database password
$db_pass = ‘password’;
//manage username
$adminnane=’797audio’;
//password
$gbpass=’35845268c63ab03ef60f625e92cf6dff’;
?>
init.php

[Copy to clipboard] [ – ]CODE:<?php
/**
* XajaxBook 留言簿公用文件
* ============================================================================
* 本软件欢迎转载 但请保留作者信息
* 网站地址: http://www.im502.com
* ============================================================================
* @file:    init.php
* @author:     im502 <im502@126.com>
* @version: V1.0 Beta
* @update:     2007-11-5
* ———————————————
*/
/*包含数据库连接文件,adodb类库,xajax类库*/
include_once("db.config.php");
require_once("adodb/adodb.inc.php");
require_once("xajax/xajax.inc.php");
/* 初始化数据库类 */
$db = &NewADOConnection(‘mysql’);
$db->Connect($db_host, $db_user, $db_pass, $db_name) or die("Connect database failed");
$db->query("SET NAMES ‘gb2312’;");
$db->debug = 0;//是否开启mysql侦错
?>
index.php

[Copy to clipboard] [ – ]CODE:/<?php
/**
* XajaxBook 留言簿主程序文件
* ============================================================================
*   本软件欢迎转载 但请保留作者信息
* 网站地址: http://www.im502.com
* ============================================================================
* @file:    index.php
* @author:     im502 <im502@126.com>
* @version: V1.0 Beta
* @update:     2007-11-5
* ———————————————
*/
session_start();
/* 包含公共文件 */
require_once (‘init.php’);
$xajax = new xajax();
//$xajax->debugOn();//开启调试
$xajax->decodeUTF8InputOn();
/* xml字符过滤函数*/
function xml_escape($string) {
     return str_replace(array(‘&’,’"’,"’",'<‘,’>’),
         array(‘&’,’"’,’&apos;’,’&lt;’,’&gt;’),
         $string    
     );
}
/**
* 获得用户的真实IP地址
*
* @access   public
* @return   string
*/
function real_ip()
{
     if (isset($_SERVER))
     {
         if (isset($_SERVER["HTTP_X_FORWARDED_FOR"]))
         {
             $arr = explode(‘,’, $_SERVER["HTTP_X_FORWARDED_FOR"]);
         /* 取X-Forwarded-For中第一个非unknown的有效IP字符串 */
         foreach ($arr AS $ip)
         {
            $ip = trim($ip);
            if ($ip != ‘unknown’)
            {
                   $realip = $ip;
                   break;
            }
         }
         }
         elseif (isset($_SERVER["HTTP_CLIENT_IP"]))
         {
             $realip = $_SERVER["HTTP_CLIENT_IP"];
         }
         else
         {
             $realip = $_SERVER["REMOTE_ADDR"];
         }
     }
     else
     {
         if (getenv(‘HTTP_X_FORWARDED_FOR’))
         {
             $realip = getenv(‘HTTP_X_FORWARDED_FOR’);
         }
         elseif (getenv(‘HTTP_CLIENT_IP’))
         {
             $realip = getenv(‘HTTP_CLIENT_IP’);
         }
         else
         {
             $realip = getenv(‘REMOTE_ADDR’);
         }
     }
     return $realip;
}
/* 注册xajax自定义函数*/
$xajax->registerFunction("postmessage");
$xajax->registerFunction("show");
$xajax->registerFunction("adminlogin");
$xajax->registerFunction("logout");
$xajax->registerFunction("del");
$xajax->registerFunction("reply");
$xajax->registerFunction("addreply");
/* 创建自定义函数postmessage()*/
/**
* 发布留言
*
* @access   public
* @param string    $from    表单ID
* @return   void
*/
function postmessage($form)
{
    global $db;
     $objResponse = new xajaxResponse();
     /* 获取表单传递的参数 */
     $Nick=xml_escape($form[‘Nick’]);
     $HomePage=xml_escape($form[‘HomePage’]);
    $Email=xml_escape($form[‘Email’]);
     $Message=xml_escape($form[‘Message’]);
     $ip=real_ip();
    $errmsg=""; //初始化错误提示
     /* 表单验证 */
    if (trim($Nick) == "")   $errmsg.="Nick is required!n";
    else if (trim($Message) == "") $errmsg.="Message is required!n";
    else if (!(preg_match("/^w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*$/", $Email)))   $errmsg.="Email is wrong!n";
    else if ($errmsg=="")
    {
      /* 入库操作 */
         $sql="insert into aj_book(nickname,email,homepage,ip,post,posttime) values(‘$Nick’,’$Email’,’$HomePage’,’$ip’,’$Message’,NOW())";
      $result=$db->Execute($sql);
         $rs=$db->getRow("select * from aj_book where id=".$db->Insert_ID());
         if($rs)
         {
             $scontent.='<div class="post" id="post_’.$rs[id].’">’.’     <h3>Nick Name : <a href="mailto:’.$rs[email].’" title="send mail">’.$rs[nickname].'</a> |   <a href="’.$rs[homepage].’">HomePage</a> | Post at ‘.$rs[posttime].'</h3>’.’     <div class="entry">’.nl2br($rs[post]).'</div></div>’;
             /* 将新入库的记录用xajaxResponse的addPrepend 插入最新留言位置
             注意此处为减轻服务器负担不再调用分页重载,所以新插入后分为位置显示的留言总条数等信息并没有更新
             页面的记录也比分页中的per_page多了一条出来,本人觉得这样做无伤大雅 ^_^
             如需要重载可将下面的语句替换为:$objResponse->addScript("xajax_show();");
             */
         $objResponse->addPrepend("content","innerHTML",$scontent);
         }
         else
         {
             $db->die();
         }
    }
    else
    {
         //弹出错误信息
         $objResponse->addAlert($errmsg);  
    }

    return $objResponse;
}
/* 创建自定义函数login()*/
/**
* 登录
*
* @access   public
* @param string    $from    表单ID
* @return   void
*/
function adminlogin($form)
{
    global $db,$adminnane,$gbpass;
     $objResponse = new xajaxResponse();
     /* 获取表单传递的参数 */
     $admin=trim($form[‘admin’]);
     $pass=md5($form[‘pass’]);
    $errmsg=""; //初始化 错误提示
     /* 表单验证 */
    if(!($admin==$adminnane))
    {
       $errmsg.="Username is wrong!n";
    }
    else if(!($pass==$gbpass))
    {
       $errmsg.="Password is wrong!n";
    }
    if ($errmsg=="")
    {
       // 登录成功 注册session
         $_SESSION[‘admin’]=$admin;
      $objResponse->addScript("xajax_show();");//重载message,出现回复和删除的链接
         //登录后将登录按钮变为注销
      $objResponse->addAssign("log","innerHTML",'<a href="javascript:void(0)" onclick="xajax_logout();">Logout</a>’);
    }
     //弹出提示信息
     else   $objResponse->addAlert($errmsg);
    return $objResponse;
}
/* 创建自定义函数logout()*/
/**
* 注销
*
* @access   public
* @param string    $from    表单ID
* @return   void
*/
function logout()
{
     $objResponse = new xajaxResponse();
     session_destroy();
     $objResponse->addScript("xajax_show(1);");
     $objResponse->addAssign("log","innerHTML",'<a href="javascript:void(0)" onclick="lAlert();">Manage</a>’);
    return $objResponse;
}
/* 创建自定义函数reply()*/
/**
* 回复留言
*
* @access   public
* @param string    $id    留言ID
* @return   void
*/
function reply($id)
{
     $objResponse = new xajaxResponse();
     //点回复后在对应的位置出现发表回复的输入框和提交按钮
     $content='<form id="rform" name="rform" onSubmit="postReply();" action="javascript:void(null);" method="post" enctype="multipart/form-data"><textarea name="re" id="re" cols="60" rows="5" ></textarea><input name="id" id="id" type="hidden" value="’.$id.’" /><input name="rep" id="rep" type="submit" value="Reply" /></form>’;
     $objResponse->addAssign("reply_".$id,"innerHTML",$content);
         
return $objResponse;
}
/* 创建自定义函数addreply()*/
/**
* 回复留言的处理
*
* @access   public
* @param string    $$form    留言ID
* @return   void
*/
function addreply($form)
{
     global $db;
     $objResponse = new xajaxResponse();
     /* 获取表单传递的参数 */
     $re=xml_escape($form[‘re’]);
     $id=$form[‘id’];
     $content="<strong>Reply:</strong>".nl2br($re);
     $objResponse->addAssign("reply_".$id, "innerHTML", "");    
     $db->query("update aj_book set reply=’".$re."’ where id=".$id);
     $objResponse->addAssign("reply_".$id, "innerHTML", $content);
   
return $objResponse;
}
/* 创建自定义函数show()*/
/**
* 显示留言及分页功能
*
* @access   public
* @param string    $page    页码
* @return   void
*/
function show($page=1)
{
global $db;
     $page=intval($page);
     $objResponse = new xajaxResponse();
//每页显示的记录数
$per_page = 10;
     /* 获取留言总条数 */
$num = $db->getOne("select count(*) as nums from aj_book");
    
     if($page<1||$page>ceil($num/$per_page)) $page=1;//判断自定义输入跳转页面是否越界
$offset = ($page – 1) * $per_page;
     /* 没有留言时显示 */
if($num <1)
     {
       $objResponse->addAssign(‘content’, ‘innerHTML’, ‘No message!’);
       return $objResponse;
}
     $sql="select * from aj_book order by id desc limit ".$offset.",".$per_page;
     $rsr=$db->getAll($sql);
     if($rsr)
     {
         /* 循环读出分页留言 */
         foreach($rsr as $rs)
         {
             $scontent.='<div class="post" id="post_’.$rs[id].’">’.’     <h3>Nick Name: <a href="mailto:’.$rs[email].’" title="send mail">’.$rs[nickname].'</a> |   <a href="’.$rs[homepage].’">HomePage</a>   | Post at ‘.$rs[posttime];
             if(!empty($_SESSION[‘admin’]))
             {
                 $scontent.=’ | <a href="javascript:void(0)" onclick="if(confirm(‘Are you sure you want to delete the message?’)){xajax_del(‘.$rs[id].’)} return false;">Delete</a> | <a href="javascript:void(0)" onclick="xajax_reply(‘.$rs[id].’);">Reply</a>’;
             }
             $scontent.='</h3>’.’     <div class="entry">’.nl2br($rs[post]).'</div><div id="reply_’.$rs[id].’" class="reply">’;
             if(!empty($rs[reply]))
             {
                 $scontent.='<strong>Reply:</strong>’.nl2br($rs[reply]);
             }
             $scontent.='</div></div>’;
         }
         /* 赋值留言内容给content对象 */
         $objResponse->addAssign("content","innerHTML",$scontent);
     }
     else
     {
         $db->die();
     }
     /* 开始分页 */
     if($num)
{
      if($num % $per_page)
         {
               $pageCount = (int)($num / $per_page) + 1;
      }
         else
         {
             $pageCount = $num / $per_page;
         }
}
else
     {
      $pageCount = 0;
}
/* 初始化分页导航*/
     $nav = "";
$nextPage = $page + 1;
$prevPage = $page – 1;
     /* 总记录数与分页信息*/
$nav.='<a class="nolink" title="Total Records">’.$num.'</a><a class="nolink">’.$page.’/’.$pageCount.'</a>’;
     /* 首页与前页的判断显示 */
     if($page == 1)
{
      $nav .= ”;
    }
     else
     {
      $nav .= ‘<a href="javascript:void(0);" onclick="xajax_show(1);" title="First Page">|<</a>’;
      $nav .= ‘<a href="javascript:void(0);" onclick="xajax_show(‘.$prevPage.’);" title="Previous Page"><<</a>’;
     }
     /* 数字分页的显示 */
$plus=ceil($per_page/2);
if(($per_page-$plus+$page)>$pageCount)$plus=($per_page-$pageCount+$page);
$begin=$page-$plus+1;
$begin=($begin>=1)?$begin:1;
$nav .=”;
for($i=$begin;$i<$begin+$per_page;$i++)
{
         if($i<=$pageCount)
         {
             if($i!=$page)
             $nav.='<a href="javascript:void(0);" onclick="xajax_show(‘.$i.’);">’.$i.'</a>’;
             else
             $nav.='<a class="curr" title="Current Page">’.$i.'</a>’; //当前页
         }
         else
         {
             break;
         }
}
unset($begin);
     /* 后页与尾页的判断显示 */
if( ($page == $pageCount) || ($pageCount == 0) )
     {
      $nav .= ”;
}
     else
     {
      $nav .= ‘<a href="javascript:void(0);" onclick="xajax_show(‘.$nextPage.’);" title="Next Page">>></a>’;
      $nav .= ‘<a href="javascript:void(0);" onclick="xajax_show(‘.$pageCount.’);" title="Last Page">>|</a>’;
}
     /* 跳转页 */
     $nav .= ‘<input type="text" name="custompage" onKeyDown="if(event.keyCode==13) {xajax_show(this.value); return false;}">’;
     //$objResponse->addAlert($nav);
     /* 分页导航赋值 */
     $objResponse->addAssign("pagebar","innerHTML",$nav);
     return $objResponse;
}
/* 创建自定义函数del()*/
/**
* 删除留言功能
*
* @access   public
* @param string    $id    留言ID
* @return   void
*/
function del($id)
{
global $db;
     $objResponse = new xajaxResponse();
     $db->Execute("delete from aj_book where id=".$id);
     //此处也没有重载,需要的可以将下句替换为:$objResponse->addScript("xajax_show();");
     $objResponse->addRemove("post_".$id);
     
     return $objResponse;
}
$xajax->processRequests();
//根据登录状态显示登录或注销链接,页面刷新后有效
$logstr=$_SESSION[admin]=="" ? ‘<a href="javascript:void(0)" onclick="lAlert();">Manage</a>’   : ‘<a href="javascript:void(0)" onclick="xajax_logout();">Logout</a>’;
?>
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=GB2312" />
<meta name="Author" content="im502,im502(at)126.com" />
<meta name="keywords" content="php,mysql,ajax,xajax,adodb,smarty,js,javascript,guestbook,book,gbook,phpbook,im502" />
<meta name="description" content="a guest book power by php,mysql,xajax,adodb,smarty" />
<link rel="stylesheet" href="images/style.css" type="text/css" />
<title>XajaxBook by php+mysql+xajax+adodb </title>
<?php echo $xajax->getJavascript(‘./xajax/’); ?>
<script type="text/javascript">
//传递表单
function loginn(){
   xajax_adminlogin(xajax.getFormValues("login"));
   return false;
}
function post(){
   xajax_postmessage(xajax.getFormValues("postform"));
   return false;
}
function postReply(){
   xajax_addreply(xajax.getFormValues("rform"));
   return false;
}
</script>
</head>
<body onload="xajax_show()">
<!–加载需要弹出半透明渐变层的js–>
<script type="text/javascript" src="al.js"></script>
<div id="wrap">
<div id="header"></div>
<div id="sidebar">
<ul>
     <li>
         <h2>Navigation</h2>
             <ul>
                 <li><a title="Show message" href="javascript:void(0)" onclick="xajax_show();">Show Message</a></li>
                 <li><a href="javascript:void(0)" onclick="sAlert();" title="Post a message">Post Message</a></li>
                 <li id="log"><?php echo $logstr; ?></li>
             </ul>
     </li>
</ul>
</div>
<script type="text/javascript">
<!–
xajax.loadingFunction = function(){
xajax.$(‘cLoadingIndicator’).style.display=’block’;
};
xajax.doneLoadingFunction = function(){
xajax.$(‘cLoadingIndicator’).style.display = ‘none’;
}
// –>
</script> <div id="cLoadingIndicator" style="display:none">
             <span ><img src="images/load.gif"> loading…</span>
         </div>
     <div id="content"></div>
     <div id="pagebar"></div>
<div id="footer">
     <p>
         Copyright &copy; <a href="http://www.im505.com">im502.com</a> | XajaxBook By PHP+Mysql+Xajax+Adodb
     </p>
</div>
</div>
</body>
</html>
下载