Solved

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

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

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…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

816 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

7 Experts available now in Live!

Get 1:1 Help Now