Solved

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

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

Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
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…
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…

758 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

19 Experts available now in Live!

Get 1:1 Help Now