Clan x86

Technical (Development, Security, etc.) => General Programming => Tutorials, References, and Examples => Topic started by: deadly7 on August 05, 2005, 09:33:18 pm

Title: [PHP]blocking ip address from viewing site
Post by: deadly7 on August 05, 2005, 09:33:18 pm
<?php
include "nav.html";
print "<font color=\"white\">". $_SERVER['REMOTE_ADDR'] ."</font>";
if $_SERVER['REMOTE_ADDR'] = 127.0.0.1 Then {
echo "<meta http-equiv=\"refresh\" content=\"5; url=http://www.mysite.com/dir/noview.php\" />" }
?>

Why doesn't it work?

Edit: Returns this message
Parse error: parse error, unexpected T_VARIABLE, expecting '(' in /home/deadly7/public_html/ on line 15
Quote
if $_SERVER['REMOTE_ADDR'] = 127.0.0.1 Then {
That is line 15.
Title: Re: [PHP]blocking ip address from viewing site
Post by: Sidoh on August 05, 2005, 10:13:29 pm
That's kind of eww-y code as it is. Here's a cleaned up version:


<?php

/**
* @return string
* @desc Searches for the enduser's IP-Address
*/
function get_ipaddr() {

if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown"))
$ip = getenv("HTTP_CLIENT_IP");
else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown"))
$ip = getenv("HTTP_X_FORWARDED_FOR");
else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown"))
$ip = getenv("REMOTE_ADDR");
else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown"))
$ip = $_SERVER['REMOTE_ADDR'];
else
$ip = "unknown";


return $ip;
}

$ip = get_ipaddr();
$b_ip = array(
'127.0.0.1',
'64.233.167.99'
);

foreach($b_ip as $v) {

if($v == $ip)
header('location: www.google.com');

}

?>

By the way, the problem with your code is that 127.0.0.1 is a string. You didn't enclose it in quotes. Also -- PHP is pretty leniant on it's syntaxing, but I'm pretty sure it requires you to enclose if statements in parenthensies. Plus, you used "Then" and "{" looks like you were trying to merge VB6 and PHP. :)
Title: Re: [PHP]blocking ip address from viewing site
Post by: deadly7 on August 05, 2005, 10:33:58 pm
Care to explain what that does? I've never used them before, and PHP.Net sucks at explaining things.
Quote
Quote
Title: Re: [PHP]blocking ip address from viewing site
Post by: Warrior on August 06, 2005, 01:38:25 am
What the code does is it performs various checks to set an IP then uses a
for each to loop through the  array comparing it and forwarding them to google.com if they
match the list.

The exact methods he used can be found on php.net by searching: getenv();
Title: Re: [PHP]blocking ip address from viewing site
Post by: rabbit on August 06, 2005, 02:20:21 am
<?php
include "nav.html";
print "<font color=\"white\">". $_SERVER['REMOTE_ADDR'] ."</font>";
if $_SERVER['REMOTE_ADDR'] = 127.0.0.1 Then {
echo "<meta http-equiv=\"refresh\" content=\"5; url=http://www.mysite.com/dir/noview.php\" />" }
?>

Why doesn't it work?

Edit: Returns this message
Parse error: parse error, unexpected T_VARIABLE, expecting '(' in /home/deadly7/public_html/ on line 15
Quote
if $_SERVER['REMOTE_ADDR'] = 127.0.0.1 Then {
That is line 15.
Because that's not valid PHP!

<?php
    include("nav.html");
    echo "<font color=\"white\">" . $_SERVER['REMOTE_ADDR'] . "</font>";
    if($_SERVER['REMOTE_ADDR'] = '127.0.0.1')
    {
        echo "<meta http-equiv=\"refresh\" content=\"5; url=http://www.mysite.com/dir/noview.php\" />";
    }
?>
Title: Re: [PHP]blocking ip address from viewing site
Post by: Sidoh on August 06, 2005, 01:06:13 pm
/me gives a golfclap for warrior.
 Very good! >_>

I already commented on why his code was invalid, rabbit. :P

Why do you weirdos use double-quotes when you're echo'ing html that uses double quotes for attributes? Make more work for yourself... freaks -_-

BTW, Rabbit. You missed something.

if($_SERVER['REMOTE_ADDR'] = '127.0.0.1')

Should be:

if($_SERVER['REMOTE_ADDR'] == '127.0.0.1')
Title: Re: [PHP]blocking ip address from viewing site
Post by: deadly7 on August 06, 2005, 01:30:11 pm
Code: [Select]
$b_ip = array(
'i.put.my.ip.here',
);

It didn't redirect me to googley. :(
Title: Re: [PHP]blocking ip address from viewing site
Post by: Sidoh on August 06, 2005, 01:49:53 pm
Where'd you obtain your IP Address from?
Title: Re: [PHP]blocking ip address from viewing site
Post by: deadly7 on August 06, 2005, 02:02:18 pm
www.whatismyip.com , here (I'm a mod and it shows my IP) , sb.net , and dota-allstars.com
Generally if 4/4 websites tell you that your ip address is what you thought it was, it's probably right.
No I don't run Firefox on any proxies or anything.
Title: Re: [PHP]blocking ip address from viewing site
Post by: Sidoh on August 06, 2005, 02:07:05 pm
I've known people that think they can obtain their public IP address by using ipconfig:

Code: [Select]
Ethernet adapter Local Area Connection:

        Connection-specific DNS Suffix  . :
        IP Address. . . . . . . . . . . . : 10.0.0.4
        Subnet Mask . . . . . . . . . . . : 255.255.255.0
        Default Gateway . . . . . . . . . : 10.0.0.2

Which is why I asked.

Anyway, use debugging. Make the routine echo the ip address it resolved and see if it matches with yours.
Title: Re: [PHP]blocking ip address from viewing site
Post by: deadly7 on August 06, 2005, 02:11:48 pm
Quote
24.163.230.126
Warning: Cannot modify header information - headers already sent by (output started at /home/deadly7/public_html/index.php:6) in /home/deadly7/public_html/bp/index.php on line 44
Let me find line 44..

         header('location: www.google.com');
that is line44
Title: Re: [PHP]blocking ip address from viewing site
Post by: Sidoh on August 06, 2005, 02:30:24 pm
That's a strange "feature" of PHP I've encountered a few times myself. Try changing it to:

header('location: www.google.com', false);
Title: Re: [PHP]blocking ip address from viewing site
Post by: deadly7 on August 06, 2005, 02:55:37 pm
Same error even though I changed the line.
Title: Re: [PHP]blocking ip address from viewing site
Post by: Sidoh on August 06, 2005, 02:59:02 pm
Odd. I have yet to figure out why it does that on some servers. Probably something in php.ini. Just use that meta tag.

Code: [Select]
echo '<meta http-equiv="refresh" content="0; URL=www.google.com" />';
Title: Re: [PHP]blocking ip address from viewing site
Post by: deadly7 on August 06, 2005, 04:36:56 pm
OMFG I LOVE YOU SIDOH.
Title: Re: [PHP]blocking ip address from viewing site
Post by: Sidoh on August 06, 2005, 08:39:32 pm
Glad I could help. =)
Title: Re: [PHP]blocking ip address from viewing site
Post by: deadly7 on August 06, 2005, 10:03:17 pm
Oh, I should add something...

echo '<meta http-equiv="refresh" content="0; URL=www.google.com" />';

If you're using that, add http:// before the website (www.google.com) or else it will redirect to www.mysite.com/folder/www.google.com
Title: Re: [PHP]blocking ip address from viewing site
Post by: Sidoh on August 06, 2005, 10:18:19 pm
Well, yeah. That was um... shorthand HTML. *looks around nervously*
Title: Re: [PHP]blocking ip address from viewing site
Post by: Warrior on August 06, 2005, 11:44:42 pm
The reason the header didn't work: If you're going to do something like that you need to make sure it's one of the first things done, else the page will already have sent it's header information out thus producing that error.
Title: Re: [PHP]blocking ip address from viewing site
Post by: deadly7 on August 07, 2005, 12:50:23 am
war, so I should've put that before <head>?
Title: Re: [PHP]blocking ip address from viewing site
Post by: Mythix on August 07, 2005, 12:54:17 am
No.

Code: [Select]

<head>
<meta http-equiv="refresh" content="5; url=refresh1.html">

<title> chapter 17 multimedia </title>

</head>


works just as well.
Title: Re: [PHP]blocking ip address from viewing site
Post by: Sidoh on August 07, 2005, 02:13:32 am
The reason the header didn't work: If you're going to do something like that you need to make sure it's one of the first things done, else the page will already have sent it's header information out thus producing that error.

It always works unless you've done anything with sessions or cookies (I'm sure there are other similar causes of the error, but these are the two main cuprates I've found).

And no, deadly7. He's saying it's the first routine that should be executed within PHP.

By tradition, meta tags are within the head tags, but it doesn't matter (I think W3C might care, but all browsers I use don't).
Title: Re: [PHP]blocking ip address from viewing site
Post by: Warrior on August 07, 2005, 10:32:01 am
First or near first, from what I've noticed.
Title: Re: [PHP]blocking ip address from viewing site
Post by: Sidoh on August 07, 2005, 01:07:50 pm
First or near first, from what I've noticed.

Anything that is before a session or cookie declaration is my belief.
Title: Re: [PHP]blocking ip address from viewing site
Post by: Warrior on August 07, 2005, 01:17:25 pm
PERHAPS AN INVESTIGATION !
Title: Re: [PHP]blocking ip address from viewing site
Post by: venox on August 23, 2005, 07:27:51 pm
Rather than looping through the array.  Which, works just fine.  Using in_array() would be a bit faster.

Code: [Select]

if (in_array($ip, $iparray)) {
    die('You are banned!');
}

Title: Re: [PHP]blocking ip address from viewing site
Post by: Sidoh on August 23, 2005, 07:44:36 pm
Haha, yeah.  I just remembered that function a few days ago when I was doing something with arrays.

Good to see ya venox! :)
Title: Re: [PHP]blocking ip address from viewing site
Post by: venox on August 23, 2005, 09:12:49 pm
Haha, yeah.  I just remembered that function a few days ago when I was doing something with arrays.

Good to see ya venox! :)

Likewise :)
Title: Re: [PHP]blocking ip address from viewing site
Post by: zorm on August 23, 2005, 10:46:29 pm
Note that given things like HTTP_X_FORWARDED_FOR are headers sent by the client they can be forged and as such checking only them is likely a bad idea. One should really be checking all possible ips and not just the first found one.
Title: Re: [PHP]blocking ip address from viewing site
Post by: Sidoh on August 23, 2005, 10:48:59 pm
Note that given things like HTTP_X_FORWARDED_FOR are headers sent by the client they can be forged and as such checking only them is likely a bad idea. One should really be checking all possible ips and not just the first found one.

Blame the guy on php.net!  :)
Title: Re: [PHP]blocking ip address from viewing site
Post by: zorm on August 23, 2005, 10:52:17 pm
I'll blame you for not knowing better yet selling your PHP services! :p
Title: Re: [PHP]blocking ip address from viewing site
Post by: Sidoh on August 24, 2005, 12:29:03 am
I just remembered that it was there and copied/pasted from it.  =P

It's better than getenv("REMOTE_ADDR").

I did know that, by the way.  :-\

"selling my services?"  Show me the price, please.
Title: Re: [PHP]blocking ip address from viewing site
Post by: rabbit on August 24, 2005, 01:31:05 am
Mine is $40/hour (http://liquid-dev.org/)
Title: Re: [PHP]blocking ip address from viewing site
Post by: Sidoh on August 24, 2005, 05:24:56 pm
I don't think anyone was asking anything near the subject of a specific price on PHP development.  Plus, zorm's statement wasn't even directed towards you.
Title: Re: [PHP]blocking ip address from viewing site
Post by: rabbit on August 24, 2005, 09:20:48 pm
Eh..he said "show me the price".  Close enough.
Title: Re: [PHP]blocking ip address from viewing site
Post by: JTN Designer on September 26, 2005, 07:17:30 pm
<?php
include "nav.html";
print "<font color=\"white\">". $_SERVER['REMOTE_ADDR'] ."</font>";
if $_SERVER['REMOTE_ADDR'] = 127.0.0.1 Then {
echo "<meta http-equiv=\"refresh\" content=\"5; url=http://www.mysite.com/dir/noview.php\" />" }
?>

Why doesn't it work?

Edit: Returns this message
Parse error: parse error, unexpected T_VARIABLE, expecting '(' in /home/deadly7/public_html/ on line 15
Quote
if $_SERVER['REMOTE_ADDR'] = 127.0.0.1 Then {
That is line 15.

Although this has been resolved; Mark since you're using a Boolean, try to put the include statement on the return true section of the script, I.E.

<?php
if($_SERVER['REMOTE_ADDR'] == "127.0.0.1")
     { ?>
     <meta http-equiv="refresh" content="5; url=noview.php">
     <?php
     } else {
                include "nav.html";
                }
?>
Title: Re: [PHP]blocking ip address from viewing site
Post by: Sidoh on September 26, 2005, 08:06:52 pm
It's easier to just include some file that checks for banned IP's and then uses exit; if it finds one.
Title: Re: [PHP]blocking ip address from viewing site
Post by: JTN Designer on September 27, 2005, 05:07:24 pm
Yeah, using the .htaccess or on a dedicated linux build, use SSH to modify the access.conf file using DenyAll
Title: Re: [PHP]blocking ip address from viewing site
Post by: Sidoh on September 27, 2005, 05:27:36 pm
Quote from: subject
[PHP]blocking ip address from viewing site

Which is why I didn't include information in .htaccess.  :)
Title: Re: [PHP]blocking ip address from viewing site
Post by: JTN Designer on September 27, 2005, 08:46:02 pm
Jeesh your hard to counterpoint :P
Title: Re: [PHP]blocking ip address from viewing site
Post by: Sidoh on September 27, 2005, 08:57:25 pm
Hehe.