1

On a D6 site I want a block that it visible only to visitors from a specific IP. If I choose Page specific visiblity settings with the PHP code

<?php 
  if(strcmp(ip_address(),'1.2.3.4')==0) {
    return true;
  } else {
    return false;
  }
?>

it works fine on all pages, except for the front page.

How can I make this setting work also on the front page?

Is this snippet not executed on the front page?

Update: Or is this some caching issue? It's working on the front page, if I connect with https. But block caching is disabled on the site.

  • Is it different for authenticated vs anonymous users? Do you have page caching turned on? – Jimajamma Oct 27 '13 at 14:55
  • @Jimajamma Yes, for authenticated users it seems to work as it should, for anonymous it doesn't. Page caching is on, block caching off. – Alex Oct 27 '13 at 15:34
  • @Alex Turning block cache off has no effect on a cached page – Mołot Oct 27 '13 at 17:13
  • @Mołot Ok, this is really a caching issue, which made me switch to a different setup: Now I use IP-based login and make the block visible to a particular user. This works reliably. Consider the problem solved... – Alex Oct 27 '13 at 17:14
  • Alex if it's confirmed and solved, write an answer and accept it. Unless you want me or @Jim to write it, of course. – Mołot Oct 27 '13 at 17:18
0

The commentators questions and remarks pointed me in the right direction, so here's the solution to my own question:

With page caching enabled, the above PHP condition is evaluated only occasionally on cache rebuild. If someone with the correct IP visits the site at the right moment the block can get visible and cached. Visitors with the wrong IP may then also see it (from the cache). Therefore the above approach is wrong.

Correct solution: Use IP login to authenticate some real or special user, then make the block visible to this user with the PHP condition:

<?php
  global $user;
  if($user->uid==123) {
    return true;
  } else {
    return false;
  }
?>

Of course, one could also define a new group and use the group-based visibility settings.

  • If you wanted the user to only access the site at a certain IP, you could probably add an IP Address field to the User entity. – mbomb007 Jun 28 at 14:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.