Restrict webpage access via IP?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mogman42
Forum Newbie
Posts: 18
Joined: Fri May 21, 2004 10:00 am

Restrict webpage access via IP?

Post 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&#1111;'REMOTE_ADDR'];

If($ip != 132.177) 
&#123;
echo 'You are not granted access to this page!';
//Error msg and do not load page
&#125;
Elseif $ip == 132.177)
&#123;
Load page normally?;
//Load page normally
&#125;
?>
Last edited by mogman42 on Wed Jun 02, 2004 11:23 am, edited 1 time in total.
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post 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 
?>
mogman42
Forum Newbie
Posts: 18
Joined: Fri May 21, 2004 10:00 am

Post 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&#1111;'REMOTE_ADDR']; 
12: If(!strstr('132.177', $ip) 
13: &#123; 
14: die('You are not granted access to this page!'); 
15: &#125; 

//Load page normally 
?>
Last edited by mogman42 on Wed Jun 02, 2004 11:23 am, edited 1 time in total.
Deemo
Forum Contributor
Posts: 418
Joined: Sun Jan 18, 2004 11:48 am
Location: Washington DC

Post 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 ;)
mogman42
Forum Newbie
Posts: 18
Joined: Fri May 21, 2004 10:00 am

Post by mogman42 »

Doh, so it is..........thank you :P

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.
User avatar
launchcode
Forum Contributor
Posts: 401
Joined: Tue May 11, 2004 7:32 pm
Location: UK
Contact:

Post 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.
mogman42
Forum Newbie
Posts: 18
Joined: Fri May 21, 2004 10:00 am

Post 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&#1111;'REMOTE_ADDR']; 
$check !=  strstr('132.177', $ip);

If($ip != '132.177') 
&#123; 
die('You are not authorized to view this page!'); 
&#125; 
//Load page normally 
?>
Last edited by mogman42 on Wed Jun 02, 2004 1:32 pm, edited 1 time in total.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

You could do:

Code: Select all

$ip = join('.', array_slice(explode('.', $_SERVER['REMOTE_ADDR']), 0, 2));
if($ip != '132.177'){
   die('whatever');
}
User avatar
launchcode
Forum Contributor
Posts: 401
Joined: Tue May 11, 2004 7:32 pm
Location: UK
Contact:

Post 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";
}
mogman42
Forum Newbie
Posts: 18
Joined: Fri May 21, 2004 10:00 am

Post 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
Post Reply