views:

234

answers:

3

I'm implementing various PHP & ASP.NET web apps for a client.

I'd like to install a very low overhead user tracking system. (It has to be on the intranet, this is an internal system only).

I'd like to do something like the following:

line 1  normal code
line 2  normal code
line 3  Send Request/Query/Info to http://internal-IP/userTracker.php?Name=UserName&Page=...
line 4  Code that proceeds immediately after line 3 without waiting for a reply of any sort.

"Fire and Forget" seems to be the best analogy here.

Any tips?

Thanks!

+5  A: 

That's pretty much a textbook case for AJAX. Essentially, you fire off a rqeuest to that URL, and code execution continues as normal.

Here's an example in jQuery (assuming you want to post to the URL). Other libraries may vary. As will doing so without any js lirbaries.

 //some code
 $.get("http://internal-IP/userTracker.php?Name=UserName", function() {
    //put something here if you want to verify the request worked.
 });
 //some more code

The browser will fire off the request, and "some more code" will continue executing without blocking on the request.

thedz
I'm already using jquery for some things such as dialogs and calendar controls. Is this get() function built into the standard jquery javascript files? Thanks for the advice!
hamlin11
Yup. See: http://docs.jquery.com/Ajax
thedz
Thanks, much appreciated
hamlin11
+3  A: 

theory one: don't even bother doing the request with server code if you can avoid it. typically, people use invisible "tracking images" where it's just an <img> tag with src="http://internal-IP/userTracker.php?Name=UserName&amp;Page=...". this avoids security headaches trying to get your webserver communicating with the tracking server directly. it's also better than ajax because it works across any combination of domains. the problem with either tracking images or ajax is if you don't want your users hitting the tracking server directly and would rather just ping server-to-server...

theory two: avoid AJAX and IMGs and just go server-to-server like you originally suggest. this is probably the better suggestion. are you writing this in .NET and having it fire off to a PHP page? if so, the .NET code above will work great. even though you don't care about the response, i'd still wrap the hell out of it with exception handling.

that's all i've got. :) good luck!

ifatree
Thanks for the advice. I feel kinda dumb for not remember the IMG method. I know advertisers use the hell out of it for this specific purpose.
hamlin11
+2  A: 

For asp.net, you can use an asychronous web request and just have a dummy method that does nothing when the request comes back to you (maybe you want to check to make sure it returned status 200?).

Here's a C# example if you wanted to do this in the code behind, but the example posted by thedz would work for both ASP.NET and PHP pages.

void SendRequest(string uri)
{
    WebRequest request = HttpWebRequest.Create(uri);
    request.Method = "GET";
    request.BeginGetResponse(new AsyncCallback(ResponseReceived), null);
}
void ResponseReceived(IAsyncResult result)
{
    return;
}
Relster
Any advantages to this over jquery or IMG tags?
hamlin11
If the rest of your site is in ASP.NET and your doing additional work on the back end, this is easier to maintain and can lead to more control down the road. The disadvantage however is that the request will appear to be coming from your server rather than the client accessing your server, so really it all depends on what your fire-and-forget needs to do. If you're recording client information, use the IMG tag. If you think you'll ever need a response back, use the jquery. If you want to hide the pixel hit from your users, use the code behind.
Relster
This will be more reliable than any client side (AJAX/image-based) method. Sometimes, users will terminate navigation before your javascript can run, or your image can load (e.g., if they click a link, hit stop, close the browser), causing your statistics not to register for that request. If you send the request server side, you can be sure it will always be sent.
Frank Farmer
Good tip Frank, thanks.
hamlin11