user2777052
user2777052

Reputation: 155

jquery equivalent of php 'if ip equals' condition?

I'd like to know, can the following PHP 'if' conditions can be done in jquery?

    if ($_SERVER['REMOTE_ADDR'] != '123.99.55.616') {
      // public doingness
    }

    if ($_SERVER['REMOTE_ADDR'] == '123.99.55.616') {
      // self doingness
    }

Or javascript, but preferably jquery; thanks.

(Of course, if I could run these PHP snippets from within a JS file that would suit me just fine, but all the reading I've done so far leads me to believe it might not be doable.)

Upvotes: 0

Views: 1150

Answers (4)

user3040610
user3040610

Reputation: 760

In JQuery you can get remote address with the below code

$.getJSON("http://jsonip.com?callback=?", function (data) {
alert(data.ip);
});

Upvotes: 0

uvais
uvais

Reputation: 416

$_SERVER is a php variable and it will not use in jquery, but you can store its value in some html element and then can fetch into jquery

Upvotes: 0

Subash Selvaraj
Subash Selvaraj

Reputation: 3385

Use jsonip.com's json response.

$.getJSON("http://jsonip.com",
    function(data){
       alert( "Your ip: " + data.ip);
  });

fiddle

Hope it helps

Upvotes: 1

Rajiv Ranjan
Rajiv Ranjan

Reputation: 1869

Just pass an url formatted with the callback=? parameter to the $.getJSON method to get IP Remote IP Address, for example:

 $.getJSON("http://jsonip.appspot.com?callback=?",
    function(data){
       alert( "Your ip: " + data.ip);
  });

Hope will help!

Upvotes: 0

Related Questions