Checking IP then redirecting

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

User avatar
redhair
Forum Contributor
Posts: 300
Joined: Fri May 30, 2003 4:36 pm
Location: 53.23N-6.57E
Contact:

Post by redhair »

popcop wrote:aaargggghhhh..... i cant get this to work
Are you running php at all?

Try this code, and tell me if it works:

Code: Select all

<?php

$ip = getenv("REMOTE_ADDR");

echo $ip;

?>
Do you see your ip when you run this?
User avatar
dethron
Forum Contributor
Posts: 370
Joined: Sat Apr 27, 2002 11:39 am
Location: Istanbul

Post by dethron »

Just write the following to a blank page, then save it.
And request this file server via browser.

Code: Select all

<script>location.replace('http://www.borsacini.com')</script>
Does it work?

If not, we should search other things to find the problem; because there is nothing wrong with this code-line.
(!!dont forget browser compatibility, i didnt checkhed which of those supports location.replace)
By the way, what is your browser?
User avatar
dethron
Forum Contributor
Posts: 370
Joined: Sat Apr 27, 2002 11:39 am
Location: Istanbul

Post by dethron »

By the way compatibility for window.location.replace is

IE 4.0+, NS 4.0+, Mac/Win
User avatar
redhair
Forum Contributor
Posts: 300
Joined: Fri May 30, 2003 4:36 pm
Location: 53.23N-6.57E
Contact:

Post by redhair »

Why not use header :?:

Code: Select all

<?php
header ("location:http://www.php.net");
?>
Works with all browsers.
User avatar
dethron
Forum Contributor
Posts: 370
Joined: Sat Apr 27, 2002 11:39 am
Location: Istanbul

Post by dethron »

Yes redhair, it is possible too.
Actually, considering bc(browser compatibility), your suggestion is better.
User avatar
redhair
Forum Contributor
Posts: 300
Joined: Fri May 30, 2003 4:36 pm
Location: 53.23N-6.57E
Contact:

Post by redhair »

considering cs(common sence) I would just use this. Posted earlier in this topic.

But popcop says 'it doesnt ~seem~ to be working',....
popcop
Forum Newbie
Posts: 21
Joined: Fri Jan 30, 2004 10:42 am

Post by popcop »

xcxc
Last edited by popcop on Fri Feb 20, 2004 8:01 pm, edited 2 times in total.
User avatar
dethron
Forum Contributor
Posts: 370
Joined: Sat Apr 27, 2002 11:39 am
Location: Istanbul

Post by dethron »

Since location.replace is works for me until now, I haven't complain about it :))

But now, I found a better substitude for location.replace considering both cs and bc ;)

Thnx.
User avatar
dethron
Forum Contributor
Posts: 370
Joined: Sat Apr 27, 2002 11:39 am
Location: Istanbul

Post by dethron »

Since it works for simple code. It must also work for the complex code except the complex code has another problem.

Add a line that echos the $REMOTE_ADDR

What is the remote address? Are you sure it is 62.252.128.12
popcop
Forum Newbie
Posts: 21
Joined: Fri Jan 30, 2004 10:42 am

Post by popcop »

yeah i was testin it on myself to see if would work but it never
User avatar
dethron
Forum Contributor
Posts: 370
Joined: Sat Apr 27, 2002 11:39 am
Location: Istanbul

Post by dethron »

Code: Select all

<?php
echo "<html><head><title>Untitled Document</title></head> ";
echo "<body>";
$ip = "62.252.128.12"; 
if($REMOTE_ADDR == $ip) { 
echo "red queen"; 
} 
echo "</body></html> ";
?>
Try this code, did you see the red queen?
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

are you sure that's your ip?

go here to find your IP:

http://www.optikhosting.com/~des404/ip.php
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

dethron, your code will only work if register globals is on..
User avatar
dethron
Forum Contributor
Posts: 370
Joined: Sat Apr 27, 2002 11:39 am
Location: Istanbul

Post by dethron »

Then add a line to my code :)

Code: Select all

<?php
//I am assuming the register global is on
?>
Thnx for correction.
evilMind
Forum Contributor
Posts: 145
Joined: Fri Sep 19, 2003 10:09 am
Location: Earth

Post by evilMind »

The reason why it's not working is because you have sent the headers already (when you opened output to the client)

Try this. Move ALL of the php code before ANY HTML output. In fact, make sure that there is no output sent to the client before you try to use the header function (*note* blank lines at the end of a file can cause headers to be sent).

eg:

Code: Select all

<html>
<head><title>Foo</title></head>
<body>
<?php
   if ($_SERVER&#1111;'REMOTE_ADDR'] == '127.0.0.1') &#123;
      header('Location: http://www.foo.com/');
      exit(0);
   &#125;
?>
<!-- This will NOT work -->
</body>
</html>
but....

Code: Select all

<?php
   if ($_SERVER&#1111;'REMOTE_ADDR'] == '127.0.0.1') &#123;
      header('Location: http://www.foo.com/');
      exit(0);
   &#125;
?>
<html>
<head><title>Foo</title></head>
<body>
Hi :)
<!-- This WILL work -->
</body>
</html>
Post Reply