我正在使用一个非常简单的方法,使用基本的PHP来开始使用我已经在工作的消息传递功能的自动回复系统。我知道我没有在这里使用最好的安全方法,但这不是什么意思,所以请避免评论脚本的安全性。
现在,当用户从名为reply
的表单发送回复时,它会决定用户是否正在使用user_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
并且您要回复自动服务,它将include
一个文件来添加您的消息并自动回复数据库:
系统/ 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')");
正如你所看到的那样,它只是一个非常基本的,只有一个测试才能让它工作,一旦这个工作应该是我可以做更多的工作,我得到的问题是试图添加用户回复并显示它通常,然后在添加AUTOMATED回复之前等待几秒钟。我试图使用sleep()
函数,但是当我点击发送添加我的回复时,只是延迟了整个页面,整个页面似乎冻结了3秒钟,然后用户和自动回复都飞到了屏幕同时。我试图首先添加用户回复,然后等待几秒钟,然后将自动回复添加到数据库。是否有其他功能而不是sleep()
我可以用来获得这些结果?
AS REQUESTED - 检索消息和显示对话的代码:
$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>
<?
}
?>
答案 0 :(得分:0)
正如评论中所建议的那样,我会使用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')");