开发者

why does my website insert two records when code is only run once!

Simply put, I am trying to create a custom logger for visitors to a php site with mysql. In doing so, I found that I was inserting two entries for every single page load.

I have already ruled out the code running twice by accident.

   I put the query on every page, assuming it would work flawlessly. After realizing it was causing two entries to occur, I was confused and removed the query from the other pages, leaving it only on the index page.

   Utilizing PHP's $_SERVER['HTTP_REFERER'] and $_SERVER['PHP_SELF'], I inserted the values into the log. To see where I was beforehand and where I was at currently.

   So let's say I was on the create page, and I clicked a link to the 'home' page named index.php. The entries in the database would say referer was 'create.php' and the current page was 'index.php'. However, a second entry would be inserted that said referer was 'index.php' and the current page was 'index.php'

   I checked what would happen when I clicked a link to a page without the logger, so I clicked to go to the 'create' page. I checked the database and there was another entry, but ironically only one entry was inserted this time. This was weird because the create page has no code to log a visitor and it was only inserting one entry. However, it was saying that the referer was 'create.php' and current page was index.php. Which is backwards from what it should be.

    Doing more research, I decided to look into watching the headers live. So I checked using firefox and IE, where I realized that the headers get the page I'm requesting and also always gets the root page as well. For example, a request to create.php returns..

GET /create.php HTTP/1.1

GET /style/ HTTP/1.1

GET / HTTP/1.1

    I understand the first GET, for the page I request, I understand the second GET for the external stylesheet. The last one is what confuses me, although it seems to corroborate why I'm getting the duplicate entry.

    Since, there is no visitor log on the create.php page there is no first entry to the log, but once it loads the root page, the entry is created for the create.php being referer and the current page is the index.php page because it is loading the root page.

    Since, there is a visitor log on the index.php page, when I go from 'create.php' to 'index.php', the first GET creates an entry of 'create.php' being the referer and current page being 'index.php', but a second entry says that 'index.php' is the referer and the current page.

So I am pretty certain it is this reasoning for which I get a second entry.

Can anyone tell me why it is loading the root page on every request?

I'm using xampp for mac os x locally, and I开发者_如何学Go understand it might be the default favicon. However, I just removed the icon and it still occurs, even though the favicon doesn't display.

I can supply code samples and other things, it just seems irrelevant at this point because the insert query works, it just occurs twice when I clearly only execute it once.


The "GET /" request is pulling index.php automatically.

There is probably an invalid image SRC url, or script SRC, or link HREF, somewhere on the page. Those can cause regular-looking requests against the server in the background without realizing it. Review all your HTML for URLs and ensure they're all valid server-side files. There could also be a server-side redirect for unknown files back to the home page.

That or you have some JavaScript (potentially ads?) that's doing an ajax request.

We'll need full source to really trace this down.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜