Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

edesignuk

Moderator emeritus
Original poster
Mar 25, 2002
19,232
2
London, England
Hoping someone here can help. I'm currently starting to build our IT intranet at work. As part of the support features I'd like to be able to display some details of the PC browsing the site (this would be useful for our help desk to get information from the user easily).

Would anyone be able to tell me the exact code I need to display:

IP Address
Hostname
Browser & Version
Anything else you can think of that php is capable of, and would be useful for this situation.

I don't know anything about php, so please be gentle and explain anything fully :)

Thank you!
 
Here's one to display the ip address

PHP:
function getip() {
	if (isSet($_SERVER)) {
		if (isSet($_SERVER["HTTP_X_FORWARDED_FOR"])) {
			$realip = $_SERVER["HTTP_X_FORWARDED_FOR"];
		} elseif (isSet($_SERVER["HTTP_CLIENT_IP"])) {
			$realip = $_SERVER["HTTP_CLIENT_IP"];
		} else {
		 	$realip = $_SERVER["REMOTE_ADDR"];
		}
	} else {
		 if ( getenv( 'HTTP_X_FORWARDED_FOR' ) ) {
		  	$realip = getenv( 'HTTP_X_FORWARDED_FOR' );
		 } elseif ( getenv( 'HTTP_CLIENT_IP' ) ) {
		  	$realip = getenv( 'HTTP_CLIENT_IP' );
		 } else {
		  	$realip = getenv( 'REMOTE_ADDR' );
		 }
	}
	return $realip; 
}
 
and I believe browser information can be displayed using this environmental variable

PHP:
$_SERVER['HTTP_USER_AGENT'];
 
thanks angelneo, what other tags do I need in the page to make this work? remember, I don't know ANYTHING about php :eek:
 
You just need to wrap them up in the php tag and they are ready to use. Just cut and paste the following code into (any file name).php
PHP:
<?php
function getip() {
	if (isSet($_SERVER)) {
		if (isSet($_SERVER["HTTP_X_FORWARDED_FOR"])) {
			$realip = $_SERVER["HTTP_X_FORWARDED_FOR"];
		} elseif (isSet($_SERVER["HTTP_CLIENT_IP"])) {
			$realip = $_SERVER["HTTP_CLIENT_IP"];
		} else {
		 	$realip = $_SERVER["REMOTE_ADDR"];
		}
	} else {
		 if ( getenv( 'HTTP_X_FORWARDED_FOR' ) ) {
		  	$realip = getenv( 'HTTP_X_FORWARDED_FOR' );
		 } elseif ( getenv( 'HTTP_CLIENT_IP' ) ) {
		  	$realip = getenv( 'HTTP_CLIENT_IP' );
		 } else {
		  	$realip = getenv( 'REMOTE_ADDR' );
		 }
	}
	return $realip; 
}

//print out the ip and browser information

print getip();
print $_SERVER['HTTP_USER_AGENT']; 
?>
 
Thanks angelneo, but the IP address it's displaying is 127.0.0.1, which is not my IP address (that's internal loopback) :confused:
 
edesignuk said:
Thanks angelneo, but the IP address it's displaying is 127.0.0.1, which is not my IP address (that's internal loopback) :confused:
Are you running a local server to test the page? I think you are accessing the page using http://localhost, you can try uploading to your remote server and test it out.
 
you need to run it from a server with PHP enabled, i belive mac 10.3 has php already compliled into apache. i can chuck in on my server if you want to have a muck around with it. i can give you upload rights (via ftp ect) if you dont have the resources.
 
Start up with an empty file an insert <? phpinfo() ?>. If you load that file up in your Browser (for example the "Microsoft Internet Explorer" :eek: ) you will get a list of all the enviromental variables. Pick the ones you want to use and output them using "echo" as such:

PHP:
<? echo $_SERVER["HTTP_USER_AGENT"] ?>
 
e,

Check out this list of all the variables that may be accessible - depends on the configuration.

http://us2.php.net/reserved.variables

An easy way to list them is to build an array of the ones you are interested in and loop through them like this.


PHP:
<?php
   $envVars = array($_SERVER["HTTP_USER_AGENT"], $_SERVER["HTTP_REFERER"]);

   foreach($envVars as $var)
   {
        print "$var: ${$var}<br>";
   }
?>
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.