Solved

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

Posted on 2008-10-01
4
333 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
TCPDF Table of Contents - Page Numbers Don't Right Align 2 37
Saving image into server to get url later 1 31
PHP warning 4 30
datetime in sql 6 28
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 …
Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
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 create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

910 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

16 Experts available now in Live!

Get 1:1 Help Now