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
Solved

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

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Fulfillment API php code sample 1 58
How to check if a session is disconnected in php 9 32
How to delete values from an array using php 4 22
if (is_singular not working 5 18
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article discusses four methods for overlaying images in a container on a web page
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…

861 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