如何修改此代码以将 IP 地址记录到另一个文件

发布于 2024-08-08 19:00:06 字数 355 浏览 7 评论 0原文

请查看下面的简单 php 网页代码,

如何修改我的代码,以便在我的服务器上创建一个日志文件,记录每个访问者的 ip 以及是否成功重定向到正确的页面。类似的事情。

   <?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;
}

?>

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;
}

?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

相守太难 2024-08-15 19:00:06

请参阅 PHP 函数 file_put_contents。您需要使用附加标志:

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

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);
左秋 2024-08-15 19:00:06

Apache access.log 应该包含您需要的所有信息。

您所要做的就是解析它。

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

All you have to do is parse it.

偏闹i 2024-08-15 19:00:06

像这样的东西:

$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

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
怎言笑 2024-08-15 19:00:06

或者 IP 日志记录:

<?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)

?> 

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)

?> 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文