php - Record current webpage to file
Solution:
Use:
basename($_SERVER['PHP_SELF']);
to get (log) the name of the current PHP file
So, for example, take a project structure with two pages index.php and contact.php and your logger file log.php:
/Root
- index.php
- contact.php
- log.php
You need to require 'log.php' in index.php and contact.php to have access to function logIP().
Since you want to log the visited page, function logIP() must be fed with the current page as a parameter:
File: log.php
function logIP(string $page = '')
{
date_default_timezone_set('Australia/Brisbane');
$ipLog = "logfile.txt";
$register_globals = (bool)ini_get('register_gobals');
if ($register_globals) $ip = getenv(REMOTE_ADDR); else $ip = $_SERVER['REMOTE_ADDR'];
$date = date("F j, Y, g:i a");
$log = fopen("$ipLog", "a+");
if (preg_match("/\\bhtm\\b/i", $ipLog) || preg_match("/\\bhtml\\b/i", $ipLog)) {
fputs($log, "$ip - $date<br>");
if($page !== '') fputs($log, "$page<br>"); // <
I.e. file index.php and contact.php would contain:
<?php
require 'log.php';
logIP(basename($_SERVER['PHP_SELF']));
Now everytime you visit these pages, they are logged in file {-code-16}