display page visitor information with PHP

It’s easy to retrieve and display information about page visitors with PHP using predefined variables. eg to show IP address, referrer and browser type:

<?php
// Display users IP address
echo "

IP Address: " . $_SERVER['REMOTE_ADDR'] . "

"; // Display users port number echo "

Port Number: " . $_SERVER['REMOTE_PORT'] . "

"; // Display users host name // see note below for using this echo "

Host Name: " . $_SERVER['REMOTE_HOST'] . "

"; // Display the URI used to access this page (entire request path, including query string) echo "

Request URI: " . $_SERVER['REQUEST_URI'] . "

"; // Display the URL used to access this page (request path without query string) echo "

Request URL: " . $_SERVER['REQUEST_URL'] . "

"; // Display the referrer echo "

Referrer: " . $_SERVER['HTTP_REFERER'] . "

"; // Display users browser type echo "

Browser: " . $_SERVER['HTTP_USER_AGENT'] . "

"; ?>
Note: To use $_SERVER['REMOTE_HOST'], your web server must be configured to create this variable. For example in Apache you’ll need HostnameLookups On inside httpd.conf for it to exist. See also gethostbyaddr().

You can utilise this in many ways, whether it in order to display different information to visitors based on their browser or operating system; or to record this information yourself for building a better site which meets the needs of users better.

Check the php.net listing of $_SERVER variables for more predefined variables you can utilise.

Advertisements

About Cameron
I'm a final year Computer Science/Information Systems major. Already finished my BA in Politics/Philosophy. I do web and software freelance on the side, while I finish studying. Hoping to be self-employed by the end of my degree, otherwise off into the real-world I go....

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: