-2

Could you please give me hint or sample code for restricting access to website to only specific IP addresses using no more or less than JS / jQuery?

I found a lot of examples for other methods like htaccess or so, but not for JS

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
Davor B
  • 11
  • 4
  • 2
    Javascript code only runs after the user already loaded the site. – SLaks Dec 15 '14 at 19:20
  • `if ( user != me ) return;` – adeneo Dec 15 '14 at 19:21
  • It doesn't have to be secured. I just don't want to show that page to "every day" web users. – Davor B Dec 15 '14 at 19:21
  • 1
    If you're hiding anything sensitive based on IP, then you'll probably have to go with server-side checks. Will it be bad/dangerous if a user messes with some javascript and accesses your IP-restricted page, or is it just to make it look different? – zaparker Dec 15 '14 at 19:24

2 Answers2

1

You can't achieve this with client side code. If you want to use JavaScript then you have to use server side JavaScript (e.g. via Node.js, Classic ASP w/JScript, etc).

The specifics of how you get the IP address will depend on the server side framework you use. Typically you would be able to retrieve it by accessing a method or property of whatever object represents.

(If you aren't using server side JS already, you probably should look towards a more mainstream solution such as the ones you've already found).

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • What about something like this: var allowedAddresses = ['127.0.0.1']; if (!currentAddress in allowedAddresses) document.body.innerHTML = ''; – Davor B Dec 15 '14 at 19:37
  • @DavorB — Turn JS off and the content is available. View source and the content is available. Use a text browser and the content is available. You can't make the client censor itself. – Quentin Dec 15 '14 at 19:45
  • ohh that's right. Didn't think of that. Thanks for note – Davor B Dec 15 '14 at 19:47
0

JS is clientside - you cannot restrict them from seeing the page... using the page.

What kind of backend are you running? It is trivial through an endpoint.

Michael Voznesensky
  • 1,612
  • 12
  • 15