Jump to content

Safe IP


Recommended Posts

i wanna save ip in database by number format ,can suggest me string is better way or number ?

like this

function vaild_ip($ip)


$ip = trim($ip);

if (filter_var($ip, FILTER_VALIDATE_IP)) {

$ip = sprintf('%d', ip2long($ip));

return $ip;



$ip = vaild_ip($_SERVER['REMOTE_ADDR']);

Link to comment
Share on other sites

You could save the results of ip2long into your database then when you pull it out again run the data through long2ip.

You also have the option to INET_NTOA to convert the IP to and from an int, then you can store it into an int unsigned. Check out the following stack overflow:


Link to comment
Share on other sites

filter validate IP validates the IP


Returns the filtered data, or FALSE if the filter fails.

So if it's false the IP is malformed/malicious.

It has to be a string as a number wouldn't allow dots (.) I think

EDIT: Dave beat me >.<

Link to comment
Share on other sites

i am using PDO then ?


PDO provides a data-access abstraction layer, which means that, regardless of which database you're using, you use the same functions to issue queries and fetch data. PDO does not provide a database abstraction; it doesn't rewrite SQL or emulate missing features. You should use a full-blown abstraction layer if you need that facility.
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...