[pmwiki-users] Retricting passwords to IPs

Peter Bowers pbowers at pobox.com
Wed Dec 9 09:12:58 CST 2009

On Wed, Dec 9, 2009 at 3:33 PM, Knud Jahnke <jahnke at mpia.de> wrote:
> if ($action=='edit'
>  && !preg_match("^90.68.", $_SERVER['REMOTE_ADDR']) )
>  { $DefaultPasswords['edit'] = crypt('foobar'); }

Typically preg_match() needs slashes around its first argument:

> if ($action=='edit'
>  && !preg_match("/^90\.68\./", $_SERVER['REMOTE_ADDR']) )
>  { $DefaultPasswords['edit'] = crypt('foobar'); }

(I took the liberty of escaping the periods as well, for greater accuracy.)

Technically it doesn't have to be a slash, but you need a character at
the start of the regex and one at the end of the regex to delimit it
and set off any possible regex options.

If this fixes the problem, please be sure to update the example on the
page you got the code from.

> requested. Interestingly, this can not be due to problems with
> $_SERVER['REMOTE_ADDR'] (how can I actually display this in the pmwiki
> context?), but even

If you put an echo statement in your code it will appear on the page.
It will probably cause other errors or warnings, but at least you will
be able to see the message:

echo "REMOTE_ADDR=".$_SERVER['REMOTE_ADDR']."<br>\n";

>  if ($action=='edit' && !preg_match(".*", "a" ))
>   { $DefaultPasswords['edit'] = crypt('foo'); }
> asks for the password, even though preg_match should return true, so the
> negation is false, so the overall condition is false and no password should be
> set.

Same issue with regex delimiter, I believe.

(If you had warnings turned on in your PHP options you would get an
error that looks like this:

Warning: preg_match() [function.preg-match]: No ending delimiter '.'
found in ...

This was when I had a "regex" of ".*".

You may want to figure out the error message thing as well -- it will
help immensely in future debugging.)


More information about the pmwiki-users mailing list