php - 访问页面时重定向特定的 IP 地址

标签 php

任何人都可以帮我处理这段代码吗?我正在尝试在访问页面时重定向特定的 IP 地址(存在于 ips.txt 中)。

代码:

<?php
ob_start(); //first line

// get the visitor ip.
if (!empty($_SERVER['HTTP_CLIENT_IP']))   //check ip from share internet
    {
      $ipaddress = $_SERVER['HTTP_CLIENT_IP']."\r\n";
    }
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))   //to check if ip is pass from proxy
    {
      $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR']."\r\n";
    }
else
    {
      $ipaddress = $_SERVER['REMOTE_ADDR']."\r\n";
    }


// check if the visitor ip exists in the txt file
$s = file_get_contents('ips.txt');
$pos = strpbrk($ipaddress, $s);

if ($pos === false) {
    // If it doesn't exist, record the IP in the txt file and then switch to an external page
    $ips = fopen("ips.txt", "a");
    fwrite($ips, $ipaddress . "\n");
    header("Location: https://google.com/");
} else {
    // If it exists, do nothing or switch to an external page
    header("Location: https://example.com"); 
}
?>

最佳答案

$s = file_get_contents("ips.txt");
if(strpos($ipaddress, $s) !== false){
// IP exists
}

关于php - 访问页面时重定向特定的 IP 地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49622179/

相关文章:

php - 更新彼此链接的三个表时出现问题

php - 编码问题?

javascript - 如何使用 php 发布数组并循环遍历它?

php - __autoload() 没有被调用?

php - 带有 PHP 变量的 mysql_query

javascript - 如何为数据库中找到的每一行/记录生成链接,以用作在单击时填充表单的方式?

php - 阻止 Laravel 观察者事件的 Action

javascript - 为什么我的对话框不起作用?

PHP关联数组解析错误仅影响某些机器

php - 无法在 PHP 中检索 mp3 持续时间标签 (TLE/TLEN)