Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Page can not Entry to only by ip

Hello dear

i want to make Page can not Entry to only by ip

Thank you

1 Answer

Chris McKirgan
Chris McKirgan
5,666 Points

You can get the client IP sent in the request using the PHP function:

$_SERVER['REMOTE_ADDR']

However, this function should be considered unsafe, as this can be forged by the connecting party. This is, however, the most reliable way to get the IP address in PHP. Limiting by IP in PHP, generally speaking, is not a good idea for secure applications, but may be useful for many reasons.

See: http://php.net/manual/en/reserved.variables.server.php

One way to limit access to an application is to place the following on the calling script such as this:

<?php
  // set to your IP
  $sYourIP = '192.168.0.25';
  // will kill execution if the remote address sent is not the one specified.
   if($_SERVER['REMOTE_ADDR'] != $sYourIP){
     die('Sorry, this is limited to IP: ' . $sYourIP);
  }

?>

P.S. also remember IPv6 is a valid schema, and if your networked PHP server serves to IPv6, then you should support this.

Nice Man THNX