Save the IP address on the website

instagram viewer

If you want to save the IP addresses of visitors to your website, you can do this either quickly via a text file or - much more professionally and more securely - via an SQL database. You can find out here which data protection principles you have to observe and how you can use both procedures.

As a website operator, observe data protection restrictions

Even if the storage of the IP addresses of website visitors for analysis purposes is widespread, this may not be done in principle.

  • In terms of data protection law, IP addresses are regarded as personal data, as it is possible to assign them directly to a specific person.
  • Personal data may only be collected, i.e. stored and processed, if the relevant Person has expressly consented to it - a note in the data protection declaration is therefore not sufficient the end.
  • Rather, the visitor must be informed of this before the IP address is saved, for example via an entry page or a login system.
  • In theory, IP addresses can be saved when you log in to your website required and the user gave his consent to the processing of the data when registering Has.
  • Chat without Java - this is how it works on your homepage

    With a chat function you can add a useful option to your homepage, ...

Save IP addresses in a file

The fastest way to save IP addresses is to write them to a text file on your web server with PHP.

  1. To do this, create a text file (for example "ip.txt") and upload it to the web server of your website.
  2. Give the file write permissions by either setting this via the admin management of your website or by changing the write permissions via an FTP client.
  3. Then open the file with which the IP address is to be saved when the page is called up (for example "index.php").
  4. Enter the following code in the first line:
    $ ip = fopen ('ip.txt', 'a');
    fwrite ($ ip, $ _SERVER ['REMOTE_ADDR']. " | ". date ("d.m. Y H: i"). "\ n");
    fclose ($ ip);
    ?>
  5. With "fopen" you open the file, with "fwrite" you write the IP address ("$ _SERVER ['REMOTE_ADDR']") and the visit date "date (" d.m. Y H: i ")" in the file and generate a line break ("\ n"). With "fclose" you close the file again.
  6. Save the changed file and, if necessary, upload it to your web server.

Although this is the fastest way to save IP addresses, evaluating the data is relatively difficult and can very quickly lead to a large file for many visitors. The file can also be easily accessed by third parties. It is therefore advisable to save the IP address in an SQL database.

Store IP addresses in a database

To save the IP addresses in an SQL database, you must first create a table and then enter the values ​​using PHP.

  1. To do this, log in to the graphical user interface of your SQL database system (for example "phpmyadmin").
  2. Then create a new table. You can either do this manually or use the following SQL code, for example:
    CREATE TABLE visitor (
    ID INT AUTO_INCREMENT PRIMARY KEY,
    ip VARCHAR (15) NOT NULL,
    date DATETIME NOT NULL
    );
    This creates the table "visitors" with the columns "ID", "ip" and "date" and assigns the columns the format "INT" for numbers, "VARCHAR" for strings and "DATETIME" for the date incl. the time too. "PRIMARY KEY" means that the number must not be used twice, while "AUTO_INCREMENT" ensures that a new number is created continuously.
  3. Then open the file with which you want to save the IP addresses.
  4. Then you have to connect to the database. For this you need the computer on which the SQL server is installed (this is usually "localhost") as well as your username and password for the database and the particular database you are using to be allowed to.
  5. At the beginning of your file, enter the following code:
    $ host = "localhost";
    $ user = "user";
    $ pw = "password";
    $ db = "database name";
    $ con = mysql_connect ($ host, $ user, $ pw);
    mysql_select_db ($ db, $ con);
    First you define the login variables and then you connect to the database in order to be able to save or retrieve data.
  6. Then enter the following lines:
    $ ip = $ _SERVER ['REMOTE_ADDR'];
    $ query = mysql_query ("INSERT INTO visitor (ip, date) VALUES (“. $ ip. ”, NOW ())”);
    ?>
    In the first line you save the IP address in a variable in order to then save it in the second line as a new line in the database. "INSERT INTO visitors" means that new values ​​will be inserted into the table "visitors". "(ip, date)" are the columns for the values ​​and "VALUES" assigns the values, ie "$ ip" for the IP address and "NOW ()" for the current date and time.

The data is then saved in the "visitors" table and you can view it on the graphical user interface or output it dynamically with another PHP file.

click fraud protection