0

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.

Don
  • 863
  • 1
  • 8
  • 22
xoi
  • 25
  • 7
  • comet server/node.js/etc there is quite a few techs that can be used to replace normal AJAX, for one a delayed AJAX which runs every say 5 mins instead of continously, also a properly configured server. – Sammaye Aug 08 '12 at 17:24
  • use jquery will save your time – timmalos Aug 08 '12 at 17:35
  • @timmalos not if the server is IP blocking – Sammaye Aug 08 '12 at 17:49
  • @Sammaye: Thanks for your response. Can I configure server using cpanel options or some .ini files? Or I have to contact my service provider? 5 mins would be my last option... – xoi Aug 08 '12 at 17:54
  • @timmalos: can jQuery help me solve this problem? I do intend to learn it someday. – xoi Aug 08 '12 at 17:54
  • @CườngNguyễnTấn You might need to call your service provider on this one, I couldn't walk you through cPanel on here – Sammaye Aug 08 '12 at 17:55
  • @CườngNguyễnTấn I would def go for the 5 min option and also contacting your service provider, both at the same time – Sammaye Aug 08 '12 at 17:56
  • @Sammaye: But I can modify cPanel settings to solve this, right? – xoi Aug 08 '12 at 17:57
  • @CườngNguyễnTấn I believe so, last time I used cPanel I could define firewall settings, but it depends on where they are blocking you, they might be blocking you from their infrastructure (no loopback) in which case no. – Sammaye Aug 08 '12 at 18:04
  • @Sammaye: Thanks. I guess I will contact the provider now. – xoi Aug 08 '12 at 18:08

3 Answers3

0

Probaly, but i seriously think that querying your server every second is totally innecesary and also a waste of resources (unless you have very compulsive customers), making your script to query your server every minute or so is better and may work even better in the long term if you have several customers using this application from the same server.

If you really think is necessary to have this feature, a good approach will be using Push notifications, more info can be found here: PHP - Push Notifications, here: Push notification to the client browser and here: Push notifications from server to user with PHP/JavaScript.

Community
  • 1
  • 1
Rafael
  • 2,827
  • 1
  • 16
  • 17
  • Also searching "push notifications php javascript" in StackExchange turns a lot of resources. – Rafael Aug 08 '12 at 17:51
0

Why don't you just update the cart when the user performs an action.

Such as 'Add item to shopping cart'?

That way you'd only call the server when it's actually needed.

diggersworld
  • 12,770
  • 24
  • 84
  • 119
  • thanks. It's just that I want to update the information in every opening tabs everytime an action is taken. to make it look cool, i guess... – xoi Aug 08 '12 at 18:16
  • In my opinion it's not really worth it. The user will probably expect alternative tabs to go out of sync as this is common behaviour on the majority of websites. You could still use a `setTimeout` but make it a few minutes or so if you really wanted. – diggersworld Aug 08 '12 at 18:19
  • ... one other thing to note is that setTimeout() does have a few issues when used in tabs that lose focus, as it generally stacks them up then fires them as quickly as possible when focus returns. – diggersworld Aug 08 '12 at 18:23
0

You have a couple of options here as I stated in my comment.

Basically the first is to chill out with the querying. You don't need to long poll such a thing. Turn down the querying to once every 5 mins or just whenever there is an action.

You can also build a simple comet server to do a push pull type thing when ever updates are "pushed" down from the server. There is a pre-built one called APE: http://www.ape-project.org/

Also node.js can handle this sort of thing for you.

Also you should probably look into your server setup, sounds kinda weird that your sever is blocking it's own IP address/domain...

Sammaye
  • 43,242
  • 7
  • 104
  • 146
  • thanks. It's just that I want to update the information in every opening tabs everytime an action is taken. and the server is a service that I paid for, not on my own computer. – xoi Aug 08 '12 at 18:15