1

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 ?

al'ein
  • 1,711
  • 1
  • 14
  • 21
katra
  • 11
  • 2

2 Answers2

2

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!

FerK
  • 21
  • 3
  • why echo $_SERVER['SERVER_ADDR'] display different results – katra Oct 11 '15 at 15:29
  • IP is dynamic, is not allways the same. If you disconnect internet or change your network or anything, system assign a new one. If you want to make the page session to work with the IP, I must tell you, this is not the way. Read some tutorial about sessions in PHP. Vote up if useful. :) – FerK Oct 13 '15 at 19:38
0

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'];
}
Community
  • 1
  • 1
Bizmate
  • 1,835
  • 1
  • 15
  • 19
  • yes, this is the oldest way.... bt doesnt work for me. ip changes after some time. it never stay same – katra Oct 11 '15 at 15:26
  • 2 points, 1) there is nothing old in this. Symfony framework for instance uses this in its dev environment see example https://github.com/bizmate/swothub/blob/master/symfony/web/app_dev.php#L12-L18 2) You question is about how to check your own ip address to then allow access. Nothing in your question suggests your IP changes and you would not just check on a fixed ip address if you needed this to change. If you need a different solution try to explain your questions better – Bizmate Oct 12 '15 at 17:24