· PHP  · 3 min read

PHP Friend Request System

Here I will share with you a PHP Project which is Friend Request System like facebook.

  • Download this project from GitHub.

Features of this Friend Request System:

  • Login and Sign up.
  • A user can send, accept, ignore, and cancel a friend request.
  • Request Notification.
  • Unfriend a friend. Total number of friends.

Steps to Setup this Project

1. Create Database and its Tables

  • Database Name: frnd_req_system

After creating the database, use the following SQL to create its tables (users, friend_requests, friends).

If you are using phpMyAdmin:

  1. Go to the SQL tab after selecting the frnd_req_system.
  2. Paste the follwing sql code into the textarea and click on the Go button to excute this code.
  3. After executing the code, you will see that the tables are created and ready to use.

creating tables for frnd_req_system in phpmyadmin

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
START TRANSACTION;
SET time_zone = "+00:00";

CREATE TABLE `friends` (
  `user_one` int(11) NOT NULL,
  `user_two` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

CREATE TABLE `friend_requests` (
  `sender` int(11) NOT NULL,
  `receiver` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

CREATE TABLE `users` (
  `id` int(11) NOT NULL,
  `name` varchar(30) NOT NULL,
  `email` varchar(50) NOT NULL,
  `password` varchar(100) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

ALTER TABLE `friends`
  ADD KEY `user_one` (`user_one`),
  ADD KEY `user_two` (`user_two`);

ALTER TABLE `friend_requests`
  ADD KEY `sender` (`sender`),
  ADD KEY `receiver` (`receiver`);

ALTER TABLE `users`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `email` (`email`);

ALTER TABLE `users`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;

ALTER TABLE `friends`
  ADD CONSTRAINT `friends_ibfk_1` FOREIGN KEY (`user_one`) REFERENCES `users` (`id`) ON DELETE CASCADE,
  ADD CONSTRAINT `friends_ibfk_2` FOREIGN KEY (`user_two`) REFERENCES `users` (`id`) ON DELETE CASCADE;

ALTER TABLE `friend_requests`
  ADD CONSTRAINT `friend_requests_ibfk_1` FOREIGN KEY (`sender`) REFERENCES `users` (`id`) ON DELETE CASCADE,
  ADD CONSTRAINT `friend_requests_ibfk_2` FOREIGN KEY (`receiver`) REFERENCES `users` (`id`) ON DELETE CASCADE;
COMMIT;

Alternative database structure:

You can also do the following where you will have only one table to handle the requests –

  • users table:
    • id (Primary key)
    • name
    • Other user-related fields
  • friend_requests table:
    • id (Primary key)
    • sender_id (Foreign key referencing users table)
    • receiver_id (Foreign key referencing users table)
    • status (Pending/Accepted/Rejected)

2. Graphical Explanation of the Working of this System

Graphical Explanation of Working of php friend request system


3. Setup the Project Folder:

Go in the root directory of your localhost which is htdocs or www, and then create a folder called php-friend-request-system (you can give any name), this is our project folder.

htdocs/
└── php-friend-request-system/

Here is the structure of this folder:

PHP Friend request system folder structure

4. Download the Project:

Now download or clone the chandantudu/php-friend-request-system repo into the project folder or you can extract the downloaded zip file here.

5. Test the application:

http://localhost/php-friend-request-system/register.php
http://localhost/php-friend-request-system/login.php
  • The actions.php handles various friend-related actions, such as sending friend requests, accepting friend requests, canceling friend requests, ignoring friend requests, and unfriending users.
  • nav.php: A component that contains dynamic navigation links.
  • home.php: home page of the logged-in user.
  • friends.php: Friends list of the logged-in user.
  • profile.php: logged-in user can see other users’ profiles and send them friend requests.
  • notification.php: Notifications for friend-request.

Some Screenshots of this application:

login page


Profile page


Notifications page

Back to Blog

Related Posts

View All Posts »

Difference between bindParam and bindValue in PHP

In PHP, `bindParam` and `bindValue` are methods used with PDO to bind parameters to SQL statements. While they both serve to pass values to SQL queries, they differ in how they handle these values.

Higher-Order Functions in JavaScript

Higher-Order Functions in JavaScript

JavaScript higher-order functions let you treat functions like values—passing them as arguments, returning them, or storing them in variables.

Node.js url Module

Node.js url Module

The Node.js url module, which provides utilities for working with URLs (Uniform Resource Locators).