Stan van der Avoird
Stan van der Avoird

Reputation: 197

PHP IPv6 and IPv4 Addresses specific access to webpage

I want to restrict access to users who are not connected to one of the WIFI's of a specific company. So for instance, we have a company and this company has 2 different WIFI networks to connect. I want to only give access to a page if the client is connected by one of these WIFI's.

The following PHP script worked perfectly in time there was only a IPv4 address:

if(!in_array($_SERVER['REMOTE_ADDR'], $ip_adres_allow) && !in_array($_SERVER["HTTP_X_FORWARDED_FOR"], $ip_adres_allow)) {

    die("You can't access this webpage, please try again by your employee.");

    exit();
}

Where $ip_adres_allow -> array of values stored in database

My questions are:

Please correct me if im wrong.

Thanks for reading.

Upvotes: 0

Views: 747

Answers (1)

Ronnie Oosting
Ronnie Oosting

Reputation: 1252

As we discussed in our chat, below is a sample code to give you an idea on how to perform your checks.:

$checkip  = $_SERVER['Something with IP'];
$validate = '';

if($checkip == $_SERVER['Some IPv4'])
{
    $ip = the IPv4 address
    $validate = 4; 
}
else
{
    $ip = the ipv6 IP 
    $validate = 6; 
}

if($validate == 4)
{
    $ip = echo substr($ip, 0, ???); // IPv4 address
}
else
{
    $ip = substr($ip, 0, 15); // 2000:950:ac2:3 
}

Again: this is just to give you an idea. You need to create your own function to use this correctly. I suggest to make this a function at the login, and give variables to your function. So you do not have to modify your script for other companies, only the variables your are sending. (function could simply return true or false;

Good luck man!

Upvotes: -1

Related Questions