-5

I want to create a web page so that when a user opens it I want to get the client's IP address. Any idea how to do that?

3 Answers3

2

You can use the $_SERVER super-global:

$userIp = $_SERVER['REMOTE_ADDR'];

From the docs:

REMOTE_ADDR
The IP address from which the user is viewing the current page.

BenM
  • 52,573
  • 26
  • 113
  • 168
  • Rep whoring. The OP could have easily found the answer with a Google search. It wasn’t an issue with OP’s PHP code, just laziness. – Martin Bean Dec 10 '13 at 14:09
  • thanks , but didn't work ! $userIp = $_SERVER['REMOTE_ADDR']; echo $userIp; – user3049398 Dec 10 '13 at 14:09
  • Still a valid answer. No reason to DV... – BenM Dec 10 '13 at 14:09
  • @user3049398 This is definitely how it's done. The problem must be elsewhere. – BenM Dec 10 '13 at 14:10
  • @MartinBean If you feel that someone is supposed to get a downvote for laziness then it should be the OP in this case not people taking effort to provide an answer to the topic at hand. –  Dec 10 '13 at 14:11
  • @MartinBean maybe we should downvote your answer here for the same reason? http://stackoverflow.com/questions/10618734/how-do-you-create-a-rest-api-in-php/10618829#10618829 – BenM Dec 10 '13 at 14:12
  • @BenM That’s your prerogative. But bear in mind it’s over a year old, has 13 up-votes and has been called a “jewel” in comments, it’d be obvious you were only doing it because of sour grapes and not its content. – Martin Bean Dec 10 '13 at 14:37
2

You can get IP address using $_SERVER super global variable

 <?php
    echo  $_SERVER['REMOTE_ADDR'];
?>
Roopendra
  • 7,674
  • 16
  • 65
  • 92
  • already being answered.... – Suresh Kamrushi Dec 10 '13 at 14:06
  • Hey after post my answer I see – Roopendra Dec 10 '13 at 14:06
  • 2
    @SureshKamrushi more than one person can answer the same question. – qwertynl Dec 10 '13 at 14:07
  • yes. i did not given downvote, just commented only. – Suresh Kamrushi Dec 10 '13 at 14:08
  • 1
    downvote was probs people who are voting to close, as this is quite an easy thing to search. – James Dec 10 '13 at 14:09
  • @James Yup. I was one who down-voted. It’s not a programming issue, just laziness on OP’s behalf. – Martin Bean Dec 10 '13 at 14:10
  • 3
    @MartinBean then punish the OP, not the people answering. – BenM Dec 10 '13 at 14:10
  • @BenM I did. Then “punished” you for choosing to answer an easy question instead of voting to close it, as: “Questions asking for code must **demonstrate a minimal understanding of the problem being solved.**” – Martin Bean Dec 10 '13 at 14:15
  • @MartinBean oh wow. You mean like you did here > http://stackoverflow.com/questions/10618734/how-do-you-create-a-rest-api-in-php/10618829#10618829? – BenM Dec 10 '13 at 14:16
  • @BenM Yes. Because that could have easily been solved with just one line in PHP after a Google search *rolls eyes* – Martin Bean Dec 10 '13 at 14:19
  • My point is that you didn't provide him with any code. Only a link to a website that you likely found through Google. I fail to see how that's different... *rolls eyes* – BenM Dec 10 '13 at 14:21
  • 1
    @MartinBean BenM does have a point you know. That other question is actually closed as not constructive... (I know that doesn't argue against your point, but fair is fair surely..?) – James Dec 10 '13 at 14:22
  • @James I agree. But bear in mind that answer was submitted *over one year ago*. I may have posted links to resources and voted to close it myself in the present day. We live, learn, and grow. – Martin Bean Dec 10 '13 at 14:31
  • Agreed, things change in a year, and resources for the same questions build up over time too so search rather than ask etc. (FTR I never said what you did was wrong, just that BenM's point was valid ;) ) – James Dec 10 '13 at 14:36
  • @James I know and I agree: in hindsight as I say I would have probably opted to close that question myself. But that’s not the topic on hand and just an attempt to dig up the past to discredit my current actions. If I didn’t know any better, I’d say BenM was in politics with that approach ;) – Martin Bean Dec 10 '13 at 14:48
  • 2
    @MartinBean If you believe it so strongly, delete your 13 upvote accepted answer... *whistle* (just joking) – James Dec 10 '13 at 14:52
0

You can use the following super global:

$_SERVER['REMOTE_ADDR'];  

However note that people can easily spoof their IP, be on a shared IP, they're using a proxy (so not really their IP) etc.
So sometimes you can get results you may not expect/might not be accurate.

James
  • 4,644
  • 5
  • 37
  • 48