facebook like messaging system with php

Facebook like messaging system with php




Hey guyz! In this tutorial i am going to teach you how to make a facebook like messaging system with php. Creating a facebook like messaging is not difficult So lets get started. First thing first Discalmer We will be using ajax for this real time facebook like messaging system but thats not idea because server send to many http connection, Instead we should use node.js or socket for this type of application. But by this tutorial you will get the understanding how the facebook like messaging system with php works.



Facebook like messaging system with php

MySql

First we have to make 1 database with name `fb_p_msg` and 3 table `user`, `conversation` and `message`.
facebook like messaging system with php

`user` table

`user` table will store all the user details like id,username,password,img and Id is unique
facebook like messaging system with php

`conversation` table

in `conversaton` we will be having 3 fields id,user_one and user_two. id is the converstion id which will be unique and user_one and user_two are the id of the users which are having conversation with each other
facebook like messaging system with php

`messages` table

in the `messages` table we will be storing messages. messages table will have 5 fields id,conversation_id,user_from,user_to,message. In `conversation_id` we will store the id of the conversation table, On `user_from` we will store the id of the user how send the message and on `user_to` we will store the id of the user who receive the message.`id` field is unique and on `message` field we store the message
[sql]
— Table structure for table `conversation`

CREATE TABLE IF NOT EXISTS `conversation` (
`id` int(11) NOT NULL,
`user_one` int(11) NOT NULL,
`user_two` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

— ——————————————————–


— Table structure for table `messages`

CREATE TABLE IF NOT EXISTS `messages` (
`id` int(11) NOT NULL,
`conversation_id` int(11) NOT NULL,
`user_from` int(11) NOT NULL,
`user_to` int(11) NOT NULL,
`message` text NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

— ——————————————————–


— Table structure for table `user`

CREATE TABLE IF NOT EXISTS `user` (
`id` int(11) NOT NULL,
`username` text NOT NULL,
`password` text NOT NULL,
`img` text NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=latin1;


— Dumping data for table `user`

INSERT INTO `user` (`id`, `username`, `password`, `img`) VALUES
(1, ‘husain’, ‘cc03e747a6afbbcbf8be7668acfebee5’, ‘img/dp-1.jpg’),
(2, ‘hunk’, ‘cc03e747a6afbbcbf8be7668acfebee5’, ‘img/dp-2.jpg’),
(3, ‘hackerkernel’, ‘cc03e747a6afbbcbf8be7668acfebee5’, ‘img/dp-3.jpg’),
(4, ‘murtaza’, ‘cc03e747a6afbbcbf8be7668acfebee5’, ‘img/dp-4.jpg’),
(5, ‘qut’, ‘cc03e747a6afbbcbf8be7668acfebee5’, ‘img/dp-5.jpg’),
(6, ‘sakina’, ‘cc03e747a6afbbcbf8be7668acfebee5’, ‘img/dp-6.jpg’);


— Indexes for dumped tables


— Indexes for table `conversation`

ALTER TABLE `conversation`
ADD PRIMARY KEY (`id`);


— Indexes for table `messages`

ALTER TABLE `messages`
ADD PRIMARY KEY (`id`);


— Indexes for table `user`

ALTER TABLE `user`
ADD PRIMARY KEY (`id`);


— AUTO_INCREMENT for dumped tables


— AUTO_INCREMENT for table `conversation`

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

— AUTO_INCREMENT for table `messages`

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

— AUTO_INCREMENT for table `user`

ALTER TABLE `user`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=7;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

[/sql]

File Structure

facebook like messaging system with php

connect.php – Connect to mysql database

We will connect to mysql database using mysqli
[php]

[/php]

index.php – login page

in index.php we are taking username and password from the user and if the username and password matches a entry in the `user` table we log the user in, store its id in the session and send him to message.php
[php]




Facebook Style Private Messaging System in php



“;
}
}
?>

Login


User & pass: husain / test123
User & pass: hunk / test123
User & pass: hackerkernel / test123
User & pass: murtaza / test123
User & pass: qut / test123
User & pass: sakina / test123




[/php]

message.php – messaging area

  1. First we will check user login status if he is login we will save $_SESSION['id'] into $user_id else send him to index.php
  2. On line 31 we will fetch all the users from the `user` table except us (The logged in user)
  3. On line 47 we will wait for the loggedin user to click on other users profile and when they click on the user we will store the id of that user in $user_two. Then we will try to fetch the conversation between then from `conversation` table, If the conversation has already started we will fetch its id and store it in $conversation_id else we will start a new conversation and then store its id in $conversation_id
  4. On line 79 we will store $conversation_id,$user_id, $user_two into a input hidden field in base64 encrypted format

[php]




Facebook Style Private Messaging System in php


Tutorial /
Download Script /
Want Me to Work on your Dream Project /

Welcome logout

  • {$row[‘username’]}
  • “;
    }
    ?>