I'm running Drupal 7 web site with a Russian multiplayer card game and have to block some very annoying players every 4-5 days.

Before Drupal I was using phpBB 3. When these persons would come from a small city/village I'd just add their IP-network to the firewall rules, so that they can't re-register with a new mail address. This worked well in my case and my question is not about this policy.

My problem is that with Drupal, I can't find the last IP address used by a user id. I.e. the abuser is reported to me by other users and I know his uid, but I can't find his IP address in the logs, because:

  1. I've enabled syslog-logging and see the Drupal messages in /var/log/messagess but there is rarely a message about a user logging in, probably because sessions are long by default, and I'd like to keep it this way for user comfort.

  2. I've also enabled "database logging," but don't know where to see it (which database table).


5 Answers 5


If you have statistics module enabled, the "accesslog" table contains the IPs for all your visitors.

SELECT hostname FROM accesslog WHERE uid = %UID_FOR_YOUR_USER% LIMIT 1

If you do not have statistics enabled, I advise you to leave it off, even for this result, because enabling it, causes a severe performance-penalty.

In that case, you can use sessions table:

SELECT hostname FROM sessions WHERE uid = %UID_FOR_YOUR_USER% LIMIT 1

The benefit of the first over the latter, is that the first will always show you the latest hostname, whereas the latter might not always be up to date.

  • True, the sessions table has the IP addresses too. I just can't find some users there as well. Maybe it's my fault somehow. I'll accept your answer and disable syslog and database statistics modules, because they weren't of much use for me. Mar 29, 2011 at 12:30
  • Users that were not logged in for a while wil not have a session, and will not be found. As I stated "the latter will not always be up to date", meaning: it might not have all the users.
    – berkes
    Mar 29, 2011 at 13:02

As far as I know, Drupal doesn't reports the IP used by users in their user profile. You could create a custom module, verify the IP used by the user, and record it in your own database table; then you could show it in the user profile, to users who has the right permission (for example, to users with the permission to administer users).

The function you need is ip_address(), which you can use in your implementation of hook_user_login(). Implementing hook_user_view(), you can then show such information to the users with the right permission.

  • Thank you, but then I'll have the same problem as I have now: the users rarely login, as I can see in the syslog. And the hook_user_login() is only called when a user logs in (enters his password). Mar 29, 2011 at 5:40
  • You can force them to log in; if you empty the sessions table, users should log in again.
    – apaderno
    Mar 29, 2011 at 11:38

If you are using the database logging module, Drupal stores this information in the watchdog table, under the hostname column. You can either write custom queries to get at this information, or use the Views Watchdog module (currently only a dev release for 7.x, so I'm not sure how well it works in Drupal 7) to expose that table to Views.

  • Nope, the watchdog table has unfortunately same problem as the syslog core module: for some users I don't have the IP address, because they had logged in long time ago. So I get a report about some malicious user, try to find his IP to add it to my firewall drop-rules, but I can't find his IP anywhere. Mar 29, 2011 at 12:24
  • The default setting for watchdog is, unfortunately, to only retain 1000 messages. On an active site, this may only last an hour or less. Depending on your site, you may want to bump that number up (admin/config/development/logging), or alternatively build a custom IP storage solution as described by kiamlaluno.
    – jhedstrom
    Mar 29, 2011 at 15:46

Two recent modules may help with this:

User IP Log, which has Views integration, and IP Address Manager, which does not have Views integration but is intended to integrate with Manifest, Cave Your Trolls, and Misery-- all banning related modules.


session_id() gives you current user session-id whether he/she is a guest or a member.

$result = db_query("SELECT hostname FROM sessions WHERE sid = '".session_id()."'");
$data = db_fetch_object($result );
echo $data->hostname;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.