0

I have a php my admin database,

I don't know very much how to structure it but following this answer I Made a

ip int(11) UNSIGNED

value for the IP

I would like to store the IP of user, so that they can't access twice to my page.

I do the following query

$ip = inet_pton($_SERVER['REMOTE_ADDR']);
$request = "INSERT INTO `users` (`ip`) VALUES (".$ip.")";
$result = $dbh->query($request);

but this give me an empty value. and nothing is added to the database

EDIT :

User table enter image description here

So full request is :

function getIPforBDD(){
    return inet_pton($_SERVER['REMOTE_ADDR']);
}
$request = "INSERT INTO `users` (`ip`, `finish`, `lastPage`) VALUES (".getIPforBDD().", ".$GLOBALS["userHasFinish"].", questionnaire_initial.php)";
$result = $dbh->query($request);
Crocsx
  • 2,534
  • 1
  • 28
  • 50
  • Please show the full users table columns. – Than Ngo Hoai Jun 04 '18 at 08:34
  • [`inet_pton()`](http://il1.php.net/manual/en/function.inet-pton.php) returns a string, not an integer. If you use [`ip2long()`](http://php.net/manual/en/function.ip2long.php) instead, you will get an integer. That's better since MySQL is faster to compare integers than strings. – M. Eriksson Jun 04 '18 at 08:43

2 Answers2

0

After using inet_pton() function, data won't be in readable format. Just use varchar(16) datatype in your database for ip column and change first row of your code to:

$ip = $_SERVER['REMOTE_ADDR'];
  • Many people over the internet when I was searching for this answer say this is a bad idea and give other solution (but are only in mysql so doesn't work in my case) – Crocsx Jun 04 '18 at 09:53
-1

Can you try this?

$request = "INSERT INTO users (ip) VALUES ('$ip')";
Antoder
  • 65
  • 2
  • 9