Ranger135xp
2006-12-20 20:53:39
ok, this is to find the page viewers IP address then log it into a text file.
[PHP]<?PHP
//By Ranger135xp
$timezone=-4;//YOu will probably pull this value from the database. I used -4 cuz i'm in Pennsylvania
$time=time();//It is a timestamp of the event. You will also pull this from the DB
$gmttime= gmdate("M d Y H:i:s", $time);
$convertedtime=date("l - F j, Y - g:i:s A",strtotime("$timezone hours",strtotime($gmttime)));
//echo "$convertedtime <br> ";
$log_file = "ip.txt";
$ip = getenv('REMOTE_ADDR');
$fp = fopen("$log_file", "a");
fputs($fp, "$ip on $convertedtime\n");
fclose($fp);[/PHP]
that should work, i use it on my site.
Bam Bam
2007-02-04 12:39:18
This doesnt look like it will work.
I recommend that you read a decent tutorial on concatenating strings and using variables such as this:
http://w3schools.com/php/php_variables.asp
Ranger135xp
2007-02-14 14:21:05
it works just fine buddy. how about using it first before giving it your expert(NOT) opinion. :)
botmaster
2007-02-14 16:17:51
Here's how to log IP's into a mysql database (in case you have one):
1. Create a new table with primary key called 'id' (autoincrement), another key called 'ip' (string/varchar), and another one called 'time' (string/varchar). Lets assume you called the table "iplog".
2. First, you must connect to the mysql database. If you already have an include to handle this, don't follow this step. If you don't, put this code into a file called "mysql_connect.php":
[PHP]
define('MYSQL_HOST', 'yourhosthere'); //put your host here
define('MYSQL_USER', 'yourusername'); //db username
define('MYSQL_PASS', 'yourpassword'); //password to db
define('MYSQL_DATABASE', 'dbname'); //db name
function connect(){
$db_link = mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASS);
if (!$db_link) {
echo "Mysql connect problem<br />\n";
die(mysql_error()); //Don't need it if you don't want to stop on error
}
mysql_select_db(MYSQL_DATABASE, $db_link) or die("Coulnt't connect to database"); //remove 'or die' part if want to run script anyways
}
[/PHP]
Save the file as mysql_connect.php . You'll need it later.
3. Now for the actual code:
[PHP]
//Put this at the beginning of your page
require_once "path_to/mysql_connect.php" //path to mysql_connect.php
connect(); //connect to db
$ip = getenv('REMOTE_ADDR');
$time = time();
mysql_query("INSERT INTO iplog VALUES (NULL, $ip, $time)" or die('Couldn\'t insert into table');
[/PHP]
There you are. Now, to read them back out from the database, you need to use the code below. Note you should have a loginsystem and make it retreive the IP only if you're logged in, else everyone on the planet can see your visitors' IP and start h4xing them to pieces.
[PHP]
//If this is a separate file from the ip-logging file, uncomment the following:
//require_once "mysql_connect.php";
//connect();
$query = "SELECT
ip,
time
FROM
iplog
ORDER BY
time DESC;";
$result = mysql_query($query) OR die(mysql_error()); //or die isn't required
echo "Here are teh logged IPs:<br>\n";
while($row = mysql_fetch_assoc($result)) {
echo $row['ip']." @ time ".$row['time']."<br />\n";
}
[/php]
Should display the IPs… I hope.