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

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
popcop
Forum Newbie
Posts: 21
Joined: Fri Jan 30, 2004 10:42 am

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

Post 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>";
}
?>
User avatar
gangboy
Forum Newbie
Posts: 14
Joined: Tue Jan 06, 2004 11:58 am
Location: Constanţa, România

Post 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>";
}
?>
popcop
Forum Newbie
Posts: 21
Joined: Fri Jan 30, 2004 10:42 am

Post by popcop »

what if i was going to be adding more ip address to it ?
User avatar
gangboy
Forum Newbie
Posts: 14
Joined: Tue Jan 06, 2004 11:58 am
Location: Constanţa, România

Post by gangboy »

Why not create an array to describe wich class of IP's to be allowed?
popcop
Forum Newbie
Posts: 21
Joined: Fri Jan 30, 2004 10:42 am

Post 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
User avatar
gangboy
Forum Newbie
Posts: 14
Joined: Tue Jan 06, 2004 11:58 am
Location: Constanţa, România

Post 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...
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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
}
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post 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
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post 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
Steveo31
Forum Contributor
Posts: 416
Joined: Sun Nov 23, 2003 9:05 pm
Location: San Jose CA

Post by Steveo31 »

But the IP's change each time...right?
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post 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...
AbelaJohnB
Forum Newbie
Posts: 9
Joined: Sat Feb 28, 2004 12:14 am

Post 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;
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post 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:
Steveo31
Forum Contributor
Posts: 416
Joined: Sun Nov 23, 2003 9:05 pm
Location: San Jose CA

Post 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
";

?>
Post Reply