473,789 Members | 2,431 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to get and store visitors IP address in a MySQL database.

1 New Member
Hello there,

I have a PHP-based webpage using MySql. I want to store the IP-adresses of the visitors into DB and when it's a new visitor (ip) the start page will show an extra image (or pop up) that will never be shown again for the same IP.

Please help me, I've got the idea but not the code.
Sep 28 '07 #1
6 6830
Atli
5,058 Recognized Expert Expert
Hi, and welcome to The Scripts!

I've changed the title of this thread to better describe it's topic.
Using good, descriptive titles that follow the Posting Guidelines will increase your chances of getting you questions answered!

As for you question...

You can get the IP address for you visitors through the $_SERVER super-global:
Expand|Select|Wrap|Line Numbers
  1. $ipAddress = $_SERVER['REMOTE_ADDR'];
  2. echo "Your IP is : $ipAddress";
  3.  
To insert it into a MySQL database you can use the MySQL Functions, or better yet, the Improved MySQL Functions.
If you don't know how to use MySQL, check out this article.

I'm not sure, however, that storing the IP address would be the best way to go about this. Many ISP's keep changing their customer's IP addresses and there are ways in which a use can change his own IP address. Proxy servers can also cause problems if you rely much on using IP addresses.

You should think about using Cookies
Sep 29 '07 #2
anvesh
2 New Member
As per my knowledge you have to store ip address into different part of integer column. String operation is always against database performance. Simply, you can divide your full length of ip address into different small four part.

In future if analysis is require then also you can easily perform using different columns. I have already created full detailed explanation into my blog. please visit this url, this is really very interesting theory on storage of IP-Addresses.
Aug 21 '15 #3
RonB
589 Recognized Expert Moderator Contributor
Why are you posting to an 8yr old thread? The OP has not been back since posting that question.

Also, on a side note, your blog post recommending splitting and storing the IP in 4 separate tinyint fields is just as bad, if not worse, as storing it in a single varchar.

The best storage method is a single unsigned int field and use mysql's inet_aton() and inet_ntoa() functions as needed when converting.
Aug 21 '15 #4
Atli
5,058 Recognized Expert Expert
I agree with you, RonB, on the 4x tinyint fields. I can see no practical reason for doing that.

However, while storing it as a uint vs string is better as far as storage space goes, in practice storing it as a string has some advantages.
  • Generally IP addresses are used in their human-readable format, so storing it as a uint will require a great deal of conversion back from the uint format. That adds code complexity and a performance penalty (albeit a very small one.)
  • Storing it in a string format can simplify searching for IP patterns, by simply doing: WHERE ip LIKE '192.168.%'. Stored as a uint, the same is of course possible, but again it adds unnecessary complexity.

Also, the uint storage space advantage isn't a major concern in most situations. Not unless you're storing a huge amount of data does it actually matter. The rule of thumb is to prioritize simplicity over optimization. No need to overcomplicate a situation for a negligible gain.

I would always suggest storing as a string unless you have a compelling reason not to.
Aug 23 '15 #5
RonB
589 Recognized Expert Moderator Contributor
I'd say that both types of storage have their advantages and disadvantages.

Lets take your 2 points.

1) Mysql has built-in functions to handle the conversion which removes the complexity issue and if there is a performance issue, which I have not experienced, it would be negligible. If that conversion was being done outside of mysql, then the complexity and performance issue would be a concern.

2) Storing it as a string and using a LIKE clause only works well when dealing with class full addresses (i.e., CIDR /8, /16, and /24). If you're working with any other subnet range, then the string approach is more difficult and cumbersome. Storing as an INT, I can easily deal with any range.

When stored as strings, how would you select the IP's in this network? 10.100.0.0/21

edit: I should clarify that question. How would you do it without having a group of OR LIKE clauses (1 for each /24 segment)?

In my case I'm dealing with hundreds of /21, /22, and 27 networks and storing IP's as strings would add a lot of complications. Storing them as INT added simplicity in my case.
Aug 23 '15 #6
Atli
5,058 Recognized Expert Expert
I agree, that each has their advantages.

1) Whether the conversion is done in MySQL or the actual code isn't really the point. It'll just be adding the complexity on another layer. For straight up SQL queries it's not a big deal but consider, for example, if you are building this on top of a framework that uses an ORM, or some other type of DBAL. (Which is fairly standard in the enterprise space.) In that scenario, calling a MySQL function like that on a field during retrieval will most likely require a great deal of unnecessary clutter.

2) As for you /21 example, I'll also agree that in that case you're probably better of with an uint. However that is not a common scenario; it's an edge case. I doubt 99.9% of sites that record IP addresses - for whatever reason - will ever need to do a more complicated search than a straight up boolean and/or LIKE comparison can provide.

My main point here is: it's generally best to start with as simple a solution as you can get away with, and only add complexity as it becomes necessary. In my experience, when you do something more complex than the current situation requires, it ends up being a wasted effort.
Aug 24 '15 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

5
3375
by: lawrence k | last post by:
I'm a little weak on my basic I/O. Help me out please. Is it right to say that I can just open any file with file(), get it as a string, and then store in a MySql database, in, say, a MediumText field? I realize that MySql supports binary fields, but my current MySql schema does not have any binary fields, and I'm wondering if I can store the file without changing my current schema. Can I recreate the file later, if I wish, just be...
7
17895
by: mukeshpatil | last post by:
How to get data from .CSV file using php & store it into Mysql database I want to read data from two different .CSV files using php & How to store it into Mysql Database tables Please help me to do this.
14
6143
by: mistral | last post by:
Need php script to create mySQL database programmatically; since hosting configuration may not allow create database from script, script also need eliminate/rewrite all restrictions in appropriate places in that hosting.
7
7462
by: kiranrredy | last post by:
Hi, I need to get the data from XML file , to store it in MySql database. How can i do it. Regards, KiranRredy
8
12235
by: hemashiki | last post by:
hi i need help..how can i store image in mysql database and i want to retrive that image can anyone suggest plz
1
2551
by: berry | last post by:
Hi all, I am scratching my head on how to store image that user upload to the MySQL database in vb6? Please HELP me!! Thank you very much..
2
1774
by: Eminosoft | last post by:
I am new to the php. plz tell that code for "how to store image in mysql database and also retrive that image"
2
2068
by: maheshonmail | last post by:
i want to store pictures in mysql database using php code/ so please send me the most efficient code for doing this.
1
4162
by: twinkle2010 | last post by:
Hi I am a new web programmer.I got an issue that reads the excel sheet data and store it to MYSQL database using php. I dont have any idea on that. can you help me?
2
3394
ilya Kraft
by: ilya Kraft | last post by:
Hello, I came across following problem. I need to sort content by categories on my web page. Just like in bytes.com. User chooses area that is related to his / her question (PHP, JavaScript, HTML etc.) So when someone clicks on PHP section He / She only sees Posts related to Selected category (PHP Category). I need to make something similar for my website, where user enters his content and than selects option. So selected option than get's...
0
9506
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10404
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10136
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9979
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9016
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7525
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5415
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5548
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2906
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.