views:

245

answers:

4

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.

A: 

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

Lucas Jones
Please check the edit question...
Nazgulled
Unfortunately, I have no idea how to help you in my current position. You could ask a separate question with information about your infinite loop problem with your solution, and we can approach it from that angle. Thanks for your understanding.
Lucas Jones
I edited once again the question above to explain the infinite loop. No need no create another question. If you want, just give a different answer below.
Nazgulled
Again, my expertise is lacking in this area - I've never been able to figure out mod-rewrite! Sorry I can't help.
Lucas Jones
Why did you asked then? :P
Nazgulled
In case I could help. Anyway, someone else could find the question and help you.
Lucas Jones
Thanks anyway :)
Nazgulled
+2  A: 

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.

Chris Thompson
Please check the edit question...
Nazgulled
Updated my answer
Chris Thompson
That might work, I'll have to test it... Not now though cause it's 5AM around here... I'll report back.
Nazgulled
Didn't work, I believe the HTTP_REFERER, in this case, is empty...
Nazgulled
You can check for an empty, but I figured that condition would work equally well for empty referers.
Chris Thompson
+1  A: 

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)

MSpreij
Please check the edit question...
Nazgulled
check the update :)
MSpreij
If Chris's solution (above) doesn't solve it, I'll give this one a go. It seems a plausible solution too...
Nazgulled
This didn't work either. I just realized that the PHP script is not even executed because the is calling the PHP script indefinitely until it reaches the limit and reports an error.
Nazgulled
A: 

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
J-16 SDiZ
I'm on shared hosting, I don't think that will work for me.
Nazgulled