7 Replies - 855 Views - Last Post: 15 September 2013 - 04:46 AM Rate Topic: -----

#1 mutago234  Icon User is online

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 95
  • Joined: 08-September 13

Peer to peer chat application partially working

Posted 14 September 2013 - 06:15 AM

For past days now am crazily insane. I tried buliding peer to peer chat application from scratch. it works excellent but have just one problem

user1 sends a chat text to user2 and both will reply each other. The problem is that user1 and user2 cannot see or query the text they sent themselves.

Example

user1 sent orange to user2 but user1 could not see the orange text sent to user2 though the text has been sent and vice versa.

I think the problem is from the query of refresh.php

REFRESH.PHP


<?php
    require_once('auth.php');
?>
<?php

//if (isset($_GET['id']))
    //{

$recp=strip_tags($_SESSION['SESS_MEMBER_ID']);
//$send=strip_tags($_GET['id']);

$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("timeline", $con);

//$result = mysql_query("SELECT * FROM chat1 where recipient = '$recp' and sender ='$recp' ORDER BY id DESC");
$result = mysql_query("SELECT * FROM chat1 where recipient = '$recp'  ORDER BY id DESC");


while($row = mysql_fetch_array($result))
  {
  echo '<p>'.'<span>'.$row['sender'].'</span>'. '&nbsp;&nbsp;' . $row['message'].'</p>';
  }


mysql_close($con);
//}
?>





<?php
    require_once('auth.php');
?>

<?php
//session_start();

if(isset($_POST['submit']))
{
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("timeline", $con);
        
$message=strip_tags($_POST['message']);
$sender=strip_tags($_SESSION['SESS_FIRST_NAME']);
$reciever=strip_tags($_POST['reciever']);
$status=strip_tags('unread');


mysql_query("INSERT INTO chat1(message, sender,recipient,status)VALUES
('$message', '$sender', '$reciever', '$status')");


        

}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Simple Chat</title>
<script language="javascript" src="jquery-1.2.7.min.js"></script>
<script language="javascript" src="jquery.timers-1.0.0.js"></script>
<script type="text/javascript">

$(document).ready(function(){
   var j = jQuery.noConflict();
    j(document).ready(function()
    {
        j(".refresh").everyTime(1000,function(i){
            j.ajax({
              url: "refresh.php",
              cache: false,
              success: function(html){
                j(".refresh").html(html);
              }
            })
        })
        
    });
    j(document).ready(function() {
            j('#post_button').click(function() {
                $text = $('#post_text').val();
                                $text1 = $('#post_text1').val();
                j.ajax({
                    type: "POST",
                    cache: false,
                    url: "save.php",
                    data: "text="+$text & "text1="+$text1,
                    success: function(data) {
                        alert('data has been stored to database');
                    }
                });
            });
        });
   j('.refresh').css({color:"green"});
});

</head>
<body>
<form method="POST" name="" action="">
<input name="sender" type="text" id="texta" value="Welcome <?php echo htmlentities($_SESSION['SESS_FIRST_NAME'], ENT_QUOTES, "UTF-8");?>"/>
<div class="refresh">
<?php
               require('./data6rst.php');



$commentid = mysql_real_escape_string($_GET['id']);
$commentid=htmlspecialchars($_GET['id'], ENT_QUOTES);
$commentid = preg_replace('/[^a-zA-Z0-9-_\.]/','', $_GET['id']);

$result3 = $db->prepare('
            SELECT * FROM members
            WHERE id = :id
');
        
$result3->execute(array(
            ':id' => $commentid ));    

                 while ($row3 = $result3->fetch())
                       {
echo ' Send Messages to<br><b>'.htmlentities($row3['fname'], ENT_QUOTES, "UTF-8").' '.htmlentities($row3['lname'], ENT_QUOTES, "UTF-8").'</b>';                                  
                                  }
                ?>









<?php

$commentid=strip_tags($_GET['id']);
$commentid=mysql_real_escape_string($_GET['id']);
$commentid=htmlspecialchars($_GET['id'], ENT_QUOTES);


$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("Timeline", $con);
$result = mysql_query("SELECT * FROM chat1 where recipient = '$commentid' ORDER BY id DESC");


while($row = mysql_fetch_array($result))
  {
  echo '<p>'.'<span>'.$row['sender'].'</span>'. '&nbsp;&nbsp;' . $row['message'].'</p>';

  }

mysql_close($con);
?>

</div>

<input name="message" type="text" id="textb"/>
<input name="reciever" type="hidden" value="<?php echo htmlentities($_GET['id'], ENT_QUOTES, "UTF-8");?>">

<input name="submit" type="submit" value="Chat" id="post_button" />
</form>
</body>
</html>




Is This A Good Question/Topic? 0
  • +

Replies To: Peer to peer chat application partially working

#2 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3397
  • View blog
  • Posts: 9,604
  • Joined: 08-June 10

Re: Peer to peer chat application partially working

Posted 14 September 2013 - 06:27 AM

Quote

For past days now am crazily insane. I tried buliding peer to peer chat application from scratch.

definitely insane. I would use Javascript for a chat (Node.js + Socket.io).

nevertheless, even using PHP I’d try to get WebSockets running.

besides that, I wouldn’t do a refresh. it would suffice to append the text received from the server (and of course you can append what you just wrote without the server).

if you still insist on a full refresh, then you have to change your query (btw. you’re using the deprecated mysql library (see below) and are wide open to SQL injection (see Prepared Statements below)). you have to look for text that each user is either sender or recipient.

This post has been edited by Dormilich: 14 September 2013 - 06:28 AM

Was This Post Helpful? 0
  • +
  • -

#3 mutago234  Icon User is online

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 95
  • Joined: 08-September 13

Re: Peer to peer chat application partially working

Posted 14 September 2013 - 07:12 AM

Thank you and As for sql injection I will still
move to PDO but am just testing it. my problem is the query of refresh.php. can you emphasize on what am doing wrong

thanks
Was This Post Helpful? 0
  • +
  • -

#4 snoj  Icon User is offline

  • Married Life
  • member icon

Reputation: 84
  • View blog
  • Posts: 3,564
  • Joined: 31-March 03

Re: Peer to peer chat application partially working

Posted 14 September 2013 - 12:45 PM

You need to add another piece to your query's where clause that flips the sender and receiver.
Was This Post Helpful? 0
  • +
  • -

#5 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3397
  • View blog
  • Posts: 9,604
  • Joined: 08-June 10

Re: Peer to peer chat application partially working

Posted 14 September 2013 - 03:54 PM

Quote

my problem is the query of refresh.php.

IMHO, your query is the least of your problems, it’s the somewhat quirky application design.
Was This Post Helpful? 0
  • +
  • -

#6 mutago234  Icon User is online

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 95
  • Joined: 08-September 13

Re: Peer to peer chat application partially working

Posted 15 September 2013 - 04:06 AM

You are right Dormilich. I tried redesigning the application but it says



Notice: Undefined index: id in C:\xampp\htdocs\copytimeline\timeline\refresh.php on line 12

but when i added this within ajax in index.php file as per like this
url: "save.php?id=<?php echo htmlentities($id, ENT_QUOTES, "UTF-8");?>",

instead of just save.php


i was able to get the text i sent but i cannot get the text sent by reciever unless i refreshed the page then i will see it and is not supposed to be like that. I think the problem is based on the errorneous id within ajax.Just assume that i loged in using sessions.

below is the new code design. please just dont mind mysql deprecated function as i will still move it to PDO when am done.


refresh.php


<?php
	require_once('auth.php');
?>
<?php

//if (isset($_GET['id']))
	//{


$user=$_SESSION['SESS_FIRST_NAME'];
$userid=$_GET['id'];
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("timeline", $con);


$result = mysql_query("SELECT * FROM chat1 WHERE (recipient = '$user') AND ( sender = '$userid') ORDER BY id DESC");

//$result = mysql_query("SELECT * FROM chat1 WHERE (recipient = '$user' OR sender = '$user') AND (recipient = '$username' OR sender = '$username') ORDER BY id DESC");



while($row = mysql_fetch_array($result))
  {
  echo '<p>'.'<span>'.$row['recipient'].'</span>'. '&nbsp;&nbsp;' . $row['message'].'</p>';
  }


mysql_close($con);
//}
?>






index.php
<?php
	require_once('auth.php');
?>

<?php 
//session_start();

if(isset($_POST['submit']))
{
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("timeline", $con);
		
$message=$_POST['message'];
$reciever=$_SESSION['SESS_FIRST_NAME'];
$sender=$_POST['sender'];
$status='unread';


mysql_query("INSERT INTO chat1(message, sender,recipient,status)VALUES
('$message', '$sender', '$reciever', '$status')");


		//mysql_query("INSERT INTO chat1(message, sender,recipient)VALUES
//('$message', '$sender', '$reciever')");



}
?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Simple Chat</title>
<script language="javascript" src="jquery-1.2.7.min.js"></script>
<script language="javascript" src="jquery.timers-1.0.0.js"></script>
<script type="text/javascript">

$(document).ready(function(){
   var j = jQuery.noConflict();
	j(document).ready(function()
	{
		j(".refresh").everyTime(1000,function(i){
			j.ajax({
			  url: "refresh.php",
                          
			  cache: false,
			  success: function(html){
				j(".refresh").html(html);
			  }
			})
		})
		
	});
	j(document).ready(function() {
			j('#post_button').click(function() {
				$text = $('#post_text').val();
                                $text1 = $('#post_text1').val();
				j.ajax({
					type: "POST",
					cache: false,
					url: "save.php",
                                        
					data: "text="+$text & "text1="+$text1,
					success: function(data) {
						alert('data has been stored to database');
                                         
					}
				});
			});
		});
   j('.refresh').css({color:"green"});
});
</script>






</head>
<body>
<form method="POST" name="" action="">
<input name="sender" type="text" id="texta" value="Welcome <?php echo htmlentities($_SESSION['SESS_FIRST_NAME'], ENT_QUOTES, "UTF-8");?>"/>
<div class="refresh">




$recp=strip_tags($_SESSION['SESS_MEMBER_ID']);

$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("Timeline", $con);
//$result = mysql_query("SELECT * FROM chat1 where recipient = '$recp' ORDER BY id DESC");
$result = mysql_query("SELECT * FROM chat1 ORDER BY id DESC");


while($row = mysql_fetch_array($result))
  {
  echo '<p>'.'<span>'.$row['sender'].'</span>'. '&nbsp;&nbsp;' . $row['message'].'</p>';

  }

mysql_close($con);
?>

</div>

<input name="message" type="text" id="textb"/>
<input name="sender" type="hidden" value="<?php echo htmlentities($_GET['id'], ENT_QUOTES, "UTF-8");?>">

<input name="submit" type="submit" value="Chat" id="post_button" />
</form>
</body>
</html>


Was This Post Helpful? 0
  • +
  • -

#7 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3397
  • View blog
  • Posts: 9,604
  • Joined: 08-June 10

Re: Peer to peer chat application partially working

Posted 15 September 2013 - 04:15 AM

Quote

i was able to get the text i sent but i cannot get the text sent by reciever

cf.

Dormilich said:

you have to look for text that each user is either sender or recipient.

and

snoj said:

You need to add another piece to your query's where clause that flips the sender and receiver.



and again, I wouldn’t fetch all messages, only the new ones.

and besides that, I wouldn’t use the auto_increment id either but the timestamp when the message was inserted in the DB

Quote

Notice: Undefined index: id in C:\xampp\htdocs\copytimeline\timeline\refresh.php on line 12

happens when you assume $_GET['id'] to be there.
Was This Post Helpful? 0
  • +
  • -

#8 mutago234  Icon User is online

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 95
  • Joined: 08-September 13

Re: Peer to peer chat application partially working

Posted 15 September 2013 - 04:46 AM

if i use it this was
url: "save.php?id=<?php echo htmlentities($_GET['id'], ENT_QUOTES, "UTF-8");?>",

it still print the same error warnings above.

do you want me to send you the entire application and the tables
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1