How to store ip addresses of visitors in database in php?
'::1' means it is your localhost IP Address.
::1 == 127.0.0.1
You can try this one also.
$ip=$_SERVER['REMOTE_ADDR'];
echo "IP address= $ip";
If your application hosted on same machine from where you are trying to request it will always return '::1', It means LocalHost. else it will return client IP Address.
how to save ip address in database
You can capture ip address by
$_SERVER['REMOTE_ADDR'];
every time if an user clicks on vote
, first capture ipaddress. then run a query like select * from table where ipaddress = "current ip address" and question number = some id and time difference >= 3 hours
if ip address is present, then user has answered, else increment the votes.
Store IP into mysql database
For handling and storing both IPv4 and IPv6 addresses, you can use datatype VARBINARY(16)
.
In version 5.6, MySQL (finally!) introduced conversion functions for IPv6 addresses: INET6_ATON
, so you don't have to do the conversion in your application.
If you are handling only IPv4 addresses, you can continue to use the INET6_ATON
function, and BINARY(4)
is a suitable datatype for storing it.
Conveniently, the inverse functions, for converting from the binary representation back to dotted decimal form (for IPv4 addresses) or the hex colon form (for IPv6 addresses) are also available.
Reference: https://dev.mysql.com/doc/refman/5.6/en/miscellaneous-functions.html#function_inet6-aton
Q: How to should I make this column decimal if the ip (version 6) can have more than 15 chars ?
A: Don't make the column DECIMAL
, make it VARBINARY(16)
, and convert both the IPv4 dotted decimal representation, and the IPv6 hex colon representation into a binary representatin to store it.
Storing IP address in MySQL database (IPv4 AND IPv6)
To store an IPv4 you can use an INT UNSIGNED
, while for a IPv6 you need a decimal(39,0),
to store an ip in the table you can use the function INET_ATON
:
INSERT INTO table (ipcol) VALUES (INET_ATON('192.168.0.10'));
and retrieve it back with the function INET_NTOA
:
SELECT INET_NTOA(ipcol) AS ip FROM table;
This answered existing before MySQL IPv6 support; users should be made aware that MySQL now natively supports IPv6: https://dev.mysql.com/doc/refman/5.6/en/miscellaneous-functions.html
IP address is stored as 0 in SQL using PHP
You shouldn't store your ip address in an unsigned integer field. Store it in an varchar format and it should fix your problem. Integer is expecting a normal number, not something with multiple .
in it.
Best way to store IP in database?
Store the ip as a INT(11) UNSIGNED
, then use the INET_ATON
and INET_NTOA
functions to store/retrieve the ip address.
Sample code:
INSERT table(ip) VALUES (INET_ATON('192.168.0.1')); /*ip = 3232235521*/
SELECT INET_NTOA(ip) As IPAddress FROM table; /*IPAddress = 192.168.0.1*/
saving current ip addresses in mysql table
Firstly, may I suggest that this question isn't titled very well? It's a typical mistake for people to 2nd guess what the solution for a problem is when asking for help - I see this all the time in current/previous companies I've worked when staff are submitting bug reports.
The thing that you're trying to achieve is implementing some form of analytics so you can determine how many people are visiting your site, how many of them are unique visitors, and also being able to see realtime data of who is currently using the site. So a better title would be something like "How can I get information about who is visiting my site?".
Storing ip addresses in a table is a valid way of achieving this, but is far from ideal. I'd highly suggest using Google Analytics (or something along those lines) as this will enable you to see all of this information and more, plus it won't require very much effort on your part. You don't even have to be a coder - you just copy and paste the snippet they provide into your HEAD tag. There is plenty of information out there about how to do this so I won't go into detail, but don't hesitate to ask if you're having any problems.
Related Topics
Adding Subscribers to a List Using Mailchimp's API V3
Deleting a File After User Download It
How to Build Unlimited Level of Menu Through PHP and MySQL
Download CSV File Using "Ajax"
Pdo Fetchall Array to One Dimensional
Tcpdf Utf-8 Symbols Not Showing Up
Check If Specific Input File Is Empty
Mysqli Binding Params Using Call_User_Func_Array
Handling Big User Ids Returned by Fql in PHP
Interleaving Multiple Arrays into a Single Array
Do Ajax Requests Retain PHP Session Info
PHP Errors Not Being Displayed in the Browser [Ubuntu 10.10]
In PHP, How to Detect the Execution Is from Cli Mode or Through Browser
How to Convert Word Smart Quotes and Em Dashes in a String
How to Test If a User Has Selected a File to Upload