Message real-time push function based on anti-Ajax push and PHP implementation

Source: Internet
Author: User
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)); 
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.