Page 1 of 1

getting ip

Posted: Thu Jan 30, 2003 5:59 am
by jamal
Hi Guys,
Please can anyone tell me why this snippet
is not doing what is expected.
The code could store only the date but not
IP address.
Please why??
Can anyone help me??
Thanks in advanced

Code: Select all

<?php

$ip_address = $REMOTE_ADDR;

$the_date = date("Y-m-d");

$conn = @mysql_connect("localhost","username","password") or die("cannot connect to MySQL");

@mysql_select_db("ipvisits") or die("cannot connect to the database");

@mysql_query("INSERT INTO visitorsips(ip,date) VALUES ('$ip_address','$the_date')");

mysql_close($conn);
?>

Posted: Thu Jan 30, 2003 7:01 am
by Wayne
This could be a couple of things. What error message are you getting?

For a start, have you checked that $REMOTE_ADDR has a value? Let us know the error and we will try and help out!

Posted: Thu Jan 30, 2003 7:44 am
by twigletmac
Have you tried:

Code: Select all

$ip = $_SERVER['REMOTE_ADDR'];
if you have PHP version 4.1 or above or

Code: Select all

$ip = $HTTP_SERVER_VARS['REMOTE_ADDR'];
if you have PHP version 4.0.6 or below.

Mac

Posted: Thu Jan 30, 2003 9:44 am
by jamal
I don't get any error but the IP is not store into the db.
And my second question is to limit the IP to 1.
Because the date is recorded in every visit of the visitor which I don't want.

Posted: Fri Jan 31, 2003 3:18 am
by twigletmac
Did you try the two variables I posted above?

$REMOTE_ADDR will not work if register_globals is off.

Mac

Posted: Fri Jan 31, 2003 3:31 am
by bionicdonkey
this is what i used for mine web site

$count_query = mysql_query("SELECT * FROM counter");
$hits = mysql_num_rows($count_query);
$hits++;
$time = time();
$ip = $_SERVER['REMOTE_ADDR'];
mysql_query("INSERT INTO counter VALUES($hits, '$ip', $time)"); // Log current IP, hit number & time

$ip address is stored as string, $time & $hits are integers