Hi , How to make IP logs for who logging on Ex : 0313 account

Hi , How to make IP logs for who logging on Ex : 0313 account . To see what ip’s who logged in to that account

Hi
It is a homemade forum ?

The easiest thing for you is to look at (source) how some of the more famous forum engines done it

Is not clear to me what this represents Ex : 0313 account

the forum should assign an ID to each forum member
and then…let say after a successful login to a specific ID
you should look at and capture its IP and store it in the DB under the appropriate user ID
and then retrieve that data from the DB during the search or whatever.

4 Likes

onthe forum landing page use this script

<?php
include 'getipaddress.php';
?>

create file getipaddress.php

Insert this code

function getUserIpAddr(){
    if(!empty($_SERVER['HTTP_CLIENT_IP'])){
        //ip from share internet
        $ip = $_SERVER['HTTP_CLIENT_IP'];
    }elseif(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
        //ip pass from proxy
        $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    }else{
        $ip = $_SERVER['REMOTE_ADDR'];
    }
    return $ip;
}

$ipadd =  ''.getUserIpAddr();


$servername = "localhost";  //add host name when db setup
$username = "username"; //add username when db setup
$password = "userpassword"; //add userpassword when db setup
$dbname = "databasename";  //add databasename when db setup

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
   
}


$query = "SELECT * FROM data WHERE ipaddress = '$ipadd'";
$result = $conn->query($query);

if ($result->num_rows > 0) {
 while($row = $result->fetch_assoc()) {
    $add = $row["visits"]+1;	
		
	$addvisit = "UPDATE ipaddress SET visits = '$add' WHERE ipaddress = '$ipadd' ";

	if ($conn->query($addvisit) === TRUE) {
 
} else {
  echo "Error: " . $sql . "<br>" . $conn->error;
	}
 }
}
else
{	

$sql = "INSERT INTO ipaddress (ipaddress, visits)


VALUES ('" . $ipadd . "', 1)";

if ($conn->query($sql) === TRUE) {
 
} else {
  echo "Error: " . $sql . "<br>" . $conn->error;
}
}
$conn->close();
header("Location: forumlandingpage.php");  // change to your forum landing page
?>

create a database table called ipaddress and add tables id, visits and ipaddress

SQL

CREATE TABLE `ipaddress` (
  `id` int(6) UNSIGNED NOT NULL,
  `ipaddress` varchar(30) NOT NULL,
  `visits` varchar(30) NOT NULL,
  `ipaddress` varchar(50) DEFAULT NULL,  
  `reg_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

let me know any faults as not tested

2 Likes