Solved

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

Posted on 2009-05-12
5
2,418 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
updating the date data 12 19
Session timeout 5 13
javascript date picker in php while loop 5 20
PHP: Insert Data into MySQL 5 0
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
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…
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 create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

929 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now