I have the latest version of drupal and I am slowly going round the twist trying to configure the login block so it only shows to people appearing from a certain ip address.

I have tried every single combination of php you can imagine and either the login block always shows or never shows. The strange thing is if you evaluate the same condition in a normal php script it always behaves correctly.

My latest incarnation of the test code is this:

if ($_SERVER['REMOTE_ADDR'] eq 'xxx.xxx.xxx.xxx')
  {
RETURN TRUE;
}
else
{
RETURN FALSE;

I have tried == as well as eq. Used a very simple $_SERVER['REMOTE_ADDR'] == 'xxx.xxx.xxx.xxx' single statement rather than the complicated if and else. I have tried everything with and without the enclosing php tags too.

Any ideas on why this doesn't work?
Am I doing something completely blonde and everyone reading this is sniggering at my obvious mistake? ;-)

Comments

pb8’s picture

echo "<code>";
print_r($GLOBALS);
echo "

";

And.. see if $_SERVER['REMOTE_ADDR'] and etc are what you expect.

srichards’s picture

They are what I expect. If I use the exact same code in a normal php script then it works properly. I have set it to echo the remote ip address and echo a yes if the ip address matches and a no if it doesn't. The test condition works how I would expect. The same condition in the drupal block control doesn't :-/ Either the login box is always on or it is always off.

pb8’s picture

To test if you are logged in use:
if ($user->uid) {..}

And Im not sure if $_SERVER is super global, and are the same thing from within a function, but I blieve it exist inside Drupal also..

(was print_r($GLOBALS); too huuuge?)

Then test print_r($_SERVER) for short.

srichards’s picture

I wrote the same php and used it within a drupal page and that worked ok. I then copied that into the block visibility settings and for some strange reason it now appears to be working :-)


if ($_SERVER['REMOTE_ADDR'] == '10.10.10.10')
{
return TRUE;
}
else
{
return FALSE;
}

I can only assume some strange character or other weirdness was the reason for it not working.

pb8’s picture

Then test to use "$HTTP_SERVER_VARS":
http://se2.php.net/manual/en/reserved.variables.php#reserved.variables.s...

(there are some php version inconsistent there)