Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect if running on localhost

I have a PHP script where I'd like to detect if the user is running on a local machine, not accessible over the Internet. Currently I check for the server address to he 127.0.0.1. Is this the best practice or is there a better way?

like image 201
Alex Dunae Avatar asked Jan 01 '11 02:01

Alex Dunae


1 Answers

Localhost always translates to the loopback IP address 127.0.0.1 in IPv4, or ::1 in IPv6, So validating the IP Within your application would be secure, if you mean

if(IPAddress::In(array("127.0.0.1","::1")))
{
     //Show Application
}

I Very much doubt that you will have a team of elite hackers after your port 80 but as a side note there has been some talk about flaws in relying on an IP address as TCP Packets can be modified.

But that should not be a worry for you.

like image 130
RobertPitt Avatar answered Oct 10 '22 23:10

RobertPitt