getting ip

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jamal
Forum Commoner
Posts: 57
Joined: Sat Oct 26, 2002 7:53 pm
Location: London

getting ip

Post 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);
?>
User avatar
Wayne
Forum Contributor
Posts: 339
Joined: Wed Jun 05, 2002 10:59 am

Post 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!
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
jamal
Forum Commoner
Posts: 57
Joined: Sat Oct 26, 2002 7:53 pm
Location: London

Post 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.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Did you try the two variables I posted above?

$REMOTE_ADDR will not work if register_globals is off.

Mac
bionicdonkey
Forum Contributor
Posts: 132
Joined: Fri Jan 31, 2003 2:28 am
Location: Sydney, Australia
Contact:

Post 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
Post Reply