如何记录使用 2 个不同域对我的网站的每次访问

发布于 2024-07-25 20:26:01 字数 1279 浏览 4 评论 0原文

我有两个不同的域,它们都指向同一服务器中的我的主页。

我想记录对我的主页进行的每一次访问,并记录用户访问我的主页所使用的域,我该怎么做?

我在 Apache 中尝试了 mod_rewrite 并使用 PHP 登录到 MySQL 数据库,但我所能做的就是无限循环。

有任何想法吗?

编辑:
根据您的回答,我发现您没有得到我想要的...

据我所知,如果它们都指向同一网站,Google Analytics 不允许我区分正在使用的域,并且它也不允许我区分正在使用的域看到一些文件(例如图像)是直接访问的,而不是通过我的网页访问的。

我也不能只使用 $_SERVER['HTTP_HOST'] 因为就像我刚才所说的那样,我想记录所有内容,例如图像和所有其他文件,每个请求,即使它不存在。

至于Webalizer,我从未见过它区分域,它总是假设帐户中配置的默认域并将其用作root,它甚至不显示它。 我必须再次检查它,但我不确定它会执行我想要的操作...

无限循环:
我尝试的方法涉及使用指向 PHP 脚本的简单重写规则重写 Apche 中的 url,PHP 脚本会将条目记录到 MySQL 数据库中,并使用 header() 函数将用户发送回文件。 像这样的东西:

.htaccess:

RewriteCond %{HTTP_HOST} ^(www\.)?domain1\.net [NC]
RewriteRule ^(.*)$ http://www.domain1.net/logscript?a=$1 [NC,L]

RewriteCond %{HTTP_HOST} ^(www\.)?domain2\.net [NC]
RewriteRule ^(.*)$ http://www.domain2.net/logscript?a=$1 [NC,L]

PHP 脚本:

$url = $_GET['a'];
$domain = $_SERVER['HTTP_HOST'];

// Code to log the entry into the MySQL database

header("Location: http://$domain/$url");
exit();

因此,我访问一些文件,将该文件指向 PHP 脚本,该脚本将记录并重定向到该文件。但是,当 PHP 重定向到该文件时,htaccess 规则将拾取该文件并再次重定向 PHP 脚本,从而创建无限循环。

I have two different domains that both point to my homepage in the same server.

I want to log every single access made to my homepage and log which domain the user used to access my homepage, how can I do this?

I tried mod_rewrite in Apache and logging to a MySQL database with PHP but all I could do was infinite loops.

Any ideas?

EDIT:
By your answers, I see you didn't get what I want...

As far as I know Google Analytics does not allow me to differentiate the domain being used if they both point to the same site and it also does not allow me to see that some files like images were accessed directly instead of through my webpages.

I can't also just use $_SERVER['HTTP_HOST'] cause like I just said, I want to log EVERYTHING, like images and all other files, every single request, even if it doesn't exist.

As for Webalizer, I never saw it differentiate between domains, it always assumes the default domain configure in the account and use that as root, it doesn't even display it. I'll have to check it again, but I'm not sure it will do what I want...

INFINITE LOOP:
The approach I tried involved rewriting the urls in Apche with a simple Rewrite rule pointing to a PHP script, the PHP script would log the entry into a MySQL database and the send the user back to the file with the header() function. Something like this:

.htaccess:

RewriteCond %{HTTP_HOST} ^(www\.)?domain1\.net [NC]
RewriteRule ^(.*)$ http://www.domain1.net/logscript?a=$1 [NC,L]

RewriteCond %{HTTP_HOST} ^(www\.)?domain2\.net [NC]
RewriteRule ^(.*)$ http://www.domain2.net/logscript?a=$1 [NC,L]

PHP Script:

$url = $_GET['a'];
$domain = $_SERVER['HTTP_HOST'];

// Code to log the entry into the MySQL database

header("Location: http://$domain/$url");
exit();

So, I access some file, point that file to the PHP script and the script will log and redirect to that file... However, when PHP redirects to that file, the htaccess rules will pick it up and redirect again too the PHP script, creating an infinite loop.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

无语# 2024-08-01 20:26:01

最好的办法是解析服务器日志。 这些将显示域和请求。 即使大多数共享托管帐户也提供对日志的访问。

如果您打算采用重写路线,则可以使用 RewriteCond 检查 HTTP_REFERER 值,以查看引荐来源网址是否为本地链接。

 RewriteCond %{HTTP_HOST} ^(www\.)?domain1\.net [NC]
 RewriteCond %{HTTP_REFERER} !^(.*)domain1(.*)$ [NC]
 RewriteRule ^(.*)$ http://www.domain1.net/logscript?a=$1 [NC,L]

 RewriteCond %{HTTP_HOST} ^(.*)domain2\.net [NC]
 RewriteCond %{HTTP_REFERER} !^(.*)domain2(.*)$ [NC]
 RewriteRule ^(.*)$ http://www.domain2.net/logscript?a=$1 [NC,L]

您可能还想在 mod_rewrite 论坛中发帖。 他们有一整节关于处理域的内容。

The best thing do would be to parse the server logs. Those will show the domain and request. Even most shared hosting accounts provide access to the logs.

If you're going to go the rewrite route, you could use RewriteCond to check the HTTP_REFERER value to see if the referer was a local link or not.

 RewriteCond %{HTTP_HOST} ^(www\.)?domain1\.net [NC]
 RewriteCond %{HTTP_REFERER} !^(.*)domain1(.*)$ [NC]
 RewriteRule ^(.*)$ http://www.domain1.net/logscript?a=$1 [NC,L]

 RewriteCond %{HTTP_HOST} ^(.*)domain2\.net [NC]
 RewriteCond %{HTTP_REFERER} !^(.*)domain2(.*)$ [NC]
 RewriteRule ^(.*)$ http://www.domain2.net/logscript?a=$1 [NC,L]

You may also want to post in the mod_rewrite forum. They have a whole section about handling domains.

2024-08-01 20:26:01

如果 Google Analytics 不是您的菜,

$_SERVER['HTTP_HOST']

它拥有所使用的域,您可以记录它(以及时间、浏览器、文件路径等)。 我认为不需要 mod_rewrite 。 检查 print_r($_SERVER) 以查看其他可能需要记录的内容。
确保仍然转义 (mysql_real_escape_string()) 所有日志值,例如,通过浏览器的用户代理字符串注入 SQL 非常容易。


因此,我访问某个文件,将该文件指向 PHP 脚本,该脚本将记录并重定向到该文件...但是,当 PHP 重定向到该文件时,htaccess 规则也会选择它并再次重定向PHP 脚本,创建无限循环。

您可以检查 RewriteCond 中的 HTTP 标头吗? 如果是这样,请尝试在 PHP 中的重定向旁边设置一个额外的标头(按照惯例,自定义 HTTP 标头以“X-”开头,因此它可能是 header('X-stayhere: 1');),并且如果 X-stayhere 标头是目前,RewriteCond 失败并且它不会将浏览器转发到 PHP 脚本。

但是,如果您可以运行一个脚本来下载服务器日志并通过一些免费软件日志文件分析器运行它们,我会选择这样做。 对每个请求进行两次重定向是相当大的开销..(如果我更清醒,我可能能够想出不同的解决方案)

If Google Analytics is not your thing,

$_SERVER['HTTP_HOST']

holds the domain that is used, you can log that (along with time, browser, filepath etc). No need for mod_rewrite I think. Check print_r($_SERVER) to see other things that might be interesting to log.
Make sure to still escape (mysql_real_escape_string()) all the log values, it's trivially easy to inject SQL via the browser's user-agent string for example.


So, I access some file, point that file to the PHP script and the script will log and redirect to that file... However, when PHP redirects to that file, the htaccess rules will pick it up and redirect again too the PHP script, creating an infinite loop.

Can you check for HTTP headers in the RewriteCond? If so, try setting an extra header alongside the redirect in PHP (by convention custom HTTP headers start with 'X-' so it could be header('X-stayhere: 1');), and if the X-stayhere header is present, the RewriteCond fails and it doesn't forward the browser to the PHP script.

If, however, you can cron a script to download the server logs and run them through some freeware logfile analyzer, I'd go with that instead. Having two redirects for every request is a fair bit of overhead.. (and if I was more awake I might be able to come up with different solutions)

残疾 2024-08-01 20:26:01

Google Analytics 不提供此选项吗? 或者您无法解析您的服务器日志文件?

Does Google Analytics not provide this option? Or could you not parse your server log files?

余生共白头 2024-08-01 20:26:01

为什么不使用 apache 中构建的访问日志工具?
Apache 有一个“管道日志”功能,允许您重定向访问登录到任何程序。

CustomLog "|/path/to/your/logger" common 

Why not use the access log facility build in apache?
Apache have a "piped log" function that allow you redirect the access log to any program.

CustomLog "|/path/to/your/logger" common 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文