Page 1 of 1
Restrict webpage access via IP?
Posted: Wed Jun 02, 2004 9:07 am
by mogman42
Ok, I'm trying to restrict webpage access for some internal pages to local network users only via IP address using REMOTE_ADDR.
Looking at the code below, am I going about it in the right way?
Thanks!
Code: Select all
<?
$ip = $_SERVERї'REMOTE_ADDR'];
If($ip != 132.177)
{
echo 'You are not granted access to this page!';
//Error msg and do not load page
}
Elseif $ip == 132.177)
{
Load page normally?;
//Load page normally
}
?>
Posted: Wed Jun 02, 2004 9:09 am
by qads
Code: Select all
<?php
$ip = $_SERVER['REMOTE_HOST'];
If(!strstr('132.177', $ip)
{
die('You are not granted access to this page!');
}
//Load page normally
?>
Posted: Wed Jun 02, 2004 10:46 am
by mogman42
Thanks very much for the code help!
However, I get this...
Parse error: parse error, unexpected '{' in D:\web\beta\search.html on line 13
I've tried moving it around, etc. Refuses to work.
Code: Select all
10: <?php
11: $ip = $_SERVERї'REMOTE_ADDR'];
12: If(!strstr('132.177', $ip)
13: {
14: die('You are not granted access to this page!');
15: }
//Load page normally
?>
Posted: Wed Jun 02, 2004 11:05 am
by Deemo
u need another ) at the end of line 12, because u still havent closed the parenthessis for the If statement, you only closed the one for the strstr function

Posted: Wed Jun 02, 2004 11:13 am
by mogman42
Doh, so it is..........thank you
K, another question....
How do I specify to only check the first two sets of #'s from the $_SERVER['REMOTE_ADDR'] variable?
I only want it to check for 132.177.xxx.xxx the last two don't matter.
Posted: Wed Jun 02, 2004 11:42 am
by launchcode
Split the string on the period character and then compare the first two elements? Or split it on the 2nd period so you're only checking the first 2 ranges.
Posted: Wed Jun 02, 2004 12:11 pm
by mogman42
Tried this..........page just loads, even when the criteria isn't met...
Code: Select all
<?php
//Check for local network users
$ip = $_SERVERї'REMOTE_ADDR'];
$check != strstr('132.177', $ip);
If($ip != '132.177')
{
die('You are not authorized to view this page!');
}
//Load page normally
?>
Posted: Wed Jun 02, 2004 12:15 pm
by markl999
You could do:
Code: Select all
$ip = join('.', array_slice(explode('.', $_SERVER['REMOTE_ADDR']), 0, 2));
if($ip != '132.177'){
die('whatever');
}
Posted: Wed Jun 02, 2004 12:15 pm
by launchcode
Erm.. of course not.. you aren't even using the result of $check and it should be !==, not !=. Try this:
if ($check)
{
echo "Go away";
}
Posted: Wed Jun 02, 2004 12:22 pm
by mogman42
markl999 wrote:You could do:
Code: Select all
$ip = join('.', array_slice(explode('.', $_SERVER['REMOTE_ADDR']), 0, 2));
if($ip != '132.177'){
die('whatever');
}
Bingo!!! That was it!!
Thank you SOOOO much!!
Also, for reference I found this article on PHP Authentication...
http://www.informit.com/articles/articl ... 0&seqNum=4