Solved

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

Posted on 2009-05-12
5
2,484 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

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…
This article discusses four methods for overlaying images in a container on a web page
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

617 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