MrUserStack MrUserStack - 4 months ago 21
PHP Question

Meta Refresh IP Address Redirect Once Every 3 IP Addresses

I'm now coming to StackOverflow after trying several forums and searching through at least 80 questions on Stack Overflow for some sort of answer, so please do not mark this as spam.

I am looking to have a META REFRESH redirect activate only once per IP Address, and of this to be one in 3 users.

So every 1 in 3 users is redirected using the META REFRESH, but those IP Addresses would never be redirected ever again.

Is this sort of script possible?

Answer

You will want to break this up into tasks, but it is doable. Some words of warning: IP address can change or be shared (think University) but that may be fine for whatever you goal is.

  1. Check if IP address has been redirected before
  2. Use random check to determine if it is the one in three
  3. If it is log it somehow. This example logs it to a file.

I left some stuff in here commented out that is useful for debugging:

// print_r($_SERVER);
$ineligibleIP=file_get_contents('reddit.txt');
//echo $ineligibleIP;
//echo $_SERVER['REMOTE_ADDR'];
if (strpos($ineligibleIP,$_SERVER['REMOTE_ADDR']) === false) {
    //echo 'Eligible';
    $test=rand(1,3);
    // echo $test;
    if($test==1) {
        $ineligibleIP.=$_SERVER['REMOTE_ADDR']."\n";
        file_put_contents('reddit.txt', $ineligibleIP);
        header('Location: https://www.google.com');
    }
}
echo "Yay, not redirected.";