From the Blog

Feb
18

Continuing my series on web development stuff for hacks, this post includes the necessary information to detect a user’s IP address (as well as other variables) with SSI on an Apache server. SSI stands for Server Side Includes and they are directives which are placed in HTML pages and evaluated on the server while the pages are being served allowing for some dynamic content. For more information on SSI, Apache has a pretty good tutorial.

By the way, this post assumes you have control over your web server. If you do not check with your hosting company to see if they allow SSI. Also, there is something available for SSI on Windows servers but I don’t know much about it.

The first part of this exercise is to ensure the web server is configured properly. SSI is available on Apache servers with mod_include installed and enabled. For more information on mod_include visit the Apache web site for your version of Apache. In order to use SSI the following directive must be present in either the httpd.conf file or in an .htaccess file:

Options +Includes

The following directives must also be present in the httpd.conf file to tell Apache which files to parse for SSI:

AddType text/html .shtml
AddHandler server-parsed .shtml

I figured out where the last three directives were located by opening httpd.conf with pico and then used the Command W feature to search for Options, then SHTML. If these directives are already present but preceded by a # then simple remove the # (it comments out the line it precedes in httpd.conf).

On my server the Options directive was already configured as:

Options Indexes FollowSymLinks MultiViews

and the first time I made the mistake of adding a second line starting with Options

like this:

Options Indexes FollowSymLinks MultiViews
Options Includes

This DID NOT work, the web server would not restart. Instead, the correct syntax

is:

Options Indexes FollowSymLinks MultiViews Includes

Finally, it is necessary to add the following line inside the virtual directory for the specific site which will be running the SSI files:

Options +Includes

In context this will look something like:

<VirtualHost *:80>
ServerName yourserver.com
DocumentRoot /Library/WebServer/Documents/yoursite
Options +Includes
</VirtualHost>

Once you have properly configured your server it is time to test this out. The simplest approach is to create an SHTM file, for instance index.shtml. Then place this code into the HEAD of your SHTML document:

<SCRIPT LANGUAGE=”JavaScript”>

// Set a variable IP with the IP address
var ip = ‘<!–#echo var=”REMOTE_ADDR”–>’;

//Then put it in an Alert box (or do anything else you want with it)
alert(“Your IP address is “+ip);

</script>

If everything has been done properly you will see this:

alert

Inside the alert window it will say: “Your IP address isĀ  000.000.000.0″ (of course, replace the 0′s with your IP address)