Search code examples
phphtmlmysqlautomated-testsmessage

PHP Automated reply message needs to pause before adding reply to MySQL database


I am using a really really simple method here using basic PHP to get started on an automated reply system using my already working messaging feature. I know that I am not using the best methods of security here but that's not what this is about so please avoid commenting on the security of the script.

Now, when the user sends a reply from a form named reply it decides if the user is talking to the automated reply feature, with a user_id of 0. It it is, $sarssystem returns as 1. If it doesn't return as 1 the form will process as a general message, which works perfectly. Here is the form process:

///////////// ADD REPLY TO CONVERSATION //////////////////////////////
if(isset($_POST['reply'])){
    $user_id = $_SESSION['userid'];
    $message = $_POST['message'];
    $conversation_id = $_POST['conversation_id'];
    $sarssystem = $_POST['sarssystem'];

    if(isset($sarssystem)){
    if($sarssystem == 1){
        include 'system/sars_system.php';
    } else {

        $reply = str_replace("'","\\'",$message); 


mysqli_query($conn,"INSERT INTO ap_messages (message_id, message, sender_id, time_sent, time_read, conversation_id) 
VALUES ('','$reply','$user_id', NOW(), '', '$conversation_id')");   

    mysqli_query($conn, "UPDATE ap_conversations SET time = NOW() WHERE conversation_id = '$conversation_id'");

    }
    } else {

        $reply = str_replace("'","\\'",$message); 


mysqli_query($conn,"INSERT INTO ap_messages (message_id, message, sender_id, time_sent, time_read, conversation_id) 
VALUES ('','$reply','$user_id', NOW(), '', '$conversation_id')");   

    mysqli_query($conn, "UPDATE ap_conversations SET time = NOW() WHERE conversation_id = '$conversation_id'");

    }
}
//////////////////////////////////////////////////////////////////////

If it returns as 1 and you ARE replying to the automated service, it will include a file to add your message AND a automatic reply in to the database:

system/sars_system.php:

if($message == 'hello'){
    $sarsreply = 'hey, how are you?';
}

$usr_message = str_replace("'","\\'",$message);

mysqli_query($conn,"INSERT INTO ap_messages (message_id, message, sender_id, time_sent, time_read, conversation_id) 
VALUES ('','$usr_message','$user_id', NOW(), NOW(), '$conversation_id')");

mysqli_query($conn, "UPDATE ap_conversations SET time = NOW() WHERE conversation_id = '$conversation_id'");

sleep(3);

mysqli_query($conn,"INSERT INTO ap_messages (message_id, message, sender_id, time_sent, time_read, conversation_id) 
VALUES ('','$sarsreply','0', NOW(), '', '$conversation_id')");

As you can see it is MAJORLY basic, and only a test to get it working, I can do more work once this is working as it should be and the problem I am getting is trying to add the users reply and displaying it normally, then waiting for a couple seconds before adding the AUTOMATED reply. I tried to use the sleep() function as you can see but that just delays the entire page when I hit send to add my reply, the entire page seems to freeze for 3 seconds and then both user and automated replies fly on the screen at the same time. I am trying to add the user reply first, then wait a few seconds and then add the automated reply to the database. Is there another function rather than sleep() I can use to get these results?

AS REQUESTED - Code to retrieve messages and display conversation:

$conversation_id = $convoid;   
$res4=mysqli_query($conn, "SELECT * FROM ap_conversations WHERE conversation_id = '$conversation_id'");
while($row4=mysqli_fetch_array($res4))
{   
 $co_conversation_id = $row4['conversation_id'];
 $co_user_one = $row4['user_one'];
 $co_user_two = $row4['user_two'];
 if($co_user_one == $user_id){
    $co_recip = $co_user_two; 
 } else if($co_user_two == $user_id){
    $co_recip = $co_user_one; 
 }
 if($co_recip == '0'){
    $sarssystem = 1;
 } else {
     $sarssystem = 0;
 }
$res5=mysqli_query($conn, "SELECT * FROM ap_messages WHERE conversation_id = '$conversation_id'");
while($row5=mysqli_fetch_array($res5))
{   
 $co_message_id = $row5['message_id'];
 $co_message = $row5['message'];
 $co_sender_id = $row5['sender_id'];
 $co_time_read = $row5['time_read'];

}
$res6=mysqli_query($conn, "SELECT * FROM ap_users WHERE user_id = '$co_recip'");
while($row6=mysqli_fetch_array($res6))
{   
 $co_first_name = $row6['first_name'];
 $co_last_name = $row6['last_name'];
}



?>  
                <div class="col-xs-12 col-md-8">
                    <div class="panel panel-default">
                        <div class="panel-heading">
                            <div style="display:inline"><? echo ''.$co_first_name.' '.$co_last_name.''; ?></div> <div align="right" style="display:inline; float:right"><button type="button" class="btn btn-primary btn-sm" onclick="location.href='messages.php';"><span class="glyphicon glyphicon-plus-sign" aria-hidden="true"></span> New</button></div>
                        </div>
                        <div class="panel-body">
<?
}
?>                            
 <div class="list-group-message" style="overflow-y: scroll;height:385px;width:680px">                           
<?
$res6=mysqli_query($conn, "SELECT * FROM ap_messages WHERE conversation_id = '$conversation_id' ORDER BY time_sent ASC");
while($row6=mysqli_fetch_array($res6))
{   
 $me_message = $row6['message'];
 $me_message_id = $row6['message_id'];
 $me_sender_id = $row6['sender_id'];
 $todaysdate = date('d/m/Y');
 $me_time_sent_date = date('d/m/Y', strtotime($row6['time_sent']));
 $me_time_sent_date_and_time = date('d/m/Y H:i:s', strtotime($row6['time_sent']));
 $me_time_sent_time = date('H:i', strtotime($row6['time_sent']));
 if($todaysdate == $me_time_sent_date){
     $me_time = ''.$me_time_sent_time.'';
 } else {
    $me_time = ''.$me_time_sent_date.' '.$me_time_sent_time.''; 
 }


 $me_time_read = $row6['time_read'];
$res7=mysqli_query($conn, "SELECT * FROM ap_users WHERE user_id = '$me_sender_id'");
while($row7=mysqli_fetch_array($res7))
{   
 $me_first_name = $row7['first_name'];
 $me_last_name = $row7['last_name'];
  $me_display_img = $row7['display_img'];
}

mysqli_query($conn, "UPDATE ap_messages SET time_read = NOW() WHERE message_id = '{$me_message_id}' AND time_read = '0000-00-00 00:00:00' AND conversation_id = '$co_conversation_id' AND sender_id != '$user_id'");
?>  




<div class="media" style="max-width: <? echo $screenwidth; ?>px;">
  <div class="media-left">
    <a href="#">
      <img src="userimg/<? echo $me_display_img; ?>" alt="user" width="64px" height="64px" hspace="10px" class="media-object" align="left">
    </a>
  </div>
  <div class="media-body" style="position: relative !important;">
    <div style="display:inline"><b><a href=""><? echo ''.$me_first_name.' '.$me_last_name.''; ?></a></b></div> <div align="right" style="float:right; display:inline"> <? echo $me_time; ?> </div><br>
    <? echo $me_message; ?>
  </div>
</div>

<?
}
?>

Solution

  • As suggested in comments, I would increase the timestamp using DATE_ADD():

    if($message == 'hello'){
        $sarsreply = 'hey, how are you?';
    }
    
    $usr_message = str_replace("'","\\'",$message);
    
    mysqli_query($conn,"INSERT INTO ap_messages (message_id, message, sender_id, time_sent, time_read, conversation_id) 
    VALUES ('','$usr_message','$user_id', NOW(), NOW(), '$conversation_id')");
    
    mysqli_query($conn, "UPDATE ap_conversations SET time = NOW() WHERE conversation_id = '$conversation_id'");
    
    mysqli_query($conn,"INSERT INTO ap_messages (message_id, message, sender_id, time_sent, time_read, conversation_id) 
    VALUES ('','$sarsreply','0', DATE_ADD( NOW(), INTERVAL 5 SECOND), '', '$conversation_id')");