I am making a chat room.And my code works but everybody can see what each other has chatted.I want to restrict this to be able to be seen by only the two people chatting.
This is my chat.php
<?php
include_once("chat.funct.php");
if(isset($_POST['send'])){
if(send_msg($_POST['sender'],$_POST['message'])){
echo "message sent...";
}
else{
echo ("message sending failed");
}
}
?>
<div id="messages">
<?php
$messages = get_msg();
foreach($messages as $value) {
echo '<strong>'.$value['sender'].' Sent</strong><br />';
echo $value['message'].'<br /><br />';
}
?>
<form method="post" >
<label>Enter Name:<input type="text" name="sender"/></label>
<label>Enter Message:<textarea name="message" rows="8" cols="70"></textarea></label>
<input type="submit" name="send" value="Send Message">
</form>
</div>
This is chat.funct.php
<?php
$connect=mysql_connect('localhost','root');
$a=mysql_select_db('cs2');
function get_msg(){
$query="SELECT * FROM chat1";
$run=mysql_query($query);
$messages=array();
while($message=mysql_fetch_assoc($run)){
$messages[]=array('sender'=>$message['sender'],'message'=>$message['message']);
}
return $messages;
}
function send_msg($sender,$message){
if(!empty($sender)&&!empty($message)){
$sender=mysql_real_escape_string($sender);
$message=mysql_real_escape_string($message);
$query="INSERT INTO chat1 (sender,message) VALUES('$sender','$message')";
$run=mysql_query($query);
if($run){
return true;
}
else{
return false;
}
}
else{
return false;
}
}
//Level 2 is the user of the site and level 3 is the admin.
In my chat1 table I have 3 columns sender, receiver and message. What I am trying to do is in the table find the places where sender==receiver
.
$query="SELECT * FROM chat1 WHERE sender='$sender' AND receiver='$receiver'";
But to write this query I want to check the the previous message sender's name which I can't get. Any help to write a simple chat to display the messages only to the two people involved please.I tried hard but it was no success
It would make the whole thing easier if you added ID's for messages. make a column called ID and use it to identify the message.
$query="SELECT * FROM chat1 WHERE sender='$sender' AND receiver='$receiver'"
This code will get all messages posted by the two users. but if you want to find out which message got posted when, then you will have to do this>
$query="SELECT * FROM chat1 WHERE sender='$sender' AND receiver='$receiver' ORDER BY id"
This will get message by message and order them by the ID. ID will be set to auto_incrediment in the table. if you want only the two of them to see the message, then you will have to do something like this:
if($_SESSION['current_user']=="$sender" OR $_SESSION['current_user']=="receiver")
{
//If the visitor is the sender or receriver
}
You could persist/add another column, the timestamp and do sorting on the messages based on that. This would give you one more feature for your app, time at which the message sent, also :)
Now you have to fire two queries, to collect all the messages.
$query="SELECT * FROM chat1 WHERE sender='$sender' AND receiver='$receiver'"; $query="SELECT * FROM chat1 WHERE sender='$receiver' AND receiver='$sender'";
If you need further clarifications, please do comment on this.