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
mogman42
Forum Newbie
Posts: 18 Joined: Fri May 21, 2004 10:00 am
Post
by mogman42 » Wed Jun 02, 2004 9:07 am
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
}
?>
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 » Wed Jun 02, 2004 9:09 am
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 » Wed Jun 02, 2004 10:46 am
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
?>
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 » Wed Jun 02, 2004 11:05 am
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 » Wed Jun 02, 2004 11:13 am
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.
launchcode
Forum Contributor
Posts: 401 Joined: Tue May 11, 2004 7:32 pm
Location: UK
Contact:
Post
by launchcode » Wed Jun 02, 2004 11:42 am
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 » Wed Jun 02, 2004 12:11 pm
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
?>
Last edited by
mogman42 on Wed Jun 02, 2004 1:32 pm, edited 1 time in total.
markl999
DevNet Resident
Posts: 1972 Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)
Post
by markl999 » Wed Jun 02, 2004 12:15 pm
You could do:
Code: Select all
$ip = join('.', array_slice(explode('.', $_SERVER['REMOTE_ADDR']), 0, 2));
if($ip != '132.177'){
die('whatever');
}
launchcode
Forum Contributor
Posts: 401 Joined: Tue May 11, 2004 7:32 pm
Location: UK
Contact:
Post
by launchcode » Wed Jun 02, 2004 12:15 pm
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 » Wed Jun 02, 2004 12:22 pm
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