Page 1 of 1

how do i add another IP to this ????

Posted: Fri Feb 27, 2004 10:37 am
by popcop
im using this code to redirect my friend to another page when he visits my site

<?php
$ip = "213.78.27.203";
if($REMOTE_ADDR == $ip) {
echo "<script language=javascript>location.replace('http://www.yahoo.com)</script>";
}
?>


how would i add another ip address to it.... if i wanted to redirect another friend

would it be :

<?php
$ip = "213.78.27.203";
$ip = "203.20.6.78";
if($REMOTE_ADDR == $ip) {
echo "<script language=javascript>location.replace('http://www.yahoo.com)</script>";
}
?>

Posted: Fri Feb 27, 2004 10:46 am
by gangboy
I would go for:
<?php
$ip = "213.78.27.203";
$ip2 = "203.20.6.78";
if($REMOTE_ADDR == $ip or $ip2) {
echo "<script language=javascript>location.replace('http://www.yahoo.com)</script>";
}
?>

Posted: Fri Feb 27, 2004 10:47 am
by popcop
what if i was going to be adding more ip address to it ?

Posted: Fri Feb 27, 2004 10:48 am
by gangboy
Why not create an array to describe wich class of IP's to be allowed?

Posted: Fri Feb 27, 2004 10:54 am
by popcop
im just playin a trick on a few freinds

i just wantered to know how i could add a few diffrent ip address to redirect

Posted: Fri Feb 27, 2004 11:00 am
by gangboy

Code: Select all

<?php 
$ip = "213.78.27.203"; 
$ip2 = "203.20.6.78";
$ip3 = "YOUR IP HERE"
if($REMOTE_ADDR == $ip or $ip2 or $ip3) { 
echo "<script language=javascript>location.replace('http://www.yahoo.com)</script>"; 
} 
?>
And so on...

Posted: Fri Feb 27, 2004 11:02 am
by Weirdan

Code: Select all

$_ips=array(
   "213.78.27.203",
   "203.20.6.78"
//more ips could be added
)
if(in_array($_SERVER["REMOTE_ADDR"],$_ips)){
  // redirect
}

Posted: Fri Feb 27, 2004 11:04 am
by JayBird
gangboy:

You can't do this (your if syntax is incorrect

Code: Select all

$ip = "213.78.27.203"; 
$ip2 = "203.20.6.78"; 
$ip3 = "YOUR IP HERE" 

if($REMOTE_ADDR == $ip or $ip2 or $ip3) { 
echo "<script language=javascript>location.replace('http://www.yahoo.com)</script>"; 
}
has to be like this..

Code: Select all

$ip = "213.78.27.203"; 
$ip2 = "203.20.6.78"; 
$ip3 = "YOUR IP HERE" 

if($REMOTE_ADDR == $ip || $REMOTE_ADDR == $ip2 || $REMOTE_ADDR == $ip3) { 
echo "<script language=javascript>location.replace('http://www.yahoo.com)</script>"; 
}
but, would be even better to do it like this...

Code: Select all

$ips = array("213.78.27.203",
			"203.20.6.78",
			"YOUR IP HERE"); //Just keep adding as many as you want 

if (in_array($REMOTE_ADDR, $ips)) {
	echo "<script language=javascript>location.replace('http://www.yahoo.com)</script>";
}
Mark

Mark

Posted: Fri Feb 27, 2004 12:18 pm
by ol4pr0
Weirdan wrote:

Code: Select all

$_ips=array(
   "213.78.27.203",
   "203.20.6.78"
//more ips could be added
)
if(in_array($_SERVER["REMOTE_ADDR"],$_ips)){
  // redirect
}
I would go like weirdan said.

Its easyer to insert or delete a ip on a later stage when the list is getting
bigger along the way

Posted: Fri Feb 27, 2004 10:43 pm
by Steveo31
But the IP's change each time...right?

Posted: Fri Feb 27, 2004 11:23 pm
by Illusionist
ya, Like Steve said, it'd kind of hard to play a trick onsomeone liek that unless they have a static IP address...

Posted: Sat Feb 28, 2004 12:58 am
by AbelaJohnB
This instance really calls for the usage of an [php_man]array()[/php_man], I feel.

Perhaps this would be better:

Code: Select all

&lt;?php

//
// let's grab the users ip address
// should catch everything, except for some transparent proxies.
//
if( getenv('HTTP_X_FORWARDED_FOR') != '' )
&#123;
	$users_ip_address = ( !empty($HTTP_SERVER_VARS&#1111;'REMOTE_ADDR']) ) ? $HTTP_SERVER_VARS&#1111;'REMOTE_ADDR'] : ( ( !empty($HTTP_ENV_VARS&#1111;'REMOTE_ADDR']) ) ? $HTTP_ENV_VARS&#1111;'REMOTE_ADDR'] : $REMOTE_ADDR );

	if ( preg_match("/^(&#1111;0-9]+\.&#1111;0-9]+\.&#1111;0-9]+\.&#1111;0-9]+)/", getenv('HTTP_X_FORWARDED_FOR'), $ip_list) )
	&#123;
		$private_ip = array('/^0\./', '/^127\.0\.0\.1/', '/^192\.168\..*/', '/^172\.16\..*/', '/^10..*/', '/^224..*/', '/^240..*/');
		$users_ip_address = preg_replace($private_ip, $users_ip_address, $ip_list&#1111;1]);
	&#125;
&#125;
else
&#123;
	$users_ip_address = ( !empty($HTTP_SERVER_VARS&#1111;'REMOTE_ADDR']) ) ? $HTTP_SERVER_VARS&#1111;'REMOTE_ADDR'] : ( ( !empty($HTTP_ENV_VARS&#1111;'REMOTE_ADDR']) ) ? $HTTP_ENV_VARS&#1111;'REMOTE_ADDR'] : $REMOTE_ADDR );
&#125;
//
// let's make this a bit easier to detect the true/recurring ip...
//
$$users_ip_address_block = substr($users_ip_address, 0, 10);
//
// build a list of ip's we would like to ban/redirect
//
$go_bye_bye = array('208.25.194', '63.160.62.', '63.160.62.128', '206.14.153'); // , '127.0.0.1'
//
// now we do a compare of "in_array".... if something is in something, do something
//
if (in_array($$users_ip_address_block,$go_bye_bye))
&#123;
	//
	// build our redirect link... down with javascript!
	// than throw a header for google.... &lt;sigh&gt;
	// than throw a header for the true redirect
	// than throw a die() to force page-death with a nice little message.
	//
	$temp_url = 'http://www.yahoo.com/';
	header('HTTP/1.1 301 Moved Permanently');
	header("Location: ". $temp_url);
	exit('&lt;BR /&gt;Redirecting: &lt;BR /&gt;' . $temp_url);
&#125;
//
// That's All Folks
//
?&gt;

Posted: Sat Feb 28, 2004 11:13 am
by d3ad1ysp0rk
if you read the above posts you'd see that they already used an array in some code, with.. 25 less lines? :roll:

Posted: Sat Feb 28, 2004 11:33 am
by Steveo31
Illusionist wrote:ya, Like Steve said, it'd kind of hard to play a trick onsomeone liek that unless they have a static IP address...
What about a partial IP, like the first 3 letters? Don't those stay pretty static? The only prob I can see is someone else with the same first 3 digits of their IP the same as the friend, and they get offended or somethin'.

Code: Select all

<?php

$ip=getenv("REMOTE_ADDR");
			if("127"==substr("$ip", 0, 3)){
				//Don't do anything 
			}else{
				//redirect
";

?>