hm_468x60_09_395
announcement

Joomla Web Hosting

Find the best and most affordable Joomla web hosting provider.

Community Showcase on joomla.org

The Joomla Project is pleased to announce latest undertaking to promote Joomla site

Advertise on JoomlPanel.com

For only $30.00 a month you can advertise on one of the web's largest Joomla websites.

More Changes Coming Soon!

Check back for future changes!

bh_300x250_08_395

Silently Recording User IP Address

In this tutorial you will see how you can create a script that will silently record a user IP address and store all information in a .txt file, no database required! If your website has a feature such as uploader and you want to track the people that use it, then you may want to consider using some sort of record script that will record information to somewhere.

Who uses IP Logging?

IP Logging is used alot now a days, a common place to see it happening is on sites that use shopping cart systems. You will often find that a message will be on the website somewhere stating that your IP address is being logged (For that session) you can also find IP logging on major websites such as Wikipedia. IP logging is present as Wikipedia is a publicly accessible knowledge base that can be edited by anyone, in order to prevent abuse they would track user's IP address to make sure they can block anyone that abuses it.

Why use IP Logging?

I've basically started to cover why someone would want to use IP logging, mainly for prevention of events that could happen, take note of the Wikipedia situation. IP Logging allows you to have some sort of log to look over and review to spot any interesting/query IP address which you may be suspicious of. Essentially giving you the upper hand againest someone thats abusing something.

The PHP Script:

Enough chit chat. Let's show the script thats going to log IP's:

01.
02.// Checks the current date and time
03.$dateTime = date('Y/m/d G:i:s');
04.
05.// Loads the writable text file and await instruction from fwrite command
06.$fp = fopen('ip-log.txt', 'r+');
07.
08.while (!feof($fp))
09.{
10.$buffer = fgets($fp);
11.// Scan for any previous logs of the IP
12.// If scan determines that the user is a logged IP address, overwrite existing log.
13.list ($ip, $crap) = split(' ', $buffer);
14.if ($_SERVER['REMOTE_ADDR'] == trim($ip))
15.$exists = TRUE;
16.}
17.// If scan determines it's user is a new IP entry, add the IP address to the log file at the end of the document
18.if (!$exists)
19.fwrite($fp, $_SERVER['REMOTE_ADDR']. " $dateTime\n");
20.fclose($fp);
21.?>

The comment marks should explain many of your questions but I'll break it down:

1.$dateTime = date('Y/m/d G:i:s');

First off the script will check the current date and time. It will need to be called first otherwise the log can't process a date and time stamp.

1.$fp = fopen('ip-log.txt', 'r+');

This will be your text file that the script will write all logs to. Whatever you name it and wherever it's stored you must update the filename in the script otherwise the script will throw out alot of error's. You will also need to make this text file writable (CHMOD 777) As a recommendation I'd place the text file you are going to use within the public_html folder so it's easily accessible. Notice the fopen command. This is needed to start the log process otherwise the script will not write anything to the text file

01.$buffer = fgets($fp);
02.// Scan for any previous logs of the IP
03.// If scan determines that the user is a logged IP address, overwrite existing log.
04.list ($ip, $crap) = split(' ', $buffer);
05.if ($_SERVER['REMOTE_ADDR'] == trim($ip))
06.$exists = TRUE;
07.}
08.// If scan determines it's user is a new IP entry, add the IP address to the log file at the end of the document
09.if (!$exists)
10.fwrite($fp, $_SERVER['REMOTE_ADDR']. " $dateTime\n");
11.fclose($fp);

This script will now check the log file for any previous logs of the IP Address that is being logged. The fgets command will be used to fetch the log file and check for any current logs of the same IP Address. If the IP address is new and has not been logged before then the fwrite command will write the new IP address to the log.

1.fclose($fp);

The fclose command is important as it will stop the log writing any other details that could end up as bad data. It is vital it is placed as:

1. This script will slightly slow your page loading time down. Not adding in a close function will either effect your page loading times dramatically, or even cause endless loading times (Page will never load)

2. You may end up with random log data if the script does not have a stop function

Security Isssues with this script:

The security level of this script is low as it only records a users IP Address nothing else. If it recorded personal data or passwords it would be much more serious. The fact of the script not using a database may be good for some people, but in actual fact using a database would make this script more secure, but being able to make a script that can write log information to a writable text file does provide some advantages. The point of this script is a quick and simple way to record a users IP Address and as long as you don't advertise the fact that you have a text file thats logs IP address's no one will know.

Even if you want to add a note on your website that IP addresses are logged nearly 99% of people will never know how your logging IP addresses.

So thats how to make a silent and easy way of logging a users IP Address. I've pointed out it has issues but in the end the script does a big stop with little requirements!

Note: I would also like to add that this script should only be used in obtaining user information for a valid purpose. Abusing this script by trying to compramise someone's data is wrong. Respect the data protection act.

 

joomlahosting




Joomla Tutorials

Joomla panel provides easy to use website joomla tutorial free download joomla templates for joomla verssion 1.0x and 1.5x. Here you will learn how to install Joomla, all the way to installing and customizing your own templates.

Tutorials Showcase enables you to build Web sites and powerful web applications. Best of all, Joomla is an open source solution that is freely available to everyone.

Building Web CMS Project

Submit your joomla web to joomlapanel web showcase gallery directories and find the joomla stable extensions : Joomla plugin, joomla module, joomla component, joomla mambot.

Submit your Article blog into our article directories to anchance high web Traffic.

 

Article and Tips trick

Your sohowcase for Travel guide; Hotel, Villas resort, Flight, car website

tutorial