Each HTTP request is a new request to the server. It is only possible to "simulate" sessions thanks to the server-side languages that support this, as is the case with PHP. But this is only a request of the "current session" for PHP, the apache server interprets it as a new request and has no "clue" that that user was already "using" the services.
So two people using the site for two hours with the 1 second "refresh", for the server will be 14400 independent requests and there is nothing that can be done about this, except the following solutions:
- Increase the request time (5s would be good but depends on your need in case a chat is not feasible);
- Hire a host that has unlimited traffic. (Essential).
An interesting observation is that attacks by are quite similar to what they were doing, except that they see in a much larger amount of requests in a much smaller time span. That is, your own site was "attacking" and the reason comes next.
Another problem is the use of the function setInterval
that sends a new request before the previous one ends (in the case of asynchronous requests). Create a recursive function using the setTimeout
as already mentioned in the comments by Guilherme Nascimento.
AtualizaDados = function(){
var endereco = 'https://fiddle.jshell.net/';
var params = {param1: 1, param2: 2};
var time = 3000;
var el = $("#mensChat div._5chat");
$.ajax({
url: endereco,
type: 'GET',
dataType: 'html',
data: params,
success: function(html){
el.html(html);
},
error: function(x, s){
console.log(s, x);
},
complete: function(){
setTimeout(AtualizaDados, time);
}
});
};
AtualizaDados();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="mensChat">
<div class="_5chat"></div>
</div>
A good practice is also to decrease the amount of data to be trafficked, that is, remove the "garbage" from your requests, use JSON to traffic the data, so instead of downloading HTML content that would have for example 300 to 500 bytes (each character is a byte, including white spaces
) would only have 80 to 120 bytes (depending on the size of the message), and then mount the HTML on the client side, example:
Server:
<?php
/* Executa todas as ações necessárias */
header('Content-Type: application/json');
echo json_encode(Array(
'autor' => $autor,
'msg' => $msg,
'time' => $time,
'etc' => $etc
));
exit;
Client:
<script type="text/javascript">
AtualizaDados = function(){
var endereco = 'http://endereco/da/application/';
var params = {param1: 1, param2: 2};
var time = 3000;
var el = $("#mensChat div._5chat");
$.ajax({
url: endereco,
type: 'GET',
dataType: 'json',
data: params,
success: function(json){
el.append(
$('<div />').addClass('mensagem')
.append( $('<span />').addClass('autor').text( json.autor ) )
.append( $('<span />').addClass('msg').text( json.msg ) )
.append( $('<span />').addClass('hora').text( json.time ) );
);
},
error: function(x, s){
console.log(s, x);
},
complete: function(){
setTimeout(AtualizaDados, time);
}
});
};
AtualizaDados();