Welcome to WebmasterWorld Guest from 54.226.25.231

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

getenv() function question

why doesn't this work?

     

madcat

12:19 am on Oct 11, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In my book, it has a couple of examples to follow along with- it is suggested to use:
<?php 
$address = getenv("REMOTE_ADDR");
echo "Your IP address is $address.";
?>

But only this will work:

<?php 
$address = $_SERVER['REMOTE_ADDR'];
echo "Your IP address is $address.";
?>

--> wrapping getenv() around the latter fails too.

So, should I be using getenv()?
Why doesn't it produce the users IP address in this case?

php.net mentions this form:


if (getenv(HTTP_X_FORWARDED_FOR)){
$ip=getenv(HTTP_X_FORWARDED_FOR);
}
else {
$ip=getenv(REMOTE_ADDR);
}

Thanks for any clarity-

SmallTime

4:40 am on Oct 11, 2002 (gmt 0)

10+ Year Member



I think it depends whether register_globals is on or off, default used to be on, now off. Check php.info

madcat

5:13 pm on Oct 11, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'll check it out really...but php.net has as example:

<?php
$address = getenv('REMOTE_ADDR');
echo "Your IP address is $address.";
?>

>> Your IP address is .
-------------------------------------------------
Using:

<?php
$address = $_SERVER['HTTP_USER_AGENT'];
echo "Your IP address is $address.";
?>
OR...
<?php
$address = $_SERVER['REMOTE_ADDR'];
echo "Your IP address is $address.";
?>

Work correctly- so are they wrong to use? Is getenv() necessary? I'm using PHP 4.2.2- just a bit confusing when your trying to learn.

Thanks-

dhdweb

6:41 pm on Oct 11, 2002 (gmt 0)

10+ Year Member



Why not just do this:

<?
$address = "$REMOTE_ADDR";
echo "Your IP is $address";
?>

toadhall

7:22 pm on Oct 11, 2002 (gmt 0)

10+ Year Member



dhdweb

If register_globals is turned off $REMOTE_ADDR won't work. You have to use $_SERVER['REMOTE_ADDR'];

madcat

8:56 pm on Oct 11, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I did the latter- I guess I don't need to use getenv() to extract the value and place it in a variable-- it's just what php.net and my author suggest- so i thought it was the right thing.
 

Featured Threads

Hot Threads This Week

Hot Threads This Month