[PHP]Simple Redirect Other IPs

Status
Not open for further replies.

TesoMayn

Boredom, it vexes me.
Oct 30, 2011
1,482
1,482
You could easily use .htaccess to do this, but you can also do it in PHP like this:

PHP:
$allowed = array("1.7.3.5.0"); //Change To Your IP Address (That is 1337 for iTes0 BTW)
$ip = $_SERVER['REMOTE_ADDR'];
if(!in_array($ip, $allowed))
{
header("Location: http://devbest.com");
exit;
}
else
{
print('Your IP is allowed!');
}

Or you could wrap it around a webpage:

PHP:
$allowed = array("1.7.3.5.0"); //Change To Your IP Address (That is 1337 for iTes0 BTW)
$ip = $_SERVER['REMOTE_ADDR'];
if(!in_array($ip, $allowed))
{
header("Location: http://devbest.com");
exit;
}
else
{
HTML:
WEBSITE CONTENT
PHP:
}
 

Predict

Active Member
Jun 27, 2011
126
63
Looks orite, I would prefer to use .htaccess.
By the way, to improve performance; remove the variables and just actually place the data inside the function ()

PHP:
if (!in_array($_SERVER['REMOTE_ADDR'], array("127.0.0.1")))
{
header("Location: http://devbest.com");
exit;
}
else
{
echo 'wd';
}
 

Markshall

Русский Стандарт
Contributor
Dec 18, 2010
2,637
2,389
If you're only allowing one IP address to view the site, why are you using arrays? Just use a string, like this:
PHP:
<?php
if ( $_SERVER["REMOTE_ADDR"] == "127.0.0.1" )
{
    header( "Location: page.html" );
}
else
{
    echo "hello";
}
?>

However, using multiple IP addresses:
PHP:
<?php
if ( in_array( $_SERVER["REMOTE_ADDR"], array( "127.0.0.1", "255.255.255.255", "1.215.221.91" ) ) )
{
    echo "hello";
}
else
{
    header( "Location: page.html" );
}
?>

Or if you want the HTACCESS version, if I remember correctly it is something like this
Code:
order allow,deny
allow from all
deny from 34.34.34.34

Nice code/idea anyway.
 

Livar

Now 35% cooler!
Oct 15, 2010
846
86
If you're only allowing one IP address to view the site, why are you using arrays? Just use a string, like this:
PHP:
<?php
if ( $_SERVER["REMOTE_ADDR"] == "127.0.0.1" )
{
    header( "Location: page.html" );
}
else
{
    echo "hello";
}
?>

However, using multiple IP addresses:
PHP:
<?php
if ( in_array( $_SERVER["REMOTE_ADDR"], array( "127.0.0.1", "255.255.255.255", "1.215.221.91" ) ) )
{
    echo "hello";
}
else
{
    header( "Location: page.html" );
}
?>

Or if you want the HTACCESS version, if I remember correctly it is something like this
Code:
order allow,deny
allow from all
deny from 34.34.34.34

Nice code/idea anyway.

If I'm right I thought you can do this?

PHP:
$ip = $SERVER['REMOTE_ADDR'];
if($ip == "127.0.0.1" || "ipadress")
{
//allow
}
 
else
{
//deny
}
 

Predict

Active Member
Jun 27, 2011
126
63
Not exactly Livar because you want to create an array of IP's to allow inside the webpage.
 

Markshall

Русский Стандарт
Contributor
Dec 18, 2010
2,637
2,389
If I'm right I thought you can do this?

PHP:
$ip = $SERVER['REMOTE_ADDR'];
if($ip == "127.0.0.1" || "ipadress")
{
//allow
}
 
else
{
//deny
}
That would return an error because after the or statement (||), you only put "ipadress" and never put anything in front of it like '$ip = ', but I know what you mean - and yes, it would work
 

Livar

Now 35% cooler!
Oct 15, 2010
846
86
That would return an error because after the or statement (||), you only put "ipadress" and never put anything in front of it like '$ip = ', but I know what you mean - and yes, it would work
ye, I didn't exactly write it correctly but you know what I mean.
 
Status
Not open for further replies.

Users who are viewing this thread

Top