Real-Time Message push function implemented by PHP [anti-ajax push] And phpajax
This example describes the Real-Time Message push function implemented by PHP. We will share this with you for your reference. The details are as follows:
Access 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 the message. The message is not read. The message file_put_contents ($ filename, $ _ GET ['msg ']) sent by the user; 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 = dirname (_ 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); // the message sent by the other party. Set the message to read and exit the loop. If ($ isread = '0' & $ get_user = $ user) {file_put_contents ($ isread_file, '1'); break;} sleep (1 );} echo json_encode (array ('msg '=> $ msg ));