Created
September 20, 2014 16:58
-
-
Save pykotechguy/8d208e234adbcea999f0 to your computer and use it in GitHub Desktop.
sample ip address logging class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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