storing ip address of visitors

pace112

New Member
Messages
5
Reaction score
0
Points
0
Hi! does anyone know how to capture ip address of visitors? Well! I have used the following code but , it' s not working in this server.

<?php
$ip=$_SERVER['REMOTE_ADDR'];


mysql_query("INSERT INTO table (count,ipaddress)
VALUES ('null', '$ip')");
?>
Note: Database is well connected and the above tables are properly established :dunno:
 

Zenax

Active Member
Messages
1,377
Reaction score
4
Points
38
PHP:
<?php
$ip=$_SERVER['REMOTE_ADDR'];
 
 
mysql_query("INSERT INTO table (count,ipaddress) 
VALUES ('null', '$ip')");
?>

If you look at the code he already uses " at the beginning before INSERT in the statement. If he used them again it would cancel it out.
 

Livewire

Abuse Compliance Officer
Staff member
Messages
18,169
Reaction score
216
Points
63
If you look at the code he already uses " at the beginning before INSERT in the statement. If he used them again it would cancel it out.

He means double single quotes, not a single double.

Ergo, 2 of these: ' in a row, instead of 'null'.


Which if memory serves is correct - you need to give it nothing in that field, and to do so, you pass it an empty string :)
 

pace112

New Member
Messages
5
Reaction score
0
Points
0
Actually my code works properly in local server of my pc. Maybe I need to upgrade my account?
 

woiwky

New Member
Messages
390
Reaction score
0
Points
0
Getting visitors' IP's and storing them in the db is certainly something even Basic level allows. I'm not too sure what the problem is here. Have you tried getting an error from mysql_error()?
 

vTech

New Member
Messages
34
Reaction score
0
Points
0
based on the php code,i dun see u connecting to a database....
instead u just insert right away....

the server dunno which database to insert to...
 

woiwky

New Member
Messages
390
Reaction score
0
Points
0
I assumed that he took that code out of context since he said there was a db connection. However, you may be right seeing as how that code *should* work if there is a connection and the tables are correct as he claims.
 

pace112

New Member
Messages
5
Reaction score
0
Points
0
I have already linked to the database. I didn't think that was necessary to post. However, here is the code that I've used
<?php
mysql_connect("localhost", "username", "password") or die ("Can't connect!");
mysql_select_db("db_name") or die("Can't select database!");
?>
 
Last edited:

phpasks

New Member
Messages
145
Reaction score
0
Points
0
PHP:
<?php
mysql_connect("localhost", "username", "password") or die ("Can't connect!");
mysql_select_db("db_name") or die("Can't select database!");

$ip=$_SERVER['REMOTE_ADDR'];
 
//count is auto increament value - primary filed set
 
mysql_query("INSERT INTO table (ipaddress) 
VALUES ('$ip')");

?>

You can used this type of table and work.
 
Top