Building a Peer-to-Peer Learning Platform with PHP

In the dynamic landscape of education, fostering collaboration and peer-to-peer learning is a powerful way to enhance the educational experience. In this article, we’ll explore the concept of a Peer-to-Peer Learning Platform built with PHP, allowing students to connect, share knowledge, and engage in collaborative learning.

The Concept

A Peer-to-Peer Learning Platform enables students to connect with their peers for mutual learning, tutoring, and collaborative study sessions. This platform provides a space for students to share their expertise, seek help in challenging subjects, and build a supportive learning community.

Setting Up the Project

Step 1: Environment Setup

Before diving into the PHP code, ensure you have a web development environment set up. You can use tools like XAMPP, WampServer, or MAMP to set up a local server environment.

Step 2: Database Configuration

Create a MySQL database to store user information, messages, and other relevant data. Use the following SQL script as a starting point:

CREATE DATABASE peer_to_peer_learning;
USE peer_to_peer_learning;

-- Table for user information
CREATE TABLE users (
    id INT AUTO_INCREMENT PRIMARY KEY,
    username VARCHAR(255) NOT NULL,
    password VARCHAR(255) NOT NULL,
    email VARCHAR(255) NOT NULL
);

-- Table for messages
CREATE TABLE messages (
    id INT AUTO_INCREMENT PRIMARY KEY,
    sender_id INT,
    receiver_id INT,
    message TEXT,
    timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (sender_id) REFERENCES users(id),
    FOREIGN KEY (receiver_id) REFERENCES users(id)
);

Step 3: PHP Code Implementation

User Registration and Login (register.php)

<?php
require_once('db.php');

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = $_POST['username'];
    $password = password_hash($_POST['password'], PASSWORD_BCRYPT);
    $email = $_POST['email'];

    $stmt = $conn->prepare('INSERT INTO users (username, password, email) VALUES (?, ?, ?)');
    $stmt->bind_param('sss', $username, $password, $email);

    if ($stmt->execute()) {
        echo 'Registration successful';
    } else {
        echo 'Registration failed';
    }

    $stmt->close();
}

$conn->close();
?>

User Login (login.php)

<?php
require_once('db.php');

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = $_POST['username'];
    $password = $_POST['password'];

    $stmt = $conn->prepare('SELECT id, password FROM users WHERE username = ?');
    $stmt->bind_param('s', $username);
    $stmt->execute();
    $stmt->bind_result($user_id, $hashed_password);
    $stmt->fetch();

    if (password_verify($password, $hashed_password)) {
        echo 'Login successful';
    } else {
        echo 'Login failed';
    }

    $stmt->close();
}

$conn->close();
?>

Sending and Receiving Messages (messages.php)

<?php
require_once('db.php');

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $sender_id = $_POST['sender_id'];
    $receiver_id = $_POST['receiver_id'];
    $message = $_POST['message'];

    $stmt = $conn->prepare('INSERT INTO messages (sender_id, receiver_id, message) VALUES (?, ?, ?)');
    $stmt->bind_param('iis', $sender_id, $receiver_id, $message);

    if ($stmt->execute()) {
        echo 'Message sent';
    } else {
        echo 'Failed to send message';
    }

    $stmt->close();
}

if ($_SERVER['REQUEST_METHOD'] === 'GET') {
    $user_id = $_GET['user_id'];

    $stmt = $conn->prepare('SELECT * FROM messages WHERE sender_id = ? OR receiver_id = ? ORDER BY timestamp ASC');
    $stmt->bind_param('ii', $user_id, $user_id);
    $stmt->execute();
    $result = $stmt->get_result();
    $messages = $result->fetch_all(MYSQLI_ASSOC);

    echo json_encode($messages);

    $stmt->close();
}

$conn->close();
?>

Building a Peer-to-Peer Learning Platform with PHP opens up possibilities for collaborative education. This project, though basic, serves as a foundation that can be expanded with additional features such as user profiles, discussion forums, and real-time messaging. As you explore this concept further, consider incorporating JavaScript for a more interactive and dynamic user experience. Happy coding!