PHP自动回复消息在添加回复到MySQL数据库之前需要暂停
我在这里使用了一个非常简单的方法,使用基本的PHP,开始使用我已经在使用的消息功能的自动回复系统。我知道我在这里没有使用最好的安全方法,但这不是本文的目的,所以请避免评论脚本的安全性 现在,当用户从名为PHP自动回复消息在添加回复到MySQL数据库之前需要暂停,php,html,mysql,automated-tests,messages,Php,Html,Mysql,Automated Tests,Messages,我在这里使用了一个非常简单的方法,使用基本的PHP,开始使用我已经在使用的消息功能的自动回复系统。我知道我在这里没有使用最好的安全方法,但这不是本文的目的,所以请避免评论脚本的安全性 现在,当用户从名为reply的表单发送回复时,它决定用户是否正在使用user\u id为0的自动回复功能。它是,$sarssystem返回为1。如果它没有以1的形式返回,那么表单将作为一般消息进行处理,这非常有效。以下是表格流程: ///////////// ADD REPLY TO CONVERSATION //
reply
的表单发送回复时,它决定用户是否正在使用user\u id
为0
的自动回复功能。它是,$sarssystem
返回为1
。如果它没有以1
的形式返回,那么表单将作为一般消息进行处理,这非常有效。以下是表格流程:
///////////// 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'");
}
}
//////////////////////////////////////////////////////////////////////
如果它返回为1
,并且您正在回复自动服务,它将包含一个文件,用于添加您的消息并自动回复到数据库中:
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')");
正如你所看到的,它主要是基本的,只是一个测试,让它工作,我可以做更多的工作,一旦这是工作,因为它应该是,我得到的问题是,试图添加用户回复和显示它正常,然后等待几秒钟,然后再添加自动回复。正如您所看到的,我尝试使用sleep()
函数,但当我点击send添加回复时,整个页面似乎冻结了3秒钟,然后用户和自动回复同时在屏幕上显示。我试图先添加用户回复,然后等待几秒钟,然后将自动回复添加到数据库中。我是否可以使用另一个函数而不是sleep()
来获得这些结果
根据请求-检索消息和显示对话的代码:
$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>
<?
}
?>
$conversation\u id=$convertaid;
$res4=mysqli\u查询($conn,“从ap\u对话中选择*,其中对话\u id='$conversation\u id');
while($row4=mysqli\u fetch\u数组($res4))
{
$co_conversation_id=$row4['conversation_id'];
$co_user_one=$row4['user_one'];
$co_user_two=$row4['user_two'];
如果($co_user_one==$user_id){
$co_recip=$co_user_two;
}else if($co_user_two==$user_id){
$co_recip=$co_user_one;
}
如果($co_recip==“0”){
$sarssystem=1;
}否则{
$sarssystem=0;
}
$res5=mysqli\u查询($conn,“从ap\u消息中选择*,其中对话\u id=“$conversation\u id”);
while($row5=mysqli\u fetch\u数组($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\u查询($conn,“SELECT*FROM-ap\u users,其中user\u id='$co\u recip');
while($row6=mysqli\u fetch\u数组($res6))
{
$co_first_name=$row6['first_name'];
$co_last_name=$row6['last_name'];
}
?>
新的
正如评论中所建议的,我将使用DATE\u ADD()
增加时间戳:
我可以问一下,为什么需要暂停?出于几个原因,如果不暂停,有时自动回复会跳到用户消息之前,它发生得有点太快,看起来很愚蠢,我认为这会让网站的用户感到困惑。我们可以看看你用来将记录从数据库拉到对话中的代码吗?我最初的反应是on是将自动创建的记录的时间戳修改为+5秒DATE\u ADD(现在(),间隔5秒)
,并根据发送的时间过滤另一端的记录。我已经编辑了我的问题以包含您要求的代码,这个答案仍然是建议的前进路线吗?@Snappysites我会尝试一下,看看它是否如您所愿。您是在页面重新加载时插入对话消息,还是通过一些AJAX/JavaScript调用,或者…?我已经对它进行了测试,它确实解决了自动消息跳转到用户消息上方的问题,尽管它仍然非常快。目前我只使用页面重新加载,但我希望将其升级到与Ajax一起使用,因为您必须重新加载页面才能查看任何正常用户的回复-总是令人沮丧!
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')");