This is a simple direct messaging application built with PHP and MySQL. It allows users to register, login, send messages to each other, and view their message history.
- User Registration: Users can register with a username and password.
- User Login: Secure login system using password hashing.
- Direct Messaging: Send messages to other registered users.
- Message History: View message history with other users.
- Password Hashing: User passwords are hashed using PHP's
password_hash()
function to protect against password theft. - Prepared Statements: All database queries use prepared statements (
PDO::prepare
) to prevent SQL injection attacks. - Session Management: Sessions are securely managed with
session_start()
and validated to ensure users are authenticated ($_SESSION['user_id']
). - Input Sanitization: User inputs are sanitized using
FILTER_SANITIZE_STRING
to prevent XSS attacks. - Rate Limiting: Implemented rate limiting on login attempts to mitigate brute force attacks.
- HTTPS: Ensure the application runs over HTTPS to encrypt data transmission.
- Navigate to the application URL.
- Register with a username and password.
- Login using your registered credentials.
- Start a new chat by entering the username of the recipient.
- Send and receive messages with other registered users.
// Example of sending a message in chat.php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// Sanitize and validate receiver and message inputs
$receiver = filter_var(trim($_POST['receiver']), FILTER_SANITIZE_STRING);
$message = filter_var(trim($_POST['message']), FILTER_SANITIZE_STRING);
$sender_id = $_SESSION['user_id'];
// Retrieve receiver's user ID from database
$stmt = $pdo->prepare("SELECT id FROM users WHERE username = :username");
$stmt->execute(['username' => $receiver]);
$receiver_id = $stmt->fetchColumn();
// If receiver exists, insert message into database
if ($receiver_id) {
$stmt = $pdo->prepare("INSERT INTO messages (sender_id, receiver_id, message) VALUES (:sender_id, :receiver_id, :message)");
$stmt->execute(['sender_id' => $sender_id, 'receiver_id' => $receiver_id, 'message' => $message]);
} else {
echo "User not found";
}
}
- PHP 7.0 or higher
- MySQL 5.6 or higher
- Web server (Apache, Nginx, etc.)
- Clone or download the repository.
- Import the
database.sql
file into your MySQL database. - Configure database credentials in
config.php
. - Ensure your web server (Apache or Nginx) is configured to serve PHP files.
-
Clone the repository:
git clone https://github.com/jadehamel/simplechat.git cd simplechat
-
Setup the Database:
- Create a new MySQL database and user.
- Import the provided SQL schema to create necessary tables.
CREATE DATABASE simple_messenger; USE simple_messenger; CREATE TABLE users ( id INT AUTO_INCREMENT PRIMARY KEY, username VARCHAR(50) NOT NULL UNIQUE, password VARCHAR(255) NOT NULL ); CREATE TABLE messages ( id INT AUTO_INCREMENT PRIMARY KEY, sender_id INT NOT NULL, receiver_id INT NOT NULL, message TEXT NOT NULL, timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP, FOREIGN KEY (sender_id) REFERENCES users(id), FOREIGN KEY (receiver_id) REFERENCES users(id) );
-
Configure the Database Connection:
- Open
config.php
and update the database connection settings to match your MySQL credentials.
<?php $host = 'localhost'; $db = 'simple_messenger'; $user = 'your_db_user'; $pass = 'your_db_password'; try { $pdo = new PDO("mysql:host=$host;dbname=$db", $user, $pass); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch (PDOException $e) { die("Could not connect to the database $db :" . $e->getMessage()); } ?>
- Open
-
Run the Application:
- Place the project files in your web server's root directory (e.g.,
htdocs
for XAMPP orwww
for WAMP). - Start your web server and navigate to the project URL (e.g.,
http://localhost/simplechat
).
- Place the project files in your web server's root directory (e.g.,
-
Register a new user:
- Go to
http://localhost/simplechat/register.php
- Enter a username and password to create a new account.
- Go to
-
Login:
- Go to
http://localhost/simplechat/login.php
- Enter your registered username and password to log in.
- Go to
-
Send a Message:
- After logging in, use the chat interface to send messages to other users by entering their username.
- This is a simple implementation for educational purposes. For a production application, ensure proper input validation, error handling, and security measures like HTTPS and session management.
- The UI is very basic; consider using CSS frameworks like Bootstrap for a better appearance.
- You might want to add features like real-time messaging with WebSockets for a more advanced messenger.
This project is licensed under the MIT License. See the LICENSE file for details.