Skip to content

Instantly share code, notes, and snippets.

@jwne
Forked from pykotechguy/logger.php
Created March 15, 2016 15:13
Show Gist options
  • Save jwne/dff2cb701d5ce89f21aa to your computer and use it in GitHub Desktop.
Save jwne/dff2cb701d5ce89f21aa to your computer and use it in GitHub Desktop.
sample ip address logging class
<?php
class Logger {
public $db;
public function __construct() {
$this->db = $this->_conn('localhost','root','root','stats');
}
private function _conn($dbhost, $user,$pwd, $db) {
try {
return mysqli_connect($dbhost,$user,$pwd,$db);
} catch( Exception $e) {
die ('Could not connect to DB ' . $e->getMessage());
}
}
public function logvisit($servr = 'undefined') {
$remoteaddr = $_SERVER['REMOTE_ADDR'];
$details = json_decode(file_get_contents("http://ipinfo.io/{$remoteaddr}")); //free API to get country
$sql = "INSERT INTO logs(VisIP, VisRef, VisUrl, VisDate, VisAgent, VisCntry) VALUES (\"".$remoteaddr."\", \"".$servr."\", \"".$_SERVER['REQUEST_URI']."\", NOW(), \"".$_SERVER['HTTP_USER_AGENT']."\",\"". $details->country ."\")";
mysqli_query($this->db, $sql);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment