Hi, I have created real time script for my website. I wanted to ask u is it good or bad script? Here is my script:
My php page:servr.php
<?php
set_time_limit(0);
$con = mysqli_connect('localhost','root','') or die ("Could not connect to the Database");
mysqli_select_db($con,"msg") or die("Cannot find the database");
$last_id=null;
while(1) {
$last_ajax_call = isset($_GET['timestamp']) ? (int)$_GET['timestamp'] : null;
$date = new DateTime();
clearstatcache();
$last_change_in_data_file = $date->getTimestamp();
$last_change_in_data_file;
if ($last_ajax_call == null || $last_change_in_data_file > $last_ajax_call) {
$lastid=mysqli_query($con,"SELECT * FROM massg ORDER BY id DESC LIMIT 1") or die("no work");
while($row=mysqli_fetch_assoc($lastid))
{
$current_last_id=$row['id'];
}
if($current_last_id > $last_id && $current_last_id !=$last_id){
$msgs=mysqli_query($con, "SELECT * FROM massg WHERE id=$current_last_id") or die(mysqli_error($con));
while($row=mysqli_fetch_assoc($msgs)){
$messg=$row['messg'];
$reciver_id=$row['reciver'];
$sender_id=$row['sender'];
}
$result = array(
'data_from_file' => $messg,
'timestamp' => $last_change_in_data_file,
'msg_id'=>$current_last_id,
'reciver_id'=>$reciver_id,
'sender_id'=>$sender_id
);
$json = json_encode($result);
echo $json;
$last_id=$current_last_id;
}
//break;
} else {
sleep(1);
continue;
}
}
and this is test.php page
<div id="buffers"></div>
<script>
var timestamp=null;
var msg_lst_id = null;
var msg_crnt_id = null;
function waitForMsg(){
$.ajax({
type: "GET",
url: "servr.php",
async: true,
success: function(data){
var json=eval('('+data+ ')');
msg_crnt_id=json['msg_id'];
if(msg_crnt_id != msg_lst_id){
$("#windows").append(json['data_from_file']);
msg_lst_id =msg_crnt_id;
}
timestamp =json["timestamp"];
setTimeout("waitForMsg()",1500);
},
error: function(XMLHttpRequest,textStatus,errorThrown) {
setTimeout("waitForMsg()",15000);
}
});
}
waitForMsg();
</script>