?
Solved

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

Posted on 2008-10-01
4
Medium Priority
?
381 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
[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
  • 2
  • 2
4 Comments
 
LVL 82

Accepted Solution

by:
hielo earned 2000 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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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 …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…
Suggested Courses

764 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