PHP-based real-time chat system’s message burning and deletion after reading
With the rapid development of the Internet, the chat function has become a part of modern people’s daily life Indispensable part. In particular, the real-time chat system allows people to transmit messages instantly through the Internet and achieve rapid communication. However, in some scenarios, protecting user privacy and information security has become an urgent problem to be solved. This article will introduce how to use PHP to implement the functions of disappearing and deleting messages after reading in the real-time chat system.
The key to realizing this function is to set the message deletion time to a certain time limit. After this time limit is exceeded, the message will be automatically deleted, thereby ensuring the timeliness and security of the message. The following will introduce how to implement these two functions through code examples.
First, we need to create a database to store user chat messages. The following is a simple database structure:
CREATE TABLE messages ( id INT PRIMARY KEY AUTO_INCREMENT, sender_id INT, receiver_id INT, content TEXT, create_time DATETIME, delete_time DATETIME );
In a real-time chat system, when a user sends a message, we need to store the sender ID, receiver ID, message content, sending time and deletion time of the message . The sending time can be obtained through the PHP function date("Y-m-d H:i:s")
, while the deletion time needs to be set according to the requirements of burning after reading and deleting after reading.
The following is a simple sample code that implements the function of users sending messages and saving messages to the database:
<?php // 连接数据库 $conn = mysqli_connect("localhost", "username", "password", "database"); if (mysqli_connect_errno()) { echo "数据库连接失败:" . mysqli_connect_error(); die(); } // 获取发送者ID和接收者ID $senderId = $_POST['sender_id']; $receiverId = $_POST['receiver_id']; // 获取消息内容 $content = $_POST['content']; // 获取当前时间 $createTime = date("Y-m-d H:i:s"); // 设置消息的删除时间为30分钟后 $deleteTime = date("Y-m-d H:i:s", strtotime("+30 minutes")); // 插入消息到数据库 $sql = "INSERT INTO messages (sender_id, receiver_id, content, create_time, delete_time) VALUES ('$senderId', '$receiverId', '$content', '$createTime', '$deleteTime')"; mysqli_query($conn, $sql); // 关闭数据库连接 mysqli_close($conn);
In the above code, we obtain it through $_POST
The sender ID, receiver ID and message content in the message sent by the user. Then, get the current time through date("Y-m-d H:i:s")
, and set the deletion time to 30 minutes later through strtotime(" 30 minutes")
. Finally, the message is inserted into the database and the database connection is closed.
When the user views the message, we need to check whether the current time exceeds the message deletion time. If the deletion time is exceeded, we can delete the message from the database. The following is a simple sample code that implements the function of users viewing messages and deleting messages:
<?php // 连接数据库 $conn = mysqli_connect("localhost", "username", "password", "database"); if (mysqli_connect_errno()) { echo "数据库连接失败:" . mysqli_connect_error(); die(); } // 获取消息ID $messageId = $_GET['message_id']; // 获取当前时间 $currentTime = date("Y-m-d H:i:s"); // 查询消息的删除时间 $sql = "SELECT delete_time FROM messages WHERE id = $messageId"; $result = mysqli_query($conn, $sql); $row = mysqli_fetch_assoc($result); $deleteTime = $row['delete_time']; // 检查当前时间是否超过了删除时间 if ($currentTime > $deleteTime) { // 超过了删除时间,删除消息 $sql = "DELETE FROM messages WHERE id = $messageId"; mysqli_query($conn, $sql); } else { // 未超过删除时间,显示消息内容 $sql = "SELECT content FROM messages WHERE id = $messageId"; $result = mysqli_query($conn, $sql); $row = mysqli_fetch_assoc($result); echo $row['content']; } // 关闭数据库连接 mysqli_close($conn);
In the above code, we use $_GET
to obtain the message ID in the messages viewed by the user. Then, get the current time through date("Y-m-d H:i:s")
and query the deletion time of the message. Based on the comparison between the current time and the deletion time, we can decide whether to delete the message or display the message content.
Through the above code examples, we can implement the functions of the message disappearing and deleting after reading in the real-time chat system based on PHP. In this way, messages sent by users can be automatically deleted after a specified time, protecting the user's privacy and information security. Of course, we can also make appropriate optimization and improvements based on actual needs. Hope this article helps you!
The above is the detailed content of Real-time chat system based on PHP, messages disappear after reading and are deleted after reading. For more information, please follow other related articles on the PHP Chinese website!