katra
katra

Reputation: 11

How to check my own ip address in php

I am trying to check my ip address and if the ip is mine the display my name other wise display This is not me.

My code is some thing like this:

$my_ip = $_SERVER['SERVER_ADDR'];
if($my_ip == '113.199.172.73'){echo "This is john";} else {"This is not me";}

At the beginning I check echo $_SERVER['SERVER_ADDR']; and output was 113.199.172.73

Someone can help ?

Upvotes: 1

Views: 1753

Answers (2)

FerK
FerK

Reputation: 21

You already have the ip, and the "if" sentence is not working.

This could be because you are comparing different kind of variables. If you want to compare both as literals, you should use triple eq. symbol

   $my_ip === '113.199.172.73'

Or you could use the

strcmp(a,b)

function instead wich returns 0 if both strings are the equal.

Good luck!

Upvotes: 2

Bizmate
Bizmate

Reputation: 1872

There are so many duplicates of this question

How to get the client IP address in PHP?

in short

if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
    $ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
    $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
    $ip = $_SERVER['REMOTE_ADDR'];
}

Upvotes: 0

Related Questions