Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 250
  • Last Modified:

Display/log host name only

$host = gethostbyaddr($ip);

This will return the user's IP address and Host in a single line. If I wish to solely display the Host and still be able to return each value individually how would I go about that? In short I still want to be able to log the user's IP address, but only display the Host name.

  • 2
1 Solution
    $ip = $_SERVER['REMOTE_ADDR'];
    $fullhost = gethostbyaddr($ip);
    $host = preg_replace("/^[^.]+./", "*.", $fullhost);

IP address: <?=$ip?> | Host: <?=$host?>

Using your example aside from assigning a superglobal to a variable. You can still log the IP address of your visitors and only show the hostname or show them both.

getWiseAuthor Commented:
Thanks Kriek! That is exactly what I was looking for. Although I am not familiar with the slang superglobal could you point me in the right direction as far as manual references, perhaps?
You could classify superglobal arrays (4.1.0) under ..

Predefined variables: http://www.php.net/manual/en/language.variables.predefined.php 

I suppose, but $_SERVER is also considered a ..

Reserved predefined variable: http://www.php.net/manual/en/reserved.variables.php 

I started using superglobals just after PHP 4.2.0 was released. (External variables are no longer registered in the global scope by default, register_globals off by default)

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now