tags:

views:

140

answers:

4

I am currently investigating a double request problem on my site. Not all the time, but sometimes, a requested page will in fact load twice...which is not a problem really until it is on a page with PHP that inserts stuff into my db on request (my tracking script).

I have read that an empty src in an image tag, and an empty url() in a css background could potentially cause the page to be requested twice.

However, I can't find any problems with those.

Is there anything else that could be causing something like this?

ANSWER FOR MY SITUATION

After some extensive research, it turns out that in my case specifically, the second request has been coming from the user agent "Mediapartner-Google". I began to notice that on pages that serve an Adsense ad, I could expect a secondary visit from this crawler within seconds after I visit the page myself.

This doesn't seem to be happening on pages without Adsense ads.

I am going to mark an answer below, because it seems like for most situations, those are the correct things to check.

+2  A: 

404's are a prime source for a request seemingly being requested twice. Check your CSS, JS and image sources are all correct.

Matt
@matt, so basically, even if the src and url etc...are all filled in...if something is incorrect, it could trigger a 404?also, its interesting that this doesn't seem to be happening on my local testing machine....just remote
johnnietheblack
@Byron: If you have a stylesheet whose `href` is `/an-outdated-stylesheet.css`, it will be instead loading the 404 handler for your site; whatever that maybe. Your development and production servers are of course configured slightly differently, so 404 handler may be different between machines.
Matt
+3  A: 

I have sat beside people whom I would swear knew better than this, and watch aghast as they double-clicked on every hyperlink in our app.

Didn't take long to figure out why they were experiencing double the page load time of everyone else...

Things like this certainly tend to give one pause when implementing pages that change the backend state. A lot of people put sequence numbers in hidden form elements so the backend can detect a double-submit.

Jason
I have a game where people kept paying double money or buying double at the supermarket. Then I realized they were double clicking... it just made me lose all hope in humanity. Telling your users "stop being stupid" doesn't go down well.
Coronatus
@jason, thats a good idea...how exactly does that work? whats the theory...have a random number submitted, and if that random number appears twice in a row, the load is a double?
johnnietheblack
It also keeps them from hitting the back button and submitting again (very critical if your application has issues detecting the Back Button. We have an OSS tool we use in house that I've beaten into everyone never to use the back button EVER when they're changing the settings. You end up making changes to the wrong data set nearly every time you do)
Jason
@jahnnietheblack: Basically you put a random number in, and you associate that number with the http session in your application. You can either track all previously submitted numbers, or track the in-flight ones, and return an error in the appropriate case for a hit/miss check.
Jason
Of course you can - and should - also reduce the number of back-submits by using the Redirect-on-POST mechanism, so that the browser history only ever has GET requests on it.
Jason
@Coronatus: At the end of the day, all engineering is supposed to be providing a service to humanity. Sometimes, part of that is protecting the humans from themselves (or each other). The trick is knowing when it's appropriate to intervene, and when your intervention becomes the source of harm.
Jason
+3  A: 

The causes I've seen before:

  • Missing stylesheet or image

  • Web developer addon for Chrome/Firefox sometimes requests things twice if you're validating HTML etc.

  • Browser inconsistency

Sometimes it's just too difficult to track down the root cause of a double request.

Either way, you should NOT be changing database state (or session state) through a GET request. The only SQL query you should be running without postdata is SELECT. All updates and inserts should be done using forms, even if the form consists only of a submit button.

Lotus Notes
@byron, cool ill check out those addons and try some different browsers and stuff to be sure. otherwise....if i need to track each pageview...how do i do it if i dont alter the db on load?
johnnietheblack
You can't avoid it, really. All you can do is attempt to ensure the uniqueness of visitors through their current login/session or ip address. This is assuming that you don't want to count page refreshes as an additional view.
Lotus Notes
+1 for not making database changes with GET requests.
Brian
A: 

We had a very strange behaviour in our CMS where an iframe in a jQuery dialog lightbox made a doubled database insert. After hours of debugging and loud WTFs we nailed it down. the dialog close method was setting the focus to the iframe of the dialog before destroying it and caused a reload of the iframe url!

pixeljunkie