hi i wonder if someone can please help, i am wanting to include this piece of code in the header tag of my website home page to give me some idea of who is using my site and how often etc.
i want to gather the users ip address and store this in the database. i am trying to do this using the following php and mysql code:
<?php
//Test if it is a shared client
if (!empty($_SERVER['HTTP_CLIENT_IP'])){
$ip=$_SERVER['HTTP_CLIENT_IP'];
//Is it a proxy address
}elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
}else{
$ip=$_SERVER['REMOTE_ADDR'];
}
//The value of $ip at this point would look something like: "192.0.34.166"
$ip = ip2long($ip);
//The $ip would now look something like: 1073732954
$sql = "INSERT INTO ptb_sessions(user_ip) VALUES('$ip')";
$dbQuery = mysql_query($sql,$dbLink);
?>
heres what my table looks like:
ptb_sessions
session_id | user_id | user_ip | session_start | session_destroyed
for some reason its not working and no values are being inserted into the table, can anyone please explain where im going wrong thanks.
Error reporting
On the top of your php file add this:
error_reporting(E_ALL);
mysql_error();
Run your script again and see if any errors come up.
Field Type
If user_ip
is an INT
, remove the single quotes from ('$ip')
:
$sql = "INSERT INTO ptb_sessions(user_ip) VALUES($ip)";
Having quotes means that you are trying to store the ip
as a string, but actually the database expects an integer.
Note
Using mysql
is deprecated and you wouldn't step into this kind of problem if you were using PHP: PDO instead.