Solved

getting Notice: Undefined variable: ip how do I do this correctly?

Posted on 2008-10-01
4
317 Views
Last Modified: 2013-12-12
I have a probably simple issue..
I have all errors on and want them on. but I am getting an error message:
Notice: Undefined variable: ip in C:\Program Files\Zend\Apache2\htdocs\babyearth_testing\testip.php on line 65

Not sure why??
<?php

$ip = (isset($_SERVER["HTTP_CLIENT_IP"])) ? $_SERVER["HTTP_CLIENT_IP"] : $_SERVER["REMOTE_ADDR"];
 

echo "<br> Your IP recorded is: $ip";

?>

Open in new window

0
Comment
Question by:jbrashear72
  • 2
  • 2
4 Comments
 
LVL 82

Accepted Solution

by:
hielo earned 500 total points
ID: 22616336
try:
<?php
$ip = (isset($_SERVER["HTTP_CLIENT_IP"]) ? $_SERVER["HTTP_CLIENT_IP"] : (isset($_SERVER["REMOTE_ADDR"]) ? $_SERVER["REMOTE_ADDR"] : "EMPTY") );
 
echo "<br> Your IP recorded is: $ip";
?>

Open in new window

0
 
LVL 3

Author Comment

by:jbrashear72
ID: 22616600
That did it. Can you explain what corrected it?
I need to understand so if I load a variable I don't get the error.
0
 
LVL 82

Expert Comment

by:hielo
ID: 22616733
you have:
$ip = (isset($_SERVER["HTTP_CLIENT_IP"])) ?

but the value for $ip should be the value of the whole expression on the right. You are closing the first parenthesis immediately before the question mark, but you really need to close it at the very end of the statement.
$ip = (isset($_SERVER["HTTP_CLIENT_IP"]) ? ... : ... );

furthermore, you also need to make sure that
$_SERVER["REMOTE_ADDR"]

isset.
0
 
LVL 3

Author Comment

by:jbrashear72
ID: 22616822
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
PHP and Soap 3 29
Wordpress type image upload 10 31
How can I do a Select All on this page? 8 12
PHP json decode 9 20
Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
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.

760 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

22 Experts available now in Live!

Get 1:1 Help Now