Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jmcausing/946b433e6206b3155d9f3350cc77ae2c to your computer and use it in GitHub Desktop.
Save jmcausing/946b433e6206b3155d9f3350cc77ae2c to your computer and use it in GitHub Desktop.
access log sample.
add_action( 'init', 'start_logging' );
function start_logging() {
// GET IP
if ( ! empty( $_SERVER['HTTP_CLIENT_IP'] ) ) {
//check ip from share internet
$ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif ( ! empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) {
//to check ip is pass from proxy
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$ip = $_SERVER['REMOTE_ADDR'];
}
// get user agent
$user_agent = $_SERVER['HTTP_USER_AGENT'];
// get current path
$pagePath = parse_url( $_SERVER['REQUEST_URI'] ); // returns an array. ex: 'path' => string '/new/' (length=5)
$pagePath = $pagePath['path']; // returns the path. ex: /wp-admin/themes.php
// $pagePath = substr($pagePath, 1, -1);//remove slashes
echo "Your IP: " . $ip . "<br>";
echo "User agent: " . $user_agent . "<br>";
echo "Your current path: " . $pagePath . "<br>";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment