Click to See Complete Forum and Search --> : Detecting an IP address?


hammerslane
10-08-2003, 03:47 AM
I'm sure that there's got to be a thread about this somewhere, but 'IP' is under the minimum word search length :(

ah well, here goes... (bear in mind i'm very new at PHP)
I'm doing an Intranet system, and I want to detect which user is browsing the intranet by their IP address. http://www.whatismyip.com/ does this, but I don't know if it's PHP or what not. The general idea is that I'd then write the IP address to a variable, and case test the variable (against the list of all users IP addys that I have) to then display "Hello JOE BLOGGS" or something... a bit pointless; but a nice corny touch :)

I'm not really sure if I know what I'm doing, so point me in the right direction if what I'm trying to do is impossible :cool:
thanks,
regards

fyrestrtr
10-08-2003, 04:06 AM
You can find out the IP for the remote user using the $_SERVER auto-global array (if your version of php is < 4.1 .. use $HTTP_SERVER_VARS ... and then promptly upgrade your PHP).

More information about server variables is at the php manual (http://www.php.net/manual/en/reserved.variables.php#reserved.variables.server).

A problem with your idea is that it assumes that a user will always be on the same physical computer.

If Joe -- who normally is at 192.168.1.122 -- decides to browse the webpage from Bob's computer (192.168.1.123), your script will reply "Hello Bob!" -- which would alarm Joe to no end.

Now there are a few ways to get around this.

First, if you are using PHP with IIS and have integrated authentication enabled, then $_SERVER['LOGON_USER'] will tell you what the currently logged on Windows
user is.

Now, if the HTTP server (IIS) is on a domain, then this should also work :

$username = str_replace("DOMAIN\\", "", $_SERVER['REMOTE_USER']);

This might turn out to be some expensive cheeze.

hammerslane
10-08-2003, 04:15 AM
:D
thanks for the reply... i didn't realise you could detect which Windows user was logged on. spiffeh.
i'm not sure if i made this very clear before, but i know each individual ip address of every computer within the network (about 10 machines) and they'd all browse the intranet via their own machines (we don't move about in our office here :) ), therefore eliminating the possibility that bob would see 'hello joe'.

i'll have a dig around at php.net and let you know!
Thanks once again,
regards,

pyro
10-08-2003, 07:07 AM
Originally posted by fyrestrtr
If Joe -- who normally is at 192.168.1.122 -- decides to browse the webpage from Bob's computer (192.168.1.123), your script will reply "Hello Bob!" -- which would alarm Joe to no end.I'd think that if Joe is using Bob's computer, he'd expect to be identified as Bob, no? ;)

fyrestrtr
10-08-2003, 07:15 AM
One more thing that I forgot to mention ...if the IP addresses are assigned by DHCP, there is a remote chance that the IP address will change for a physical computer, which would then render the script useless.

pyro
10-08-2003, 07:28 AM
If all you want to do is display a message, I'd just use a cookie (http://us3.php.net/manual/en/function.setcookie.php).

hammerslane
10-08-2003, 07:29 AM
I'd think that if Joe is using Bob's computer, he'd expect to be identified as Bob, no?good point... :)

i've used the switch statement to differentiate between users on the network - i've tried it, and it works.

if someones IP changes (not sure why it would... don't know DHCP stuff) it'll just say 'hello user' - which won't really be much bother, the script is just a piece of cheese that people will stop noticing after a while anyway.

code used:

<?php
echo "Welcome, ";
switch ($REMOTE_ADDR) {

case $REMOTE_ADDR="192.***.0.127":
echo ("hammerslane!");
break;

case $REMOTE_ADDR="192.***.0.78":
echo ("Bob!");
break;




default:
echo ("User");
}

?>


edit: pyro: i thought about using cookies, but people here are always deleting their cookies and messing around with what cookies are allowed in and what not... so as i had just made a note of all IP's, i just went for this method.

:cool:
thanks guys,
RESOLVED
not sure if i have to put that... but yeh..