Question

please look at the simple php webpage code below

how can modify my code so that a log file is created on my server which logs each visitor's ip and whether it was successfully redirected to the correct page . something like that.

   <?php

$a = $_SERVER['REMOTE_ADDR'];
if ( $a == "117.96.112.122" )
{
        header("Location: ad_sams_mobile.html");
        return true;
}
else
{
        header("Location: ad_other_mobile.html");
        return true;
}

?>
Was it helpful?

Solution

See the PHP function file_put_contents. You'll need to use the append flag:

file_put_contents("log.txt", "IP: ". $a .", Location: ad_other_mobile.html", FILE_APPEND);

OTHER TIPS

The Apache access.log should have all the information you need.

All you have to do is parse it.

Something like this:

$logfile = 'redirect.log';
$handle = @fopen($logfile, "a");

$a = $_SERVER['REMOTE_ADDR'];

if ( $a == "117.96.112.122" )
{
    $redirect_loc = 'ad_sams_mobile.html';
    header("Location: {$redirect_loc}");
 }
else
{
    $redirect_loc = 'ad_other_mobile.html';
    header("Location: {$redirect_loc}");
}

if ($handle && is_writable($logfile))
{
    $log = "{$a} -> {$redirect_loc}\n";
    fwrite($handle, $log);
    fclose($handle);
}
return true; // you always return true so just put it at the end

Or this for IP logging:

<?php
$file = fopen("log.html", "a");

$time = date("H:i dS F");
fwrite($file, "<b>Time:</b> $time<br/>" );

if( $REMOTE_ADDR != null)
{
fwrite($file,"<b>IP address:</b> $REMOTE_ADDR<br/>");
}

if( $HTTP_REFERER != null)
{
fwrite($file,"<b>Referer:</b> $HTTP_REFERER<br/>");
}

fwrite($file,"<b>Browser:</b> $HTTP_USER_AGENT<hr/>");

fclose($file)

?> 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top