Of course. Here is a comprehensive analysis and guide for guestbook.php in DedeCMS (织梦CMS).

This guide covers:
- What
guestbook.phpis and its purpose. - How to access and use the guestbook.
- A detailed breakdown of the PHP code.
- How to customize it (add CAPTCHA, change fields, etc.).
- Security considerations and best practices.
What is guestbook.php?
guestbook.php is the core file for DedeCMS's built-in guestbook (留言簿) module. It's a simple, self-contained system that allows visitors to leave messages, feedback, or comments on your website without needing to register.
When you install DedeCMS, the guestbook module is typically enabled by default. It usually resides in the root directory of your DedeCMS installation.
Key Features:

- Public Submission: Anyone can leave a message.
- Admin Approval: Messages are not published immediately by default. An administrator must log in to the backend and approve them first.
- Reply Functionality: Admins can reply to guestbook messages, and the reply is displayed below the original message.
- Simple Interface: It provides a basic form for users to input their name, contact info, and message.
How to Access and Use the Guestbook
For Visitors (Front-end):
- Navigate to
http://www.yourdomain.com/guestbook.phpin your browser. - You will see a list of already approved messages (if any).
- Scroll down to find the "Post a Message" (发表留言) form.
- Fill in your details (Name, Contact Info, Message, etc.).
- If enabled, you will need to solve a CAPTCHA (verification code).
- Click the "Submit" (提交) button.
- Your message is now in a "pending" state and will not be visible until an admin approves it.
For Administrators (Back-end):
- Log in to your DedeCMS admin panel:
http://www.yourdomain.com/dede/ - In the left-hand menu, find and click on "留言簿管理" (Guestbook Management).
- You will see a list of all messages, categorized by:
- 待审核留言 (Pending Messages): Messages waiting for approval.
- 已审核留言 (Approved Messages): Messages already visible on the front end.
- You can perform actions like:
- 审核 (Approve): Move a pending message to the approved list.
- 删除 (Delete): Permanently remove a message.
- 回复 (Reply): Add an official reply to a message.
- 编辑 (Edit): Modify the content of a message.
Code Breakdown of guestbook.php
Understanding the code is key to customization. The file is generally structured into three main parts: Displaying Messages, Processing New Submissions, and Displaying the Form.
<?php
require_once(dirname(__FILE__)."/config.php");
require_once(DEDEINC."/datalistcp.class.php");
require_once(DEDEINC."/guestbook.class.php");
// --- Part 1: Processing a new submission or reply ---
if(empty($dopost))
{
$dopost = '';
}
// If a new message is being submitted
if($dopost == 'send')
{
// 1. Instantiate the guestbook class
$guestbook = new Guestbook();
// 2. Assign form data to the object's properties
$guestbook->title = trim($title);
$guestbook->msg = trim($msg);
$guestbook->username = trim($username);
$guestbook->email = trim($email);
$guestbook->homepage = trim($homepage);
$guestbook->ip = GetIP(); // Get the user's IP address
$guestbook->typeid = $typeid; // Optional: for categorization
// 3. Save the message to the database
// The 'true' parameter means it's a new message (not a reply)
$result = $guestbook->SaveGuestbook(true);
// 4. Redirect based on the result
if($result == 'SUCCESS')
{
ShowMsg("发布成功,请等待管理员审核!", "guestbook.php");
exit();
}
else
{
ShowMsg("发布失败,".$result, "-1");
exit();
}
}
// If an admin is replying to a message
else if($dopost == 'reply')
{
// Similar process, but the 'false' parameter indicates it's a reply
$guestbook = new Guestbook();
$guestbook->id = $id;
$guestbook->msg = trim($msg);
$guestbook->replymsg = trim($replymsg); // The admin's reply content
$guestbook->SaveGuestbook(false); // Save as a reply
ShowMsg("回复成功!", "guestbook.php");
exit();
}
// --- Part 2: Displaying the list of messages ---
// Get the page number from the URL, default to 1
$PageNo = isset($PageNo) ? intval($PageNo) : 1;
// Instantiate the data list class
$dlist = new DataListCP();
// Set the query to fetch approved messages
$sql = "SELECT * FROM `#@__guestbook` WHERE ischeck=1 ORDER BY id DESC";
$dlist->SetTemplate(DEDETEMPLATE.'/guestbook.htm'); // Set the HTML template file
$dlist->SetSource($sql); // Set the data source
$dlist->display(); // Display the list
// Free up resources
$dlist->Close();
?>
Key Classes and Functions Used:

Guestbookclass: (/include/guestbook.class.php) This is the main workhorse. It handles all database interactions for saving and retrieving messages.SaveGuestbook($isnew = true): Saves a message.$isnew=truefor a new post,$isnew=falsefor an admin reply.
DataListCPclass: (/include/datalistcp.class.php) A powerful class for paginating and displaying data from a database query. It automatically handles pagination and uses a separate HTML template for the output.ShowMsg()function: A standard DedeCMS function to display a status message and then redirect the user.GetIP()function: A utility function to get the visitor's IP address, which is stored for security and moderation purposes.
How to Customize guestbook.php
Common customizations involve modifying the form, adding fields, or changing the display logic.
Customization A: Add a CAPTCHA (Verification Code)
The guestbook might not have a CAPTCHA enabled by default. To add one, you need to modify the template file.
-
Edit the Template: Open
templets/default/guestbook.htm. -
Add the CAPTCHA Code: Find the form submission button (
<button type="submit">...</button>). -
Insert the CAPTCHA code snippet just before the button:
<div class="form-group"> <label for="vdcode">验证码:</label> <div class="input-group"> <input type="text" name="validate" id="vdcode" class="form-control" style="width:150px;text-transform:uppercase;" /> <img src="{dede:global.cfg_cmspath/}/include/vdimgck.php" id="validateImg" style="cursor:pointer" onclick="this.src='{dede:global.cfg_cmspath/}/include/vdimgck.php?'+Math.random();" title="看不清?点击换一张" /> </div> </div> -
Modify
guestbook.php(Optional but Recommended): To actually validate the code, you would add a check in theif($dopost == 'send')block ofguestbook.phpbefore saving the message.
Customization B: Add a New Form Field (e.g., "Company")
Let's say you want to add a "Company" field.
-
Modify the Database:
- phpMyAdmin ->
#@__guestbooktable. - Add a new column:
ALTER TABLE#@__guestbookADDcompanyVARCHAR(100) NOT NULL DEFAULT '';
- phpMyAdmin ->
-
Modify the Template (
guestbook.htm):- Add the new input field in the form:
<div class="form-group"> <label for="company">公司名称:</label> <input type="text" id="company" name="company" class="form-control" /> </div>
- Add the new input field in the form:
-
Modify
guestbook.php:- In the
if($dopost == 'send')block, add the new field:// ... other assignments $guestbook->company = trim($company); // ... rest of the code
- In the
-
Modify the Display Logic:
- You'll also need to update the
guestbook_list.htm(which is usuallyguestbook.htmitself) to display the new company field when showing a message.
- You'll also need to update the
Customization C: Change the Number of Messages per Page
This is controlled by the DataListCP class in guestbook.php.
-
Edit
guestbook.php:- Find the line where you instantiate the
DataListCPclass. - Add a line to set the page size. For example, to show 10 messages per page:
// ... before $dlist = new DataListCP(); $pagesize = 10; // Set the number of items per page $dlist = new DataListCP(); $dlist->pageSize = $pagesize; // Set the page size // ...
- Find the line where you instantiate the
Security Considerations
The default guestbook is a common target for spammers.
- Always Use CAPTCHA: This is the most effective way to prevent automated spam bots from submitting junk messages. If it's not enabled, enable it as described in Customization A.
- Moderate All Messages: Never set
ischeck=1by default in the code. Always keep the default approval workflow (ischeck=0). This allows you to filter out spam and inappropriate content before it goes live. - Be Careful with HTML: By default, guestbook messages might allow some HTML tags. This can be dangerous (XSS attacks). The safest approach is to strip all HTML or convert it to plain text. You can modify the
Guestbookclass or add ahtmlspecialchars()call when displaying the message in the template. - Keep DedeCMS Updated: Like any CMS, vulnerabilities are found over time. Keep your DedeCMS installation and all its files up-to-date to patch security holes.
- Consider a Third-Party Solution: For high-traffic sites or sites where user interaction is critical, the default DedeCMS guestbook might be too basic. Consider more robust solutions like Disqus, a WordPress plugin with a bridge, or a dedicated customer interaction platform.
