0

hi, i've got a problem,
hmm...there are three groups (A,B, and C) and their IP address are:
A ==> 10.204.xxx.xxx
B ==> 10.205.xxx.xxx
C ==> 10.206.xxx.xxx

how to read an IP Address, but only for 2 octet, (10.204, 10.205, 10.206)?
I want to put them on index.php, so:
if user IP come from 10.204.xxx.xxx, it will directing to: www.portalA.com,
10.205.xxx.xxx www.portalB.com

2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by danang
1

hi, i've got a problem,
hmm...there are three groups (A,B, and C) and their IP address are:
A ==> 10.204.xxx.xxx
B ==> 10.205.xxx.xxx
C ==> 10.206.xxx.xxx

how to read an IP Address, but only for 2 octet, (10.204, 10.205, 10.206)?
I want to put them on index.php, so:
if user IP come from 10.204.xxx.xxx, it will directing to: www.portalA.com,
10.205.xxx.xxx www.portalB.com

The following will work so long as there's no proxy in the way. If there is, you'll need to first check whether $_SERVER["X_FORWARDED_FOR"] exists, and if so, assign $ip that value, otherwise assign S_SERVER["REMOTE_ADDR"].

Not all proxies forward on the original IP address, in which case it will be impossible to tell the real IP address.

However, if you're on a LAN or there's no proxy, the following will work:

<?php

$ip = $_SERVER["SERVER_ADDR"];
$twoOctets = substr($ip,0,strpos($ip,".",strpos($ip,".")+1));
switch ($twoOctets) {
  case "10.204":
  header("Location: http://www.portalA.com");
  exit;
  break;
  case "10.205":
  header("Location: http://www.portalB.com");
  exit;
  break;
  case "10.206":
  header("Location: http://www.portalC.com");
  exit;
  default:
  // put code here to cover no match
}

?>

Edited by edwinhermann: n/a

Votes + Comments
solved my problem
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.