0

I'm trying to get the user's ip address once the page loads with the following:

<?php $user_ip = $_SERVER['REMOTE_ADDR']; ?> 

and trying to print the value like so:

<input id="ip" name="ip" value="<?php $user_ip = echo ($user_ip); ?>" type="hidden"> 

for some reason it won't print. Any ideas?

4
  • 3
    try remove $user_ip = and just do echo $user_ip; Commented Dec 4, 2013 at 14:52
  • 6
    It should be noted that anyone can change the form value if they want to. Do not expect the ip form value to be correct. Commented Dec 4, 2013 at 14:54
  • 1
    echo is a language construct. it has no return value. $foo = echo ... is utterly pointless. Commented Dec 4, 2013 at 14:58
  • h2o is correct. If you need the value on the server side for some reason (I'm guessing so since it's a hidden input) you should just assign it using $_SERVER['REMOTE_ADDR']; again not get it from a POSTed form. If you're just displaying it to the user for their own benefit, why is it hidden? Commented Dec 4, 2013 at 14:59

1 Answer 1

6

If you assign the echo result, it will print nothing :

<input id="ip" name="ip" value="<?php echo ($user_ip); ?>" type="hidden"> 

If you want to display it to user :

<input id="ip" name="ip" value="<?php echo ($user_ip); ?>" type="text"> 
Sign up to request clarification or add additional context in comments.

7 Comments

So how should I write this php in order print the ip address?
Well you have chosen type="hidden", so it is not displayed to user on the page. If you want to display it, use type="text"
Okay, I see now but it doesn't display an IP address. It just prints the following: <?php echo ($user_ip); ?>
@J-LO whats the file extension?
Do you manage to execute php on your server ? Because it should work. Is it a .php file that you call ?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.