I'm using an Ajax function with a call to itself to update the information continuously. But I let the script run for a while and then the server blocked my IP because it thought I was flooding it or something like that, I don't know. Anyway, I wonder if there's another way to do this more properly. Here's my code:
Ajax function:
function update_cart()
{
if (window.XMLHttpRequest)
var http = new XMLHttpRequest();
else
var http = new ActiveXObject('Microsoft.XMLHTTP');
http.onreadystatechange=function()
{
if ((http.readyState == 4) && (http.status == 200))
{
id('cart_quantity').innerHTML = parseInt(http.responseText);
setTimeout('update_cart()', 1000);
}
}
http.open('GET', actual_path+'fetch_cart_quantity.php', true);
http.setRequestHeader("X-Requested-With", "XMLHttpRequest");
http.send();
}
PHP script:
<?php
if($_SERVER['HTTP_X_REQUESTED_WITH'] != 'XMLHttpRequest')
{
header('Location: ./');
exit();
}
session_start();
include '../include/config.php';
include '../include/db_handler.php';
include '../include/cart_handler.php';
$cart = get_cart_quantity($_SESSION['cart_id']);
if ($cart == NULL) $cart = 0;
echo $cart;
?>
Thanks in advance for your help. Sorry that my English is not very good.