0

I want to implement something on my website to record the visitor's IP address and put it into a database with a unique id for support and analytics.
Does anyone know of a way to do that, with out changing my pages to PHP, or making the process to crazy to do.

Thanks in advance,
Peter
I will use PHP if needed. Any answer is helpful

Peter
  • 141
  • 2
  • 11

3 Answers3

1

This cannot be done without PHP, if you do decide to do it with PHP, then simply create a table with an id which is a primary key and which auto_increment, and an IP which is varchar(15) and unique

Then in your index, or whatever page you want to record it with, simply add this

$link = new mysqli('host', 'username', 'password', 'database');
$stmt = $mysqli->prepare("INSERT INTO `TABLE_NAME` (`ip`) VALUES (?)");
$stmt->bind_param('s', $_SERVER['REMOTE_ADDR']);
$stmt->execute();
Ali
  • 3,479
  • 4
  • 16
  • 31
1

You can without having to change the page to a dynamic page, but in the end you need some dynamic web page or web service to write to a database. That dynamic page can be on another website even, but you can not write to a database or file from a static page (not even via javascript).

In your static page you can add two javascript functions: First get the IP-address using pure Javascript. See: How to get client's IP address using javascript only?

Then make an ajax call (again from javascript) to a service you create and host somewhere else. Send the ip address to that service and log it.

Hope this helps

Community
  • 1
  • 1
Thor
  • 291
  • 2
  • 8
-1

You can't do this without PHP.

Make a table named "iplogs" in your database with one field called "id", and define it as a auto-increment
Add another field to the table called "ip" as a text field.

php:

<?php
    $username = "root";
    $password = "";
    $hostname = "localhost"; 
    $ip_user = $_SERVER['REMOTE_ADDR'] 

    //connect to database
    $dbhandle = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL");

    //Select database
    $dbname = mysql_select_db($db_name, $dbhandle) or die("Cannot find the database");

    //execute the SQL INSERT query
    mysql_query("INSERT INTO iplogs (ip) VALUES (" . $ip_user . ")");

    //close the connection
    mysql_close($dbhandle);
?>
Cœur
  • 37,241
  • 25
  • 195
  • 267
Thom
  • 591
  • 4
  • 12
  • 30