Current Location: Home> Latest Articles> How to Implement the In-site Messaging Function in a CMS System Using PHP

How to Implement the In-site Messaging Function in a CMS System Using PHP

M66 2025-06-13

How to Implement the In-site Messaging Function in a CMS System Using PHP

With the development of the internet, many websites and content management systems (CMS) have incorporated in-site messaging to enhance user interaction. This article will guide you on how to implement a simple in-site messaging function using PHP, and provide code examples to help developers quickly get started.

1. Database Design

Before implementing the in-site messaging feature, you first need to design the database table to store relevant message data. Assume that we have a database named "cms", which contains a "users" table. To support the messaging system, we need to create a new table for storing messages.

Here is the SQL query to create the messages table:

CREATE TABLE IF NOT EXISTS messages (
  id INT(11) AUTO_INCREMENT PRIMARY KEY,
  sender_id INT(11),
  receiver_id INT(11),
  subject VARCHAR(255),
  body TEXT,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  FOREIGN KEY (sender_id) REFERENCES users (id),
  FOREIGN KEY (receiver_id) REFERENCES users (id)
);

2. Fetching the User’s In-site Messages

Next, we need to create a page in the CMS system to display the user’s in-site messages. On this page, users can view their received messages and click to view the full message content.

Here’s a simple PHP function to fetch the list of messages:

function getInboxMessages($userId) {
  $query = "SELECT * FROM messages WHERE receiver_id = '$userId' ORDER BY created_at DESC";
  $result = mysqli_query($conn, $query);
  $messages = mysqli_fetch_all($result, MYSQLI_ASSOC);
  return $messages;
}

In this code, we query the database to fetch messages that the user has received, ordered by creation time in descending order.

3. Displaying the In-site Messages

On the page, we will loop through the messages and display each message’s title and sender’s information. Each message will also have a link, allowing the user to click and view the detailed message.

$inboxMessages = getInboxMessages($userId);

foreach ($inboxMessages as $message) {
  $senderId = $message['sender_id'];
  $subject = $message['subject'];

  // Query the sender's information
  $query = "SELECT * FROM users WHERE id = '$senderId'";
  $result = mysqli_query($conn, $query);
  $sender = mysqli_fetch_assoc($result);

  echo "<div>";
  echo "<p>Sender: " . $sender['username'] . "</p>";
  echo "<p>Subject: " . $subject . "</p>";
  echo "<a href='view_message.php?id=" . $message['id'] . "'>View</a>";
  echo "</div>";
}

4. Viewing the Message Details

When the user clicks the "View" link, the system will redirect to a new page to display the detailed content of the message. Below is the PHP code to query and display the detailed message:

$messageId = $_GET['id'];

// Query the details of the message
$query = "SELECT * FROM messages WHERE id = '$messageId'";
$result = mysqli_query($conn, $query);
$message = mysqli_fetch_assoc($result);

echo "<p>Sender: " . $message['sender_id'] . "</p>";
echo "<p>Subject: " . $message['subject'] . "</p>";
echo "<p>Content: " . $message['body'] . "</p>";
echo "<p>Time: " . $message['created_at'] . "</p>";

In this code, we fetch the message ID from the URL parameters ($_GET), then query the database to retrieve the full message content. Finally, we display the sender, subject, content, and timestamp on the page.

5. Conclusion

At this point, we have successfully implemented a basic in-site messaging function, allowing users to view their received messages and read the full content of each message. This feature is important for building interactive social sites or community-based websites.

Although this example is simple, in actual development, you might consider adding more features like the ability to send messages, filter out malicious content, and so on, to improve security and user experience.

We hope this article provides useful guidance and helps you implement in-site messaging functionality in your CMS system with PHP.