· 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:
- Go to the SQL tab after selecting the
frnd_req_system
. - Paste the follwing sql code into the textarea and click on the Go button to excute this code.
- After executing the code, you will see that the tables are created and ready to use.
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
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:
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.