Jump to content

Problem with "If IP equals wildcard"


Icebergness

Recommended Posts

Hi,

 

I have a page on my Intranet which lists a bunch of install files (for example, Microsoft Office), which are stored in the same directory structure as the site itself. I also have a duplicate installs folder structure in another office, on their local server. I want to make it so that the link changes, depending on which office the user is in. To simplify:

 

If (user is in London)

display London link

else

display Blackpool link

 

I've been trying to make it work via IP Address. The IP range in London is 10.44.6.* However, I can't make the script work with a wildcard (it works fine if I put a specific address in). The code I have so far is:

 

<?php
$ip=$_SERVER['REMOTE_ADDR'];
if ($ip == '10.44.6.*') {
?><a href="link1.php">Click Here</a><?php
} else {
?><a href="link2.php">Click Here</a><?php
};
?>

 

I think it may be an obvious solution, but if some kind person could perhaps point out my mistakes?

 

Thanks,

Dave

Link to comment
Share on other sites

There's no such thing as a wildcard in IP an address. You have to write code to figure out what you want to do with the address.

 

To do this properly, you'd need to write code that would allow you to enter an IP address a network address and a subnet mask, then perform a binary AND operation to see if the given IP address falls within the range of network addresses defined by the mask.

Link to comment
Share on other sites

Use the netmask to and ip2long to compare.

$offices = array(
     array('mask' => 0xFFFFFF00, 'base' => ip2long('10.44.6.0')
    //...
);
$ip = ip2long('10.44.6.56'); //whatever client's IP is

foreach ($offices as $off){
   if (($ip&$off['mask']) == $off['base']){
     //it's this one
  }
}

 

Link to comment
Share on other sites

in this case you could remove everything after the last . in $ip giving 10.44.6.

 

then this would work

 

if ($ip == '10.44.6.')

 

this won't work, because for instance, '10.44.6.' does not equal '10.44.6.1'.  You use something like strpos() instead.  Alternatively you can use regex to match it, if you want to match ranges. 

 

but i wasn't sugesting comparing '10.44.6.' with '10.44.6.1'

I was suggesting that if you take $ip, remove anything after the last '.'

you will then effectively be comparing  '10.44.6.' with '10.44.6' no matter what the last part of the address is

 

'10.44.6.' does equal '10.44.6.'

 

there are obviously othe similar approaches, the point was that the check could be made without involving wildcards or netmasks etc

 

 

Link to comment
Share on other sites

in this case you could remove everything after the last . in $ip giving 10.44.6.

 

then this would work

 

if ($ip == '10.44.6.')

 

this won't work, because for instance, '10.44.6.' does not equal '10.44.6.1'.  You use something like strpos instead.  Alternatively you can use regex to match it, if you want to match ranges.

 

Hi guys,

 

I went with the strpos idea which worked perfectly. I put this code in the header:

 

<?php $ip = $_SERVER['REMOTE_ADDR'];
$london = '10.44.';
$office = strpos($ip, $london);
$blackpool_path = 'path';
$london_path = 'path'
?>

 

And put this where the link would go:

 

<a href="<?php

if ($office === false) 

{
echo $blackpool_path;						
} 
else 
{
echo $london_path;
};
?>installer.exe">Link name</a>

 

Many thanks for your help :)

 

Dave

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.