Register Now


Lost Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Chatroom in PHP

Chatroom in PHP

A chatroom in PHP allows users to communicate with each other in real-time through a web-based interface. It typically consists of a client-side component, which is the web page that users interact with, and a server-side component, which handles the communication between the client and the database where the messages are stored.

Here’s a basic overview of how a chatroom script in PHP works:

  1. User connects to the chatroom website by entering the URL in their web browser.
  2. The PHP script on the server starts a new session or resumes an existing one and connects to the database where the messages are stored.
  3. Users can enter their username and a message, which are sent to the server via an HTTP request, typically by submitting a form.
  4. The PHP script on the server receives the data, it validates it and then stores the message along with the username in the database.
  5. The server then retrieves the latest messages from the database and sends them back to the client, to be displayed on the web page.
  6. The client-side component, which is usually Javascript, periodically sends a request to the server to check for new messages and updates the web page with any new messages.

It is worth noting that this is a very basic explanation of how a chatroom script in PHP works. To create a production-ready chatroom, you will likely need to consider other features such as user authentication, handling multiple chatrooms, pagination, security, and performance.

An online chat system would typically include the following elements:

  • Actors: Users and Administrators
  • Use Cases:
    • Register/Login
    • View/Send/Receive Messages
    • Create/Edit/Delete Groups
    • Add/Remove Friends
    • Search for Users/Groups
    • Logout
  • Associations:
    • Users can view and send messages, create and edit groups, add and remove friends and search for users/groups.
    • Administrators can perform all actions that users can, in addition to deleting groups and banning users.
    • Users and Administrators must register/login before they can perform any actions.

Here is an example of a simple PHP script that can be used to create a basic chatroom:


// Start a new session or resume an existing one

// Connect to the database
$host = "hostname";
$username = "username";
$password = "password";
$dbname = "dbname";
$conn = mysqli_connect($host, $username, $password, $dbname);

// Check if the form has been submitted
if (isset($_POST['submit'])) {
    // Get the username and message from the form
    $username = mysqli_real_escape_string($conn, $_POST['username']);
    $message = mysqli_real_escape_string($conn, $_POST['message']);

    // Insert the message into the database
    $query = "INSERT INTO chatroom (username, message) VALUES ('$username', '$message')";
    mysqli_query($conn, $query);

// Get the latest messages from the database
$query = "SELECT * FROM chatroom ORDER BY id DESC LIMIT 10";
$result = mysqli_query($conn, $query);


    <form method="post" action="">
        <label for="username">Username:</label>
        <input type="text" name="username" id="username">
        <label for="message">Message:</label>
        <input type="text" name="message" id="message">
        <input type="submit" name="submit" value="Submit">
    <div id="chatroom">
            while ($row = mysqli_fetch_array($result)) {
                echo $row['username'] . ": " . $row['message'] . "<br>";

This script first starts a new session or resumes an existing one. Next, it connects to a MySQL database using the provided hostname, username, password, and database name.

When the form is submitted, it retrieves the username and message from the form, escapes any special characters to prevent SQL injection and then stores the values in the database using an insert SQL query.

It then retrieves the latest 10 messages from the database and displays them on the page in the div element with an id of “chatroom”.

This script only covers the basic functionality of a chatroom, you may want to consider adding features like pagination, user authentication, and more.

It’s worth noting that this script is vulnerable to SQL Injection and XSS attack. You may want to validate user inputs, use prepared statements, and/or use a framework that handle security properly.

Leave a reply