Solved

$_SERVER['REMOTE_ADDR']; not working in PHP5

Posted on 2009-05-12
5
2,452 Views
Last Modified: 2012-05-06
Here is the error I get from the code below.

PHP Notice:  Undefined index:  REMOTE_ADDR

This causing my entire script to fail.

Running PHP 5.1.6 (cli) (built: Apr  7 2009 08:00:04)

<?
 
$ip=$_SERVER['REMOTE_ADDR'];
 
echo $ip;
 
?>

Open in new window

0
Comment
Question by:pdheady
  • 3
  • 2
5 Comments
 
LVL 34

Accepted Solution

by:
Beverley Portlock earned 500 total points
ID: 24365556
The actual code you have given works OK. The only oddball is the short tag <? instead of <?php but I do not think that is the cause. I suspect there is a build in the build. I'll do a bit of digging.

If you add phpinfo() to your script and look at the output, does REMOTE_ADDR show up in the output?
0
 
LVL 1

Author Comment

by:pdheady
ID: 24365640
That is just a snippet of my code. I always use <?php...  sorry for confusion

Yes, REMOTE_ADDR is showing up in phpinfo(); test page and I see my IP address there.

I checked /etc/php.ini and register globals is off which should be fine.
0
 
LVL 1

Author Comment

by:pdheady
ID: 24365710
Ok I found problem and it's really my fault.

I'm running a web-service and connecting with client to web-service from the same server, so my IP detection was working just that the script was failing on output because of other syntax errrors. When debug on command line it was complaining about PHP Notice:  Undefined index:  REMOTE_ADDR

It is detecting the IP, but this still leads me to ask why is PHP still outputting that error on command line?

PHP Notice:  Undefined index:  REMOTE_ADDR
0
 
LVL 34

Assisted Solution

by:Beverley Portlock
Beverley Portlock earned 500 total points
ID: 24365777
I thought it was a bit odd. PHP "notice" errors can usually be ignored, but I have heard of some instances where $_SERVER variables go missing, particularly under Windows and I was thinking more along those lines myself.

Glad you're sorted.
0
 
LVL 1

Author Comment

by:pdheady
ID: 24365821
Thx
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Row insertion failed. Array 5 60
Change static entered data to dynamic with counter 5 32
How Close unsubmited attempts 10 42
CURL sending XML without spaces PHP 5 28
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question