is there a variable of the ip address of users? $hostname?


Tiburón’s picture

Hi sisyphus,

Have a look at the PHP manual and what it says about "PHP: Predefined Variables".

In this case

  $userip = $_SERVER['REMOTE_ADDR'];

should give you what you are looking for.


Christian Larsen

sisyphus’s picture

thanks. but the table comments has a field hostname. then there must have a variable inside drupal to store the user's ip address.

lordgilman’s picture

Drupal 5's comment module uses the REMOTE_ADDR value as well. This code is the query that creates the comment's row in the database:

db_query("INSERT INTO {comments} (cid, nid, pid, uid, subject, comment, format, hostname, timestamp, status, score, users, thread, name, mail, homepage) VALUES (%d, %d, %d, %d, '%s', '%s', %d, '%s', %d, %d, %d, '%s', '%s', '%s', '%s', '%s')", $edit['cid'], $edit['nid'], $edit['pid'], $edit['uid'], $edit['subject'], $edit['comment'], $edit['format'], $_SERVER['REMOTE_ADDR'], $edit['timestamp'], $edit['status'], $score, $users, $thread, $edit['name'], $edit['mail'], $edit['homepage']);

(if you're unfamiliar with sql queries the part from INSERT INTO to VALUES (cid, nid, uid...) are the columns we want to change. The values that will go into the columns are all the $variables and they are in the same order as the columns were listed above. The "hostname" row was the 8th one listed so the 8th variable ($_SERVER['REMOTE_ADDR']) is what gets put in the hostname column.)

patcon’s picture

Seems there a better way now :)

Apparently, it checks to see if the user's behind a proxy, and uses a more accurate variable if so.

chefarov’s picture

And how do we find out the ip for a specific user?
Suppose a user called DJ;

How do we call the $_SERVER['REMOTE_ADDR']; or whatever to capture his ip when he logs in for example?
I mean obviously in every login, to check for a username (DJ) and when we find it to capture ip.

patcon’s picture

Maybe I'm reading you wrong, but this might be relevant:

It's saved as part of the access log, and stored in the watchdog table.
You can choose to keep up to 1,000,000 log entries via admin/settings/logging/dblog

zrtech’s picture

After searching around to figure out an easier way to see a user's IP address other than the dreaded sifting through endless pages in Reports>Recent Hits, I came up with the easiest Drupal 7 Core solution.

Go to the People page, click on a Username, then click on Track Page Visits.

If the user does not have any tracking information, then it would be beneficial to install the IP Manager module (, which will include a new tab in the People>Username page named Manage IP Addresses.

Unfortunately there seems to be no data for users who have not visited pages after registration and the Manage IP Addresses module will not begin tracking IP adresses until after its installation.

If you are attempting to block IP addresses due to spam, It is highly recommended to check out the User Restrictions Module (, which allows you to block e-mail address domain's that are consistent during spam user sign up. (This was the scenario I was presented with and I blocked IP address, e-mail domain and user account for each spam account.)

If you have any further questions with managing spammers, contact us at (

Good luck!
"Z" - Zahir Narsisto Rodulfo
ZRTECH - ITS/Prjct Mngr/Grphx Dsgnr

malcomio’s picture

In case anyone lands on this page from a search, there is some documentation on how to do this in Drupal 7 and 8 at

sumanthkumarc’s picture

ip_address() function in drupal 7 is the best way i know.