CodeIgniter Forums

Full Version: Retrieving Chat messages between two users with Codeigniter
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi
I implemented a chat system using Codeigniter where users can send and receive chat messages but the problem I'm having is that everyone can see everyone's chat.
How can I make the chat messages only accessible to the sender and receiver/recipient and vice versa?


[font=Arial,]Controller - Chat.php[/font]

Code:
public function ajax_get_chat_messages(){
$chat_id = $this->input->post('chat_id');
$recipient = $this->input->post('recipient');

if (!$recipient){
echo $this->_get_chat_messages($chat_id);
}
}

function _get_chat_messages($chat_id){

$last_chat_message_id = (int)$this->session->userdata('last_chat_message_id_' . $chat_id);

$chat_messages = $this->Chat_model->get_chat_messages($chat_id, $last_chat_message_id);

if ($chat_messages->num_rows() > 0)
{

//store last chat message id
$last_chat_message_id = $chat_messages->row($chat_messages->num_rows() - 1)->chat_message_id;
$this->session->set_userdata('last_chat_message_id_' . $chat_id, $last_chat_message_id);
$chat_messages_html = '<ul>';
foreach($chat_messages->result() as $chat_message)
{

$li_class = ($this->session->userdata('user_id') == $chat_message->user_id) ? 'class="by_current_user"' : '';

$chat_messages_html .='<li ' . $li_class. '>' . '<span class="chat_message_header">' . $chat_message->chat_message_timestamp . ' by ' . $chat_message->username . '</span><p class="message_content">' .$chat_message->chat_message_content . '</p></li>';
}

$chat_messages_html .='</ul>';



$result = array('status' =>'ok', 'content'=>$chat_messages_html);


return json_encode($result);

}
else
{
$result = array('status' =>'ok', 'content'=>'');
//print_r($result);

return json_encode($result);

exit();

}


}

[font=Arial,][b][font=helvetica, arial, sans-serif]Model - Chat_model.php[/font][/b][/font]


Code:
public function get_chat_messages($chat_id, $last_chat_message_id = 0){


$query_str = "SELECT cm.chat_message_id, cm.user_id, cm.chat_message_content, DATE_FORMAT(cm.date_created, '%D of %M %Y at %H:%i:%s') AS chat_message_timestamp, u.username FROM chat_messages cm JOIN users u ON cm.user_id = u.user_id WHERE cm.chat_id = ? and cm.chat_message_id > ? ORDER BY cm.chat_message_id ASC";

$result = $this->db->query($query_str, array($chat_id, $last_chat_message_id));

return $result;
}

[font=Arial,][b][font=helvetica, arial, sans-serif][b][font=helvetica, arial, sans-serif][font=helvetica, arial, sans-serif]js - chat.js[/font][/font][/b][/font][/b][/font]


Code:
function get_chat_messages()
{

$.post(base_url +"user/chat/ajax_get_chat_messages", { chat_id : chat_id}, function(data) {

if (data.status == 'ok')
{

var current_content = $("div#chat_viewport").html();


$("div#chat_viewport").html(current_content + data.content);

}
else
{
//there was an error do something

}

}, "json");

}

get_chat_messages();
Send the user id along with it and check it if that's not the user do not send it to them.

User 1 chats with User 4

User 4 can only see messages from User 1 and vise versa.

Sessions would be great for this.
(11-07-2017, 10:10 AM)InsiteFX Wrote: [ -> ]Send the user id along with it and check it if that's not the user do not send it to them.

User 1 chats with User 4

User 4 can only see messages from User 1 and vise versa.

Sessions would be great for this.

Hi InsiteFX,

Thanks for the feedback.

Would hat be in the where clause of my query?

The logic is what I imagined bu for some reason I cant really get i right.

Please see a screenshot of my table.

Thank you.
Your storing the chat_id and user_id so you can use those.

If chat_id is the user_id of the person that started the chat and then
the user_id is the one that they are chatting with then you can check those.

If it was me I would have a method called intiateChat() that would set this
all up for checking later.

Without seeing some of your code it is hard to see what you are trying to do.