Welcome Guest, Not a member yet? Register   Sign In
Dynamic Base URL
#1

[eluser]Mr Lazy[/eluser]
Hi,

I have a site that has the requirement that it is accessed using one IP address from inside my company, and the exactly the same site available externally with a different IP address. So the client's IP addresses needs to be inspected, so I am using a rather cool function called 'ip_in_range' written by Paul Gregg (http://www.pgregg.com/projects/php/ip_in_range/) to identify where the client is... and now the bit I could use some advice on.

I have dynamically changed the base URL in config.php using the result of the ip_in_ range function and the function code is currently sitting at the bottom of the config file. my question is, where should the logic and the function code sit? This is a quick and dirty solution that does work, but I don't know the best place for it.

Anyone got any suggestions?

Thanks,
L
#2

[eluser]rogierb[/eluser]
Do you have 2 IP adresses or more? I really don't see the need to use ip_in_range().

Why not simply use
Code:
$config['base_url']     = "http://".$_SERVER['SERVER_NAME']."/";

This way you don't need to worry what IP's you use.
#3

[eluser]Mr Lazy[/eluser]
Hi,

Maybe my description needs filling out.. one server, and one instance of the application, but the server has two IP addresses. One is inside the LAN, and one outside (don't ask me the how and why's, I have no idea.. it has been decreed by the network admin).

But I need to check a range of client IP addresses, because of course all the clients inside the LAN do not share the same IP address. This is why I need to use the function to check an IP range.

The logic in the config file looks like this:

Code:
// Loop over the different ranges for internal IP addresses and compare
// the client's IP address against them. If the client's IP is within
// one of the ranges, change the base URL to include the internal server
// IP address.
$internal_client=0;
foreach ($checkips as $range) {
  $ok = ip_in_range($ip, $range);
  if($ok){
    // The client has an internal IP address.
    $internal_client=1;
  }
}

if($internal_client == 1){
  $config['base_url'] = "http://".$server_internal_ip;
} else {
  $config['base_url'] = "http://".$server_external_ip;
}

$config['base_url'] .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);

I have less concern about the logic, but rather where it should be within the application..

Thanks,
L
#4

[eluser]rogierb[/eluser]
yeah, that makes sense.

I would put in in index.php and parse the IP as a contant. It has to be done on every page, so better to do it as soon as possible. (You might want to store it in the session, don't know what is faster)

Code:
DEFINE('THE_IP', $server_internal_ip);

//in config.php
if(defined('THE_IP')) $config['base_url'] = "http://".THE_IP;
else $config['base_url'] = "http://".$_SERVER['SERVER_NAME'];
#5

[eluser]Phil Sturgeon[/eluser]
I've been using this on PyroCMS and it works with pretty much anything short of Apache's userdir http://ip/~user/ style addresses.

Code:
// Base URL (keeps this crazy sh*t out of the config.php
if(isset($_SERVER['HTTP_HOST']))
{
    $base_url = isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on' ? 'https' : 'http';
    $base_url .= '://'. $_SERVER['HTTP_HOST'];
}

else
{
    $base_url = 'http://localhost/';
}

$base_url .= str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']);
define('BASE_URL', $base_url);

I use that to define a constant in constants.php then use the constant in config.php to keep my crazy code out of the main config file.
#6

[eluser]Mr Lazy[/eluser]
OK, still not sure where to put the IP range checking function though.. maybe I could change the base URL on the fly in a parent controller constructor and have the function in there.. I might give that a go...
I just don't like having a function sitting in what is supposed to be a config file.

Thanks for the suggestions.

L




Theme © iAndrew 2016 - Forum software by © MyBB