Reputation: 219
I have the below simple code for inserting some failed login values in my database, but I have some problems:
1- The ip address is being saved in the database as zero no success whatever I do (going crazy on this). 2- I used current time stamp to get the date and time in a simple query, now that I use prepared statements it doesnt work with it, I am forced to use date('Y-m-d H:i:s'); which gives out the wrong time.
My code is:
<?php
$username = $_SESSION['Name'];
$ip_address = $_SERVER['REMOTE_ADDR'];
$attempted = date('Y-m-d H:i:s');
$query = "INSERT INTO failed_logins (username, ip_address, attempted) VALUES(?, ?, ?)";
$failed = $conn->prepare($query);
$failed->bindParam(1, $username);
$failed->bindParam(2, $ip_address);
$failed->bindParam(3, $attempted);
$failed->execute();
?>
My ip_address row in the database is int(11) and it is unsigned.
Any suggestions would help thanks
UPDATE:
I did the below but still no luck:
I changed:
$ip_address = $_SERVER['REMOTE_ADDR'];
to this:
$ip_address = ip2long($_SERVER['REMOTE_ADDR']);
Upvotes: 1
Views: 80
Reputation: 99061
I am forced to use date('Y-m-d H:i:s'); which gives out the wrong time.
Have you tried setting the date_default_timezone_set
, i.e.:
date_default_timezone_set('America/Los_Angeles');
You cannot use an int
to store a var containing dots ans colons.
Change the DB ip_address
row from
int(11)
to
varchar(15); //ipv4
ALTER TABLE `sometable`
MODIFY COLUMN `ip_address` varchar(15);
Or
varchar(45); //ipv6
ALTER TABLE `sometable`
MODIFY COLUMN `ip_address` varchar(45);
if you want to store ipv6
addresses
References:
Maximum length of the textual representation of an IPv6 address?
Upvotes: 2
Reputation: 36
Also maybe to get IP(ish):
(string)$ip_address = getRealIpAddr();
function getRealIpAddr() {
(string)$ip='';
if (array_key_exists('HTTP_CLIENT_IP', $_SERVER) && !empty($_SERVER['HTTP_CLIENT_IP'])) { //check ip from share internet
$ip .= filter_input(INPUT_SERVER, 'HTTP_CLIENT_IP');
} elseif (array_key_exists('HTTP_X_FORWARDED_FOR', $_SERVER) && !empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { //to check ip is pass from proxy
$ip .= filter_input(INPUT_SERVER, 'HTTP_X_FORWARDED_FOR');
} else {
$ip .= filter_input(INPUT_SERVER, 'REMOTE_ADDR');
}
return $ip;
}
Upvotes: -1