Search code examples
phparraysipuniquevisitor-pattern

unique ip visit redirect in php without 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: [email protected]

$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 .


Solution

  • 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