This article mainly introduced the PHP implementation of the message real-time push function, combined with examples of PHP based on the implementation of anti-AJAX push message real-time push front desk Ajax submission, background data processing and other related operations skills, the need for friends can refer to the next
In this paper, the real-time message push function of PHP implementation is described. Share to everyone for your reference, as follows:
Entry file index.html
<! DOCTYPE html>
Ajax processing Input write.php
<?php/** * Created by TXM. * TIME:2015/4/18 13:13 * Function: */$filename = dirname (__file__). ' /data.txt '; $isread _file = DirName (__file__). ' /isread.txt '; $user = DirName (__file__). ' /user.txt ';//write message, message unread, who sends message file_put_contents ($filename, $_get[' msg ']); File_put_contents ($isread _file, ' 0 '); File_put_contents ($user, $_get[' user '), Echo json_encode (Array (' SF ' =>true));
Long polling push ajaxpush.php
<?php/** * Created by TXM. * TIME:2015/4/18 13:12 * Function: */$filename = Dirna Me (__file__). ' /data.txt '; $isread _file = DirName (__file__). ' /isread.txt '; $userfile = DirName (__file__). ' /user.txt '; $get _user = $_get[' user '] = = ' 1 '? ' 2 ': ' 1 '; $msg = "; while (1) {$msg = file_get_contents ($filename); $isread = file_get_contents ($isread _file); $user = file_get_contents ($userfile); Is the message sent by the other party, setting the message read, exiting the loop. if ($isread = = ' 0 ' && $get _user = = $user) {file_put_contents ($isread _file, ' 1 '); Break } sleep (1);} echo json_encode (Array (' msg ' = $msg));