为什么我的网站在代码只运行一次时插入两条记录!
简而言之,我正在尝试使用 mysql 为 php 站点的访问者创建一个自定义记录器。这样做时,我发现我为每个页面加载插入了两个条目。
我已经排除了代码意外运行两次的可能性。
我将查询放在每个页面上,假设它能完美地工作。在意识到它导致出现两个条目后,我感到很困惑,并从其他页面中删除了该查询,只将其保留在索引页面上。
利用 PHP 的 $_SERVER['HTTP_REFERER'] 和 $_SERVER['PHP_SELF'],我将这些值插入到日志中。查看我之前所在的位置以及当前所在的位置。
假设我在创建页面上,我单击了指向名为 index.php 的“主页”页面的链接。数据库中的条目会说引用者是“create.php”,当前页面是“index.php”。但是,将插入第二个条目,表示引用者为“index.php”,当前页面为“index.php”
我检查了单击页面链接时会发生什么没有记录器,所以我点击进入“创建”页面。我检查了数据库,发现还有另一个条目,但讽刺的是,这次只插入了一个条目。这很奇怪,因为创建页面没有用于记录访问者的代码,并且仅插入一个条目。然而,它说引用者是“create.php”,当前页面是index.php。这与应有的样子背道而驰。
经过更多研究,我决定观看直播标题。所以我使用 Firefox 和 IE 进行了检查,我意识到标头获取了我请求的页面,并且也始终获取了根页面。例如,对 create.php 的请求返回..
GET /create.php HTTP/1.1
获取/样式/ HTTP/1.1
GET / HTTP/1.1
我理解第一个 GET,对于我请求的页面,我理解第二个 GET 对于外部样式表。最后一个让我感到困惑,尽管它似乎证实了为什么我会得到重复的条目。
由于 create.php 页面上没有访客日志,因此日志中没有第一个条目,但是一旦加载根页面,就会为作为引用者的 create.php 创建条目,并且当前页面是 index.php页面,因为它正在加载根页面。
因为,index.php 页面上有一个访问者日志,当我从“create.php”转到“index.php”时,第一个 GET 创建一个“create.php”条目作为引用者,当前页面为“ index.php”,但第二个条目表示“index.php”是引用者和当前页面。
所以我很确定正是这个推理让我得到了第二个条目。
谁能告诉我为什么它在每个请求上加载根页面?
我在本地使用适用于 mac os x 的 xampp,并且我知道它可能是默认的图标。但是,我刚刚删除了该图标,即使图标不显示,它仍然会发生。
我可以提供代码示例和其他东西,此时它似乎无关紧要,因为插入查询有效,当我显然只执行一次时,它只发生两次。
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 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
“GET /”请求会自动拉取index.php。
页面上某处可能存在无效的图像 SRC url、脚本 SRC 或链接 HREF。这些可能会在后台对服务器发出看似常规的请求,而您却没有意识到。检查所有 HTML 的 URL 并确保它们都是有效的服务器端文件。还可能有服务器端将未知文件重定向回主页。
或者你有一些 JavaScript(可能是广告?)正在执行 ajax 请求。
我们需要完整的源代码才能真正追踪到这一点。
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.