doulierong0334 2016-11-10 08:13 采纳率: 100%
浏览 62
已采纳

独特的IP访问重定向在PHP没有mysql

i'm sorry if my request seems silly . i've looked for a long time for this but no luck. basically what i'm trying to do is : i want when a visitor visits my link'http:/ /mysite. com/redirect .php' , my php script gets his ip address, checks if it exists in an array of ips stored in a file 'hits.txt', if it does then redirect him to another page say'google.com' if it doesn't then store his ip address in the file then redirect him to another page say 'yahoo.com'. so later when he comes back to visit again he gets redirected to google.com. ofcourse my purpose eventually is to make a unique ip visits script. if you have an idea how to do it without database and sql i'll be grateful, if u think it can only be done with sql then please suggest me the easiest way. my code so far but it doesn't work :

<?php
// Unique Hits PHP Script
// ----------- March 2004
// Contact author: uniquehits@sizzly.com

$log = 'hits.txt';

$IP = getenv (REMOTE_ADDR);
$add = true;
$hits = 0;

if (!file_exists ($log)) {
    echo "Error: $log does not exist.";
    exit;
}

$h = fopen ($log, 'r');



    if (in_array($IP, array($h))){


        header("Location: http://google.com");
    }
        else{
      $fp = fopen('hits.txt', 'a');
        fwrite($fp, "'" );
fwrite($fp, $IP );
fwrite($fp, "'" );
fwrite($fp, ',' );
fclose($fp);


        header("Location: http://yahoo.com");

        }





fclose($h);
?>

appreciated and thankful to you guys .

  • 写回答

2条回答 默认 最新

  • drws65968272 2016-11-10 08:56
    关注

    You could use cookies for this. It's probably a more reliable way than using IP adress, since a lot of people have a dynamic IP anyway.

    http://php.net/manual/en/features.cookies.php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?