Beginner: problem with redirecting

Hello all. I’m taking a course learning php so my experience is very limited. Without posting the whole assignment, I’m having trouble redirecting. The code is supposed to check to see if the user is connecting from a 127.0.x.x or a 192.168.x.x ip.

•In the PHP portion of the solution03.php file, read in the contents of the REMOTE_ADDR environment variable, and save that string in a variable named $ipaddress.
•Construct an if statement which will test to see whether the $ipaddress contains either 127.0 or 192.168. If it does, re-direct the visitor to the greeting page you created for Assignment #1 (which should be named solution01.php).
•(NOTE: The simplest way to do this is to use the if clause to test for one of the two strings and an else if clause to test for the other. That is easier than using the OR logical operator . . .)
•If the address does not contain either of those strings, re-direct the visitor to a page named solution03_distant.html.

I’m now lost as to where im going wrong, trying to view the page, it says theres an error on line 20. skipping the blank HTML heres the php code.

[php]<?php

$ipaddress = getenv(“REMOTE_ADDR”);


if ($ipaddress =="127.0") {
header(“Location: solution01.php”);
}elseif ($ipaddress =="192.168") {
header(“Location: solution01.php”);
}else {
header(“Location: solution03_distant.html”);
}
?>[/php]

Any thoughts, pointers, tips, or pointing out the obvious would be greatly appreciated. Thank you.

$ipaddress won’t equal just 127.0 or 192.168, it’ll be the complete ip. The simplest way that i know of doing this would be to do this:
[php]

<?php $ip = explode('.', $_SERVER['REMOTE_ADDR']); if($ip[0] != 192 || $ip[0] != 127) { header(“Location: solution03_distant.html”); } else { switch($ip[0]) { case 127: header(“Location: solution01.php”); break; case 192: header(“Location: solution01.php”); break; } } ?>[/php]

$ip = explode(’.’, $_SERVER[‘REMOTE_ADDR’]); creates an array that looks like (what it actually says depends on the ip) array ( [0] => 192, [1] => 168, [2] => 10, [3] => 4 )

if($ip[0] != 192 || $ip[0] != 127) { evaluates the first piece of the array. no loop is needed since we’re looking at just 1 part of it. || is OR, so if $ip[0] doesn’t equal 192 or 127, then send the user to the page. If it does, then move through the switch.

The switch is the same was doing if else statements, just a cleaner way of doing it. There needs to be a case for thing you want to do. In your case, you only need 192 and 127.

I hope that helps you.

Thanks, I’ll look over that to make sense of it. I figured I should go ahead and post this just to make sure what I’m trying to do makes some sense.

Just for fun, what we are going to do is to pretend that we can associate IP addresses with geographical locations. (We can't, particularly when dealing with the home networks most people have installed.) We will pretend that users on the 127.0.x.x and the 192.168.x.x networks reside within the boundaries of the district of The Little Batty League, and that people on other networks live farther away - far enough that the organizers of the Flea Market would like to offer to deliver items. This script will test to see what network the visitor is using, directing him or her to the proper section of the web site. Name the script which will test the IP address solution03.php.

I have no idea what you’re talking about. All i did was copy the headers you had. This is all pretty useless when you get into the real world unless you’re doing search engines.

I noticed that your quotes are invalid. Possibly copied from Microsoft Word or something similar. You will need to retype them manually instead of copy & paste.

If you did this for example:

[php]
echo htmlspecialchars("“REMOTE_ADDR”", ENT_QUOTES | ENT_SUBSTITUTE, ‘UTF-8’);
// prints �REMOTE_ADDR�
[/php]

As for you question I’ll off another solution using preg_match

[php]
$ip_address = $_SERVER[‘REMOTE_ADDR’];
if (preg_match(’/^127.0|192.168/’, $ip_address)) {
header(‘Location: solution01.php’);
} else {
header(‘Location: solution03_distant.html’);
}
[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service