homepage Welcome to WebmasterWorld Guest from 54.205.7.136
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
getenv() function question
why doesn't this work?
madcat




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

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




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

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

madcat




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

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




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

Why not just do this:

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

toadhall




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

dhdweb

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

madcat




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

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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved