0

I'm setting up a web server using apache and want to be able to get client's IP and hostname from any page they visit on the website without having to manually implement logging code in all the files.

I heard about .htaccess RewriteRule and tried that but it just redirects me to a page which logs that info as opposed to staying on the same page and automatically logging it in the background.

RewriteEngine On
RewriteRule ^flower.jpg$ info.php

Say I have a page info.php which logs that info and I have some other pages like welcome.html, flower.jpg, contact.html, game.png

What I expect: Whenever welcome.html, flower.jpg, contact.html or game.png is visited I expect info.php to auto load and log client's IP and hostname in the background.

What actually happened: I got redirected to info.php as opposed to staying on the same page(like flower.jpg as shown in the code)

How can I make sure I load info.php using htaccess in the background without getting redirected to it?

Norman
  • 11
  • 3
  • 3
    What are you really trying to accomplish? Can you not get the information you need from the [access logs](https://httpd.apache.org/docs/2.4/logs.html#accesslog)? – PaulProgrammer Apr 29 '19 at 20:50
  • I don't think .htaccess is capable of loading php files in the background. Sounds like you need a PHP framework (or router). – GrumpyCrouton Apr 29 '19 at 20:52
  • You could use https://www.php.net/manual/en/ini.core.php#ini.auto-prepend-file howver you would have to load the images through a PHP file to get those. – AbraCadaver Apr 29 '19 at 20:54
  • you could use google analytics, you wont find any better user tracking\information –  Apr 29 '19 at 20:57
  • Use the access logs. It already does what you are asking. If you need to script it look at `htaccess parsers`. – user3783243 Apr 29 '19 at 21:09
  • Possible duplicate of [How to get the client IP address in PHP](https://stackoverflow.com/questions/3003145/how-to-get-the-client-ip-address-in-php) – zod Apr 29 '19 at 21:12
  • @zod They specifically say that they would prefer not to do manual logging. – Félix Adriyel Gagnon-Grenier Apr 29 '19 at 21:13
  • "How can I make sure I load info.php using htaccess" may be this is what you looking for https://unix.stackexchange.com/questions/246079/need-to-grab-ip-of-visitors-from-the-apache-logs – zod Apr 29 '19 at 21:20

1 Answers1

0

The Apache access log is at /var/log/apache2/access.log and contains the following for every request:

  • IP
  • Timestamp
  • User agent
  • File
  • Request method
  • Protocol
  • Status code
Ryland Goldman
  • 95
  • 1
  • 14